diff --git a/v2/delivery/websocket.go b/v2/delivery/websocket.go index 6d0f1f0b8..f500ff3a7 100644 --- a/v2/delivery/websocket.go +++ b/v2/delivery/websocket.go @@ -3,6 +3,7 @@ package delivery import ( "net/http" "net/url" + "sync/atomic" "time" "github.com/gorilla/websocket" @@ -86,7 +87,7 @@ var wsServe = func(cfg *WsConfig, handler WsHandler, errHandler ErrHandler) (don func keepAlive(c *websocket.Conn, timeout time.Duration) { ticker := time.NewTicker(timeout) - lastResponse := time.Now() + lastResponse := time.Now().UnixNano() c.SetPingHandler(func(pingData string) error { // Respond with Pong using the server's PING payload @@ -99,7 +100,7 @@ func keepAlive(c *websocket.Conn, timeout time.Duration) { return err } - lastResponse = time.Now() + atomic.StoreInt64(&lastResponse, time.Now().UnixNano()) return nil }) @@ -108,7 +109,8 @@ func keepAlive(c *websocket.Conn, timeout time.Duration) { defer ticker.Stop() for { <-ticker.C - if time.Since(lastResponse) > timeout { + last := atomic.LoadInt64(&lastResponse) + if time.Since(time.Unix(0, last)) > timeout { c.Close() return } diff --git a/v2/futures/websocket.go b/v2/futures/websocket.go index 7d54b7404..8a8db5039 100644 --- a/v2/futures/websocket.go +++ b/v2/futures/websocket.go @@ -3,6 +3,7 @@ package futures import ( "net/http" "net/url" + "sync/atomic" "time" "github.com/gorilla/websocket" @@ -86,7 +87,7 @@ var wsServe = func(cfg *WsConfig, handler WsHandler, errHandler ErrHandler) (don func keepAlive(c *websocket.Conn, timeout time.Duration) { ticker := time.NewTicker(timeout) - lastResponse := time.Now() + lastResponse := time.Now().UnixNano() c.SetPingHandler(func(pingData string) error { // Respond with Pong using the server's PING payload @@ -99,7 +100,7 @@ func keepAlive(c *websocket.Conn, timeout time.Duration) { return err } - lastResponse = time.Now() + atomic.StoreInt64(&lastResponse, time.Now().UnixNano()) return nil }) @@ -108,7 +109,8 @@ func keepAlive(c *websocket.Conn, timeout time.Duration) { defer ticker.Stop() for { <-ticker.C - if time.Since(lastResponse) > timeout { + last := atomic.LoadInt64(&lastResponse) + if time.Since(time.Unix(0, last)) > timeout { c.Close() return } diff --git a/v2/options/websocket.go b/v2/options/websocket.go index 6d3d4d54c..f5e170a5c 100644 --- a/v2/options/websocket.go +++ b/v2/options/websocket.go @@ -3,6 +3,7 @@ package options import ( "net/http" "net/url" + "sync/atomic" "time" "github.com/gorilla/websocket" @@ -87,7 +88,7 @@ var wsServe = func(cfg *WsConfig, handler WsHandler, errHandler ErrHandler) (don func keepAlive(c *websocket.Conn, timeout time.Duration) { ticker := time.NewTicker(timeout) - lastResponse := time.Now() + lastResponse := time.Now().UnixNano() c.SetPingHandler(func(pingData string) error { // Respond with Pong using the server's PING payload @@ -100,7 +101,7 @@ func keepAlive(c *websocket.Conn, timeout time.Duration) { return err } - lastResponse = time.Now() + atomic.StoreInt64(&lastResponse, time.Now().UnixNano()) return nil }) @@ -109,7 +110,8 @@ func keepAlive(c *websocket.Conn, timeout time.Duration) { defer ticker.Stop() for { <-ticker.C - if time.Since(lastResponse) > timeout { + last := atomic.LoadInt64(&lastResponse) + if time.Since(time.Unix(0, last)) > timeout { c.Close() return } diff --git a/v2/portfolio/websocket.go b/v2/portfolio/websocket.go index 4e79afe1f..7c665e1f4 100644 --- a/v2/portfolio/websocket.go +++ b/v2/portfolio/websocket.go @@ -3,6 +3,7 @@ package portfolio import ( "net/http" "net/url" + "sync/atomic" "time" "github.com/gorilla/websocket" @@ -86,7 +87,7 @@ var wsServe = func(cfg *WsConfig, handler WsHandler, errHandler ErrHandler) (don func keepAlive(c *websocket.Conn, timeout time.Duration) { ticker := time.NewTicker(timeout) - lastResponse := time.Now() + lastResponse := time.Now().UnixNano() c.SetPingHandler(func(pingData string) error { // Respond with Pong using the server's PING payload @@ -99,7 +100,7 @@ func keepAlive(c *websocket.Conn, timeout time.Duration) { return err } - lastResponse = time.Now() + atomic.StoreInt64(&lastResponse, time.Now().UnixNano()) return nil }) @@ -108,7 +109,8 @@ func keepAlive(c *websocket.Conn, timeout time.Duration) { defer ticker.Stop() for { <-ticker.C - if time.Since(lastResponse) > timeout { + last := atomic.LoadInt64(&lastResponse) + if time.Since(time.Unix(0, last)) > timeout { c.Close() return } diff --git a/v2/websocket.go b/v2/websocket.go index 864443787..e9e2e4c72 100644 --- a/v2/websocket.go +++ b/v2/websocket.go @@ -3,6 +3,7 @@ package binance import ( "net/http" "net/url" + "sync/atomic" "time" "github.com/gorilla/websocket" @@ -90,7 +91,7 @@ var wsServe = func(cfg *WsConfig, handler WsHandler, errHandler ErrHandler) (don func keepAlive(c *websocket.Conn, timeout time.Duration) { ticker := time.NewTicker(timeout) - lastResponse := time.Now() + lastResponse := time.Now().UnixNano() c.SetPingHandler(func(pingData string) error { // Respond with Pong using the server's PING payload @@ -103,7 +104,7 @@ func keepAlive(c *websocket.Conn, timeout time.Duration) { return err } - lastResponse = time.Now() + atomic.StoreInt64(&lastResponse, time.Now().UnixNano()) return nil }) @@ -112,7 +113,8 @@ func keepAlive(c *websocket.Conn, timeout time.Duration) { defer ticker.Stop() for { <-ticker.C - if time.Since(lastResponse) > timeout { + last := atomic.LoadInt64(&lastResponse) + if time.Since(time.Unix(0, last)) > timeout { c.Close() return }