Skip to content

Commit

Permalink
Web: Prevent concurrent writes on Websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
cschomburg committed May 21, 2017
1 parent 9f20005 commit 53f7623
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions services/web/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,24 @@ package web
import (
"encoding/json"
"net/http"
"sync"

"github.com/gorilla/websocket"
"github.com/sarifsystems/sarif/sarif"
)

type WebSocketConn struct {
conn *websocket.Conn
conn *websocket.Conn
mutex sync.Mutex
}

func (c *WebSocketConn) Write(msg sarif.Message) error {
if err := msg.IsValid(); err != nil {
return err
}

c.mutex.Lock()
defer c.mutex.Unlock()
w, err := c.conn.NextWriter(websocket.TextMessage)
if err != nil {
return err
Expand Down Expand Up @@ -55,7 +60,7 @@ func (s *Server) handleSocket(w http.ResponseWriter, r *http.Request) {
defer ws.Close()
s.Client.Log("info", "new websocket conn from "+r.RemoteAddr)

c := &WebSocketConn{ws}
c := &WebSocketConn{conn: ws}
err = s.Broker.AuthenticateAndListenOnConn(sarif.AuthChallenge, c)
s.Client.Log("info", "websocket from "+r.RemoteAddr+" closed: "+err.Error())
}

0 comments on commit 53f7623

Please sign in to comment.