Use a mutex to ensure SetPongHandler atomicity

This commit is contained in:
Matt Caldwell
2016-05-08 20:21:27 -04:00
parent aa4fbf9617
commit 3fb44fab6c
+18 -7
View File
@@ -4,6 +4,7 @@ import (
"errors"
"github.com/gorilla/websocket"
"net/http"
"sync"
"time"
)
@@ -11,6 +12,7 @@ import (
type Session struct {
Request *http.Request
conn *websocket.Conn
mutex sync.Mutex
output chan *envelope
melody *Melody
}
@@ -75,10 +77,14 @@ loop:
}
func (s *Session) SetPongHandler(f func() error) {
s.conn.SetPongHandler(func(string) error {
s.conn.SetReadDeadline(time.Now().Add(s.melody.Config.PongWait))
return f()
})
s.mutex.Lock()
{
s.conn.SetPongHandler(func(string) error {
s.conn.SetReadDeadline(time.Now().Add(s.melody.Config.PongWait))
return f()
})
}
s.mutex.Unlock()
}
func (s *Session) readPump() {
@@ -87,9 +93,14 @@ func (s *Session) readPump() {
s.conn.SetReadLimit(s.melody.Config.MaxMessageSize)
s.conn.SetReadDeadline(time.Now().Add(s.melody.Config.PongWait))
s.SetPongHandler(func() error {
return nil
})
s.mutex.Lock()
{
s.conn.SetPongHandler(func(string) error {
s.conn.SetReadDeadline(time.Now().Add(s.melody.Config.PongWait))
return nil
})
}
s.mutex.Unlock()
for {
t, message, err := s.conn.ReadMessage()