Skip to content

Commit

Permalink
Websocket: fixed memory leak on startPinger
Browse files Browse the repository at this point in the history
  • Loading branch information
jerson committed Oct 16, 2017
1 parent 0d1de4c commit 85816fd
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions websocket/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func newConnection(ctx context.Context, s *Server, underlineConn UnderlineConnec

// write writes a raw websocket message with a specific type to the client
// used by ping messages and any CloseMessage types.
func (c *connection) write(websocketMessageType int, data []byte) {
func (c *connection) write(websocketMessageType int, data []byte) error {
// for any-case the app tries to write from different goroutines,
// we must protect them because they're reporting that as bug...
c.writerMu.Lock()
Expand All @@ -266,6 +266,7 @@ func (c *connection) write(websocketMessageType int, data []byte) {
// if failed then the connection is off, fire the disconnect
c.Disconnect()
}
return err
}

// writeDefault is the same as write but the message type is the configured by c.messageType
Expand Down Expand Up @@ -305,7 +306,11 @@ func (c *connection) startPinger() {
// wait for each tick
<-c.pinger.C
// try to ping the client, if failed then it disconnects
c.write(websocket.PingMessage, []byte{})
err := c.write(websocket.PingMessage, []byte{})
if err!=nil{
//must stop to exit the loop and finish the go routine
break
}
}
}()
}
Expand Down

0 comments on commit 85816fd

Please sign in to comment.