Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
Fix data race by using socket-local websocket dialers.
  • Loading branch information
romshark committed Oct 19, 2018
1 parent 69d04cc commit 207ef3b
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions socketImpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type socket struct {
connected bool
lock sync.RWMutex
conn *websocket.Conn
tlsConfig *tls.Config
dialer websocket.Dialer
}

// newConnectedSocket creates a new gorilla/websocket based socket instance
Expand All @@ -86,16 +86,18 @@ func newConnectedSocket(conn *websocket.Conn) Socket {

// NewSocket creates a new disconnected gorilla/websocket based socket instance
func NewSocket(tlsConfig *tls.Config) Socket {
connected := false

if tlsConfig == nil {
tlsConfig = &tls.Config{}
}

return &socket{
connected: connected,
connected: false,
lock: sync.RWMutex{},
tlsConfig: tlsConfig,
dialer: websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: 45 * time.Second,
TLSClientConfig: tlsConfig.Clone(),
},
}
}

Expand All @@ -108,8 +110,7 @@ func (sock *socket) Dial(serverAddr url.URL) (err error) {
sock.conn = nil
}

websocket.DefaultDialer.TLSClientConfig = sock.tlsConfig.Clone()
sock.conn, _, err = websocket.DefaultDialer.Dial(serverAddr.String(), nil)
sock.conn, _, err = sock.dialer.Dial(serverAddr.String(), nil)
if err != nil {
return NewDisconnectedErr(fmt.Errorf("Dial failure: %s", err))
}
Expand Down

0 comments on commit 207ef3b

Please sign in to comment.