Skip to content

Commit

Permalink
Websockets: Add GetSubscription by key
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Sep 24, 2023
1 parent 7bb2043 commit 67a15e7
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions exchanges/stream/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var (
errSubscriptionNotFound = errors.New("subscription not found in list")
errAlreadySubscribed = errors.New("already subscribed")
errNoChannelsInArgs = errors.New("no channels in args")
errKeyCannotBeNil = errors.New("key cannot be nil")
)

var globalReporter Reporter
Expand Down Expand Up @@ -973,8 +974,22 @@ func (w *ChannelSubscription) Equal(s *ChannelSubscription) bool {
w.Currency.Equal(s.Currency)
}

// GetSubscriptions returns a copied list of subscriptions
// and is a private member that cannot be manipulated
// GetSubscription returns a pointer to a copy of the subscription at the key provided
// returns nil if no subscription is at that key or the key is nil
func (w *Websocket) GetSubscription(key any) *ChannelSubscription {
if key == nil || w.subscriptions == nil {
return nil
}
w.subscriptionMutex.RLock()
defer w.subscriptionMutex.RUnlock()
if s, ok := w.subscriptions[key]; ok {
c := s
return &c
}
return nil
}

// GetSubscriptions returns a new slice of the subscriptions
func (w *Websocket) GetSubscriptions() []ChannelSubscription {
w.subscriptionMutex.RLock()
defer w.subscriptionMutex.RUnlock()
Expand Down

0 comments on commit 67a15e7

Please sign in to comment.