Use a mutex to ensure SetPongHandler atomicity
This commit is contained in:
+18
-7
@@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -11,6 +12,7 @@ import (
|
|||||||
type Session struct {
|
type Session struct {
|
||||||
Request *http.Request
|
Request *http.Request
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
|
mutex sync.Mutex
|
||||||
output chan *envelope
|
output chan *envelope
|
||||||
melody *Melody
|
melody *Melody
|
||||||
}
|
}
|
||||||
@@ -75,10 +77,14 @@ loop:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) SetPongHandler(f func() error) {
|
func (s *Session) SetPongHandler(f func() error) {
|
||||||
s.conn.SetPongHandler(func(string) error {
|
s.mutex.Lock()
|
||||||
s.conn.SetReadDeadline(time.Now().Add(s.melody.Config.PongWait))
|
{
|
||||||
return f()
|
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() {
|
func (s *Session) readPump() {
|
||||||
@@ -87,9 +93,14 @@ func (s *Session) readPump() {
|
|||||||
s.conn.SetReadLimit(s.melody.Config.MaxMessageSize)
|
s.conn.SetReadLimit(s.melody.Config.MaxMessageSize)
|
||||||
s.conn.SetReadDeadline(time.Now().Add(s.melody.Config.PongWait))
|
s.conn.SetReadDeadline(time.Now().Add(s.melody.Config.PongWait))
|
||||||
|
|
||||||
s.SetPongHandler(func() error {
|
s.mutex.Lock()
|
||||||
return nil
|
{
|
||||||
})
|
s.conn.SetPongHandler(func(string) error {
|
||||||
|
s.conn.SetReadDeadline(time.Now().Add(s.melody.Config.PongWait))
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
s.mutex.Unlock()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
t, message, err := s.conn.ReadMessage()
|
t, message, err := s.conn.ReadMessage()
|
||||||
|
|||||||
Reference in New Issue
Block a user