Skip to content

Commit

Permalink
fixup! Websocket: Simplify Connecting/Connected state
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Feb 5, 2024
1 parent 33ab202 commit 75a9718
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions exchanges/stream/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (w *Websocket) Setup(s *WebsocketSetup) error {
return errWebsocketSetupIsNil
}

if w.state != uninitialised {
if w.IsInitialised() {
return fmt.Errorf("%s %w", w.exchangeName, errWebsocketAlreadyInitialised)
}

Expand Down Expand Up @@ -611,20 +611,27 @@ func (w *Websocket) trafficMonitor() {
}()
}

// IsInitialised returns whether the websocket has been Setup() already
func (w *Websocket) IsInitialised() bool {
w.fieldMutex.RLock()
defer w.fieldMutex.RUnlock()
return w.state != uninitialised
}

func (w *Websocket) setState(s state) {
w.fieldMutex.Lock()
w.state = s
w.fieldMutex.Unlock()
}

// IsConnected returns status of connection
// IsConnected returns whether the websocket is connected
func (w *Websocket) IsConnected() bool {
w.fieldMutex.RLock()
defer w.fieldMutex.RUnlock()
return w.state == connected
}

// IsConnecting returns status of connecting
// IsConnecting returns whether the websocket is connecting
func (w *Websocket) IsConnecting() bool {
w.fieldMutex.RLock()
defer w.fieldMutex.RUnlock()
Expand All @@ -637,7 +644,7 @@ func (w *Websocket) setEnabled(b bool) {
w.fieldMutex.Unlock()
}

// IsEnabled returns status of enabled
// IsEnabled returns whether the websocket is enabled
func (w *Websocket) IsEnabled() bool {
w.fieldMutex.RLock()
defer w.fieldMutex.RUnlock()
Expand Down

0 comments on commit 75a9718

Please sign in to comment.