优化短线后cpu飙升问题

This commit is contained in:
qiyunfanbo126.com 2025-01-20 20:28:37 +08:00
parent f5d5cf28d1
commit 4e7b508212
1 changed files with 11 additions and 3 deletions

View File

@ -39,9 +39,9 @@ func (t *TcpHelper) Init(port string) *TcpHelper {
return t
}
func (t *TcpHelper) reconnect(port string) {
var conn, err = net.Dial("tcp", config.GetConf().Url+":"+port)
var conn, err = net.DialTimeout("tcp", config.GetConf().Url+":"+port, 5*time.Second)
if err == nil {
utils.Log(nil, "连接下游失败")
utils.Log(nil, "连接下游成功")
atomic.StoreInt32(t.Full, 0)
t.client = conn
t.watch(t.client)
@ -88,6 +88,11 @@ func (t *TcpHelper) Close(conn net.Conn) {
}
func (t *TcpHelper) watch(conn net.Conn) {
go func() {
defer func() {
if err := recover(); err != nil {
fmt.Println("连接断开", err)
}
}()
for {
conn.SetWriteDeadline(time.Now().Add(time.Second * 5))
_, err := conn.Write([]byte("1"))
@ -112,9 +117,12 @@ func (t *TcpHelper) watch(conn net.Conn) {
fmt.Println("客户端空闲")
atomic.StoreInt32(t.Full, 0)
}
} else {
atomic.StoreInt32(t.Full, 1)
utils.Log(nil, "连接关闭", err)
}
}
time.Sleep(1 * time.Second)
}
}()
}