Websocket: replaced time.Ticker with sleep for avoid memory leak

Former-commit-id: f17706649faebe3020792e31de877c724be41057
This commit is contained in:
Gerson Alexander Pardo Gamez 2017-10-19 22:06:31 -05:00
parent dcde9d05a2
commit 732c9f70ed
2 changed files with 8 additions and 10 deletions

View File

@ -196,7 +196,6 @@ type (
underline UnderlineConnection underline UnderlineConnection
id string id string
messageType int messageType int
pinger *time.Ticker
disconnected bool disconnected bool
onDisconnectListeners []DisconnectFunc onDisconnectListeners []DisconnectFunc
onRoomLeaveListeners []LeaveRoomFunc onRoomLeaveListeners []LeaveRoomFunc
@ -298,17 +297,18 @@ func (c *connection) startPinger() {
c.underline.SetPingHandler(pingHandler) c.underline.SetPingHandler(pingHandler)
// start a new timer ticker based on the configuration
c.pinger = time.NewTicker(c.server.config.PingPeriod)
go func() { go func() {
for { for {
// wait for each tick // using sleep avoids the ticker error that causes a memory leak
<-c.pinger.C time.Sleep(c.server.config.PingPeriod)
if c.disconnected {
// verifies if already disconected
break
}
// try to ping the client, if failed then it disconnects // try to ping the client, if failed then it disconnects
err := c.write(websocket.PingMessage, []byte{}) err := c.write(websocket.PingMessage, []byte{})
if err!=nil{ if err != nil {
//must stop to exit the loop and finish the go routine // must stop to exit the loop and finish the go routine
break break
} }
} }

View File

@ -378,8 +378,6 @@ func (s *Server) Disconnect(connID string) (err error) {
if c, ok := s.connections.remove(connID); ok { if c, ok := s.connections.remove(connID); ok {
if !c.disconnected { if !c.disconnected {
c.disconnected = true c.disconnected = true
// stop the ping timer
c.pinger.Stop()
// fire the disconnect callbacks, if any // fire the disconnect callbacks, if any
c.fireDisconnect() c.fireDisconnect()