optimal ws health check

This commit is contained in:
akrike
2025-02-16 12:47:39 +08:00
parent 7343f45dda
commit de296ee9cf

View File

@@ -113,7 +113,7 @@ func (w *wsApi) startWsConnect(wci *WsConnetInstance, cancel context.CancelFunc,
pongChan <- struct{}{} pongChan <- struct{}{}
return nil return nil
}) })
timer := time.NewTicker(time.Second * time.Duration(config.CF.WsHealthCheckInterval)) timer := time.NewTicker(time.Second)
go func() { go func() {
defer timer.Stop() defer timer.Stop()
for { for {
@@ -121,13 +121,13 @@ func (w *wsApi) startWsConnect(wci *WsConnetInstance, cancel context.CancelFunc,
wci.WsConnect.WriteMessage(websocket.PingMessage, nil) wci.WsConnect.WriteMessage(websocket.PingMessage, nil)
wci.wsLock.Unlock() wci.wsLock.Unlock()
select { select {
case <-pongChan:
timer.Reset(time.Second * time.Duration(config.CF.WsHealthCheckInterval))
case <-timer.C: case <-timer.C:
log.Logger.Debugw("pong报文超时,结束ws连接")
cancel() cancel()
return return
case <-pongChan:
} }
time.Sleep(time.Second * time.Duration(config.CF.WsHealthCheckInterval))
timer.Reset(time.Second)
} }
}() }()