From 732c9f70ed39626de80f6880b103902ce0a59648 Mon Sep 17 00:00:00 2001 From: Gerson Alexander Pardo Gamez Date: Thu, 19 Oct 2017 22:06:31 -0500 Subject: [PATCH] Websocket: replaced time.Ticker with sleep for avoid memory leak Former-commit-id: f17706649faebe3020792e31de877c724be41057 --- websocket/connection.go | 16 ++++++++-------- websocket/server.go | 2 -- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/websocket/connection.go b/websocket/connection.go index c0919eb2..2e6f46cf 100644 --- a/websocket/connection.go +++ b/websocket/connection.go @@ -196,7 +196,6 @@ type ( underline UnderlineConnection id string messageType int - pinger *time.Ticker disconnected bool onDisconnectListeners []DisconnectFunc onRoomLeaveListeners []LeaveRoomFunc @@ -298,17 +297,18 @@ func (c *connection) startPinger() { c.underline.SetPingHandler(pingHandler) - // start a new timer ticker based on the configuration - c.pinger = time.NewTicker(c.server.config.PingPeriod) - go func() { for { - // wait for each tick - <-c.pinger.C + // using sleep avoids the ticker error that causes a memory leak + time.Sleep(c.server.config.PingPeriod) + if c.disconnected { + // verifies if already disconected + break + } // try to ping the client, if failed then it disconnects err := c.write(websocket.PingMessage, []byte{}) - if err!=nil{ - //must stop to exit the loop and finish the go routine + if err != nil { + // must stop to exit the loop and finish the go routine break } } diff --git a/websocket/server.go b/websocket/server.go index cbfd7567..02a7ecdb 100644 --- a/websocket/server.go +++ b/websocket/server.go @@ -378,8 +378,6 @@ func (s *Server) Disconnect(connID string) (err error) { if c, ok := s.connections.remove(connID); ok { if !c.disconnected { c.disconnected = true - // stop the ping timer - c.pinger.Stop() // fire the disconnect callbacks, if any c.fireDisconnect()