From 81554e8fd2370bdd989061150e17c0496659328a Mon Sep 17 00:00:00 2001 From: sawka Date: Tue, 29 Nov 2022 12:45:19 -0800 Subject: [PATCH] fix concurrent write on websocket (only happened under intense write stress) --- pkg/wsshell/wsshell.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/wsshell/wsshell.go b/pkg/wsshell/wsshell.go index 3bffac1f..d6fe69e7 100644 --- a/pkg/wsshell/wsshell.go +++ b/pkg/wsshell/wsshell.go @@ -78,15 +78,12 @@ func (ws *WSShell) WriteJson(val interface{}) error { } func (ws *WSShell) WritePump() { - ticker := time.NewTicker(pingPeriodTickTime) + ticker := time.NewTicker(initialPingTime) defer func() { ticker.Stop() ws.Conn.Close() }() - go func() { - time.Sleep(initialPingTime) - ws.WritePing() - }() + initialPing := true for { select { case <-ticker.C: @@ -95,6 +92,10 @@ func (ws *WSShell) WritePump() { log.Printf("WritePump %s err: %v\n", ws.RemoteAddr, err) return } + if initialPing { + initialPing = false + ticker.Reset(pingPeriodTickTime) + } case msgBytes, ok := <-ws.WriteChan: if !ok {