Skip to content

Commit

Permalink
Websocket: Fix AddSubscription not seeing duplicates
Browse files Browse the repository at this point in the history
* Use getSubscription internally
* Also fixes us not creating fresh copies for calls to GetSubscription
  • Loading branch information
gbjk committed Feb 12, 2024
1 parent 9e89731 commit 1b0637b
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions exchanges/stream/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,8 +927,9 @@ func (w *Websocket) AddSubscription(c *subscription.Subscription) error {
if w.subscriptions == nil {
w.subscriptions = subscription.Map{}
}

key := c.EnsureKeyed()
if _, ok := w.subscriptions[key]; ok {
if s := w.getSubscription(key); s != nil {
return ErrSubscribedAlready
}

Expand Down Expand Up @@ -999,14 +1000,20 @@ func (w *Websocket) GetSubscription(key any) *subscription.Subscription {
}
w.subscriptionMutex.RLock()
defer w.subscriptionMutex.RUnlock()
s := w.getSubscription(key)
if s != nil {
s = &(*s)
}
return s
}

func (w *Websocket) getSubscription(key any) *subscription.Subscription {
if m, ok := key.(subscription.MatchableKey); ok {
return m.Match(w.subscriptions)
}

if s, ok := w.subscriptions[key]; ok {
c := *s
return &c
return s
}

return nil
Expand Down

0 comments on commit 1b0637b

Please sign in to comment.