mirror of https://github.com/gorilla/websocket.git
Add mutex to prevent concurrent connection write
This commit is contained in:
parent
0b847f2fac
commit
ae4a5a85ff
|
@ -8,6 +8,7 @@ import (
|
|||
"bytes"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
|
@ -46,6 +47,9 @@ type Client struct {
|
|||
|
||||
// Buffered channel of outbound messages.
|
||||
send chan []byte
|
||||
|
||||
// Mutex to prevent concurrent connection write
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// readPump pumps messages from the websocket connection to the hub.
|
||||
|
@ -72,6 +76,8 @@ func (c *Client) readPump() {
|
|||
|
||||
// write writes a message with the given message type and payload.
|
||||
func (c *Client) write(mt int, payload []byte) error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
return c.conn.WriteMessage(mt, payload)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue