mq/app/http/tcppool/single.go

199 lines
4.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package tcppool
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/qit-team/snow-core/redis"
"net"
"quenue/app/utils"
"quenue/config"
"strings"
"sync"
"sync/atomic"
"time"
)
var (
full int32 = 0
TcpFactory = TcpHelper{Full: &full}
lockSingle sync.Once
OrderMap = sync.Map{}
expire = 10 * time.Second
)
type TcpHelper struct {
client net.Conn
lastTime int64
Full *int32
}
func (t *TcpHelper) Init(port string) *TcpHelper {
lockSingle.Do(func() {
var conn, err = net.DialTimeout("tcp", config.GetConf().Url+":"+port, expire)
if err == nil {
t.client = conn
atomic.StoreInt32(t.Full, 0)
t.watch(t.client)
t.resend()
} else {
atomic.StoreInt32(t.Full, 1)
t.reconnect(port)
utils.Log(nil, config.GetConf().Url+":"+port)
}
})
return t
}
func (t *TcpHelper) reconnect(port string) {
var conn, err = net.DialTimeout("tcp", config.GetConf().Url+":"+port, expire)
if err == nil {
//utils.Log(nil, "连接下游成功")
atomic.StoreInt32(t.Full, 0)
t.client = conn
t.watch(t.client)
} else {
//utils.Log(nil, "重连下游")
time.Sleep(1 * time.Second)
atomic.StoreInt32(t.Full, 1)
//t.client = conn
t.reconnect(port)
}
}
func (t *TcpHelper) SendMsg(msg []byte) error {
msg = append(msg, '\n')
t = t.Init(config.GetConf().OrderPort)
if t.client == nil {
return errors.New("客户端连接失败")
}
var start = time.Now().Unix()
var clinet = t.Init(config.GetConf().OrderPort).client
clinet.SetReadDeadline(time.Now().Add(time.Second * 10))
_, err := clinet.Write(msg)
var end = time.Now().Unix()
fmt.Println(end-start, "秒")
buf := make([]byte, 1024)
reader := bufio.NewReader(clinet)
line, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Error reading from connection:", err)
return err
}
fmt.Println("结果recvStr:", line)
// 将读取到的字符串转换为字节切片
buf = []byte(line)
if err == nil {
if len(buf) > 0 {
recvStr := string(buf)
recvStr = strings.Replace(recvStr, "\n", "", 1)
if len(recvStr) > 2 {
var orderNo = recvStr
utils.LogFile(nil, "ack", orderNo)
if _, ok := OrderMap.Load(orderNo); !ok {
OrderMap.Store(orderNo, "1")
return nil
}
} else {
if len(recvStr) > 0 {
fmt.Println("结果recvStr:", recvStr)
if recvStr == "5" {
fmt.Println("客户端繁忙")
atomic.StoreInt32(t.Full, 1)
} else if recvStr == "2" {
fmt.Println("客户端空闲")
atomic.StoreInt32(t.Full, 0)
}
}
}
}
return errors.New("无效ack")
}
return err
}
func (t *TcpHelper) Close(conn net.Conn) {
t.client.Close()
}
func (t *TcpHelper) resend() {
go func() {
defer func() {
if err := recover(); err != nil {
utils.Log(nil, "重发失败", err)
}
}()
for {
if t.client == nil {
return
}
rs, err := redis.GetRedis().HGetAll(context.Background(), "kafka_message").Result()
if err == nil {
var data = map[string]interface{}{}
var nowTime = time.Now().Unix()
for _, v := range rs {
json.Unmarshal([]byte(v), &data)
if (float64(nowTime) - data["send_time"].(float64)) > 60 {
t.SendMsg([]byte(v))
}
time.Sleep(200 * time.Millisecond)
}
}
time.Sleep(3 * time.Minute)
}
}()
}
func (t *TcpHelper) watch(conn net.Conn) {
go func() {
defer func() {
if err := recover(); err != nil {
fmt.Println("连接断开", err)
}
}()
for {
if t.client == nil {
return
}
conn.SetWriteDeadline(time.Now().Add(expire))
_, err := conn.Write([]byte("1\n"))
if err != nil {
//utils.Log(nil, "连接关闭", err)
atomic.StoreInt32(t.Full, 1)
t.client.Close()
t.reconnect(config.GetConf().OrderPort)
return
} else {
var buffer = make([]byte, 1)
// 持续读取数据
t.client.SetReadDeadline(time.Now().Add(expire))
n, err := t.client.Read(buffer[:])
if err == nil && n > 0 {
recvStr := string(buffer[:n])
//fmt.Println("结果recvStr:", recvStr)
if recvStr == "5" {
utils.Log(nil, "客户端繁忙")
atomic.StoreInt32(t.Full, 1)
} else if recvStr == "2" {
utils.Log(nil, "客户端空闲")
atomic.StoreInt32(t.Full, 0)
} else if recvStr == "6" {
utils.Log(nil, "客户端心跳")
atomic.StoreInt32(t.Full, 0)
}
} else {
atomic.StoreInt32(t.Full, 1)
//utils.Log(nil, "连接关闭", err)
t.client.Close()
t.client = nil
t.reconnect(config.GetConf().OrderPort)
}
}
time.Sleep(2 * time.Second)
}
}()
}