Skip to content

Commit

Permalink
replace time.Ticker with time.Sleep in websocket pinger
Browse files Browse the repository at this point in the history
Websocket: replaced time.Ticker with sleep for avoid memory leak
  • Loading branch information
kataras committed Oct 20, 2017
2 parents 72fca9c + 2623df6 commit 6aecdd4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
16 changes: 8 additions & 8 deletions websocket/connection.go
Expand Up @@ -196,7 +196,6 @@ type (
underline UnderlineConnection
id string
messageType int
pinger *time.Ticker
disconnected bool
onDisconnectListeners []DisconnectFunc
onRoomLeaveListeners []LeaveRoomFunc
Expand Down Expand Up @@ -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
}
}
Expand Down
2 changes: 0 additions & 2 deletions websocket/server.go
Expand Up @@ -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()
Expand Down

0 comments on commit 6aecdd4

Please sign in to comment.