From de296ee9cf6239a77b64910b26c752d41f11f59d Mon Sep 17 00:00:00 2001 From: akrike <1625167628@qq.com> Date: Sun, 16 Feb 2025 12:47:39 +0800 Subject: [PATCH] optimal ws health check --- internal/app/api/ws.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/app/api/ws.go b/internal/app/api/ws.go index 2e1d8a0..f472685 100644 --- a/internal/app/api/ws.go +++ b/internal/app/api/ws.go @@ -113,7 +113,7 @@ func (w *wsApi) startWsConnect(wci *WsConnetInstance, cancel context.CancelFunc, pongChan <- struct{}{} return nil }) - timer := time.NewTicker(time.Second * time.Duration(config.CF.WsHealthCheckInterval)) + timer := time.NewTicker(time.Second) go func() { defer timer.Stop() for { @@ -121,13 +121,13 @@ func (w *wsApi) startWsConnect(wci *WsConnetInstance, cancel context.CancelFunc, wci.WsConnect.WriteMessage(websocket.PingMessage, nil) wci.wsLock.Unlock() select { - case <-pongChan: - timer.Reset(time.Second * time.Duration(config.CF.WsHealthCheckInterval)) case <-timer.C: - log.Logger.Debugw("pong报文超时,结束ws连接") cancel() return + case <-pongChan: } + time.Sleep(time.Second * time.Duration(config.CF.WsHealthCheckInterval)) + timer.Reset(time.Second) } }()