Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 100 additions & 24 deletions pkg/kraken/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"net/http"
"sync"
Expand All @@ -14,6 +15,18 @@ import (
"github.com/krakenfx/api-go/v2/pkg/callback"
)

// ErrAlreadyConnected is returned by [WebSocket.Connect] when a connection is
// already established, or when another caller is in the middle of establishing
// one.
//
// Opening a second connection is worse than a no-op. The reader of the first
// one keeps running, both readers end up on whichever connection was stored
// last, and two readers on a single gorilla connection share its buffered
// reader. Their frames interleave, the connection fails with errors such as
// "RSV2 set" or "bad opcode", and the shared buffer can be corrupted badly
// enough to panic with a slice bounds error.
var ErrAlreadyConnected = errors.New("already connected")

// WebSocket implements a common structure for the WebSocket APIs.
type WebSocket struct {
Reconnect func()
Expand All @@ -25,11 +38,19 @@ type WebSocket struct {
OnSent *callback.Manager[*WebSocketMessage]
OnReceived *callback.Manager[*WebSocketMessage]

conn *websocket.Conn
URL string
writeMux sync.Mutex
Insecure bool
active bool

// mux guards the connection state below. It is never held while a callback
// runs or while the network is touched.
mux sync.Mutex
// connecting is held for the duration of a dial so that racing callers are
// turned away before they open a connection that all but one of them would
// immediately close.
connecting bool
conn *websocket.Conn
active bool
writeMux sync.Mutex
}

// NewWebSocket creates a new [WebSocket] object with default values.
Expand All @@ -43,7 +64,13 @@ func NewWebSocket() *WebSocket {
}
ws.Reconnect = func() {
for {
if err := ws.Connect(); err == nil {
err := ws.Connect()
if err == nil {
return
}
// Another caller has already restored the connection, which is the
// whole purpose of this loop. Retrying would open a second one.
if errors.Is(err, ErrAlreadyConnected) {
return
}
time.Sleep(ws.ReconnectWait)
Expand All @@ -58,26 +85,53 @@ func NewWebSocket() *WebSocket {
}

// Connect establishes a connection.
//
// It is safe for concurrent use and returns [ErrAlreadyConnected] instead of
// opening a second connection over a live one, so that a caller with its own
// reconnect logic on top of the built-in handler cannot end up with two readers
// on the same connection.
func (ws *WebSocket) Connect() error {
ws.mux.Lock()
if ws.active || ws.connecting {
ws.mux.Unlock()
return ErrAlreadyConnected
}
ws.connecting = true
insecure, url := ws.Insecure, ws.URL
ws.mux.Unlock()

dialer := &websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: 45 * time.Second,
}
if ws.Insecure {
if insecure {
dialer.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
return nil
},
}
}
connection, _, err := dialer.Dial(ws.URL, nil)
connection, _, err := dialer.Dial(url, nil)

ws.mux.Lock()
ws.connecting = false
if err != nil {
ws.mux.Unlock()
return fmt.Errorf("dial failed: %s", err)
}
// A reader may have failed and reconnected while this dial was in flight.
if ws.active {
ws.mux.Unlock()
_ = connection.Close()
return ErrAlreadyConnected
}
ws.conn = connection
ws.active = true
ws.DoReconnect = true
go ws.read()
ws.mux.Unlock()

go ws.read(connection)
ws.OnConnected.Call(nil)
return nil
}
Expand Down Expand Up @@ -125,33 +179,51 @@ func (m *WebSocketMessage) Map() (map[string]any, error) {
return dataMapped, nil
}

func (ws *WebSocket) read() {
ws.active = true
defer func() {
ws.active = false
}()
// read pumps a single connection. The connection is a parameter instead of a
// field read on every iteration, so that a reader can never drift onto a
// connection that replaced the one it was started for.
func (ws *WebSocket) read(conn *websocket.Conn) {
for {
_, data, err := ws.conn.ReadMessage()
_, data, err := conn.ReadMessage()
if err != nil {
_ = ws.conn.Close()
_ = conn.Close()
ws.mux.Lock()
// Only retire the connection if this reader still owns it. Clearing
// the flag here is what allows the handler below to reconnect.
if ws.conn == conn {
ws.active = false
}
ws.mux.Unlock()
ws.OnDisconnected.Call(err)
return
}
ws.OnReceived.Call(NewWebSocketMessage(data))
}
}

// Disconnect stops the connection.
// Disconnect stops the connection. It is a no-op when nothing is connected.
func (ws *WebSocket) Disconnect() error {
ws.mux.Lock()
ws.DoReconnect = false
done := make(chan bool)
defer close(done)
conn := ws.conn
ws.mux.Unlock()
if conn == nil {
return nil
}
// Buffered, and sent without blocking: the handler runs on the reader
// goroutine, and it must neither block on a channel nobody reads after the
// wait below times out, nor send on one that has been closed.
done := make(chan struct{}, 1)
cb := ws.OnDisconnected.Recurring(func(e *callback.Event[error]) {
select {
case done <- struct{}{}:
default:
}
})
defer ws.OnDisconnected.Deregister(cb)
defer func() {
_ = ws.conn.Close()
_ = conn.Close()
}()
callback := ws.OnDisconnected.Recurring(func(e *callback.Event[error]) {
done <- true
})
message := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")
if err := ws.WriteMessage(websocket.CloseMessage, message); err != nil {
return fmt.Errorf("write close failed: %s", err)
Expand All @@ -160,12 +232,13 @@ func (ws *WebSocket) Disconnect() error {
case <-done:
case <-time.After(time.Second):
}
ws.OnDisconnected.Deregister(callback)
return nil
}

// IsActive returns the status of the connection.
func (ws *WebSocket) IsActive() bool {
ws.mux.Lock()
defer ws.mux.Unlock()
return ws.active
}

Expand All @@ -183,12 +256,15 @@ func (ws *WebSocket) WriteJSON(message any) error {

// WriteMessage submits a raw message to the connection.
func (ws *WebSocket) WriteMessage(messageType int, data []byte) error {
if ws.conn == nil {
ws.mux.Lock()
conn := ws.conn
ws.mux.Unlock()
if conn == nil {
return fmt.Errorf("no connection")
}
ws.writeMux.Lock()
defer ws.writeMux.Unlock()
if err := ws.conn.WriteMessage(messageType, data); err != nil {
if err := conn.WriteMessage(messageType, data); err != nil {
return fmt.Errorf("write message failed: %s", err)
}
ws.OnSent.Call(NewWebSocketMessage(data))
Expand Down
Loading