From c6da9c1b7805f191f55219826dddd6144896b05f Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Sat, 17 Feb 2024 18:01:32 +0700 Subject: [PATCH] Subscription: Fix Match --- exchanges/stream/websocket.go | 7 ++++++- exchanges/subscription/key.go | 1 - exchanges/subscription/store.go | 15 +++++++++------ exchanges/subscription/subscription.go | 8 +++++--- 4 files changed, 20 insertions(+), 11 deletions(-) delete mode 100644 exchanges/subscription/key.go diff --git a/exchanges/stream/websocket.go b/exchanges/stream/websocket.go index 56c229cb99d..5fa1edb1356 100644 --- a/exchanges/stream/websocket.go +++ b/exchanges/stream/websocket.go @@ -872,7 +872,12 @@ func (w *Websocket) UnsubscribeChannels(channels subscription.List) error { return fmt.Errorf("%s websocket: %w", w.exchangeName, errNoSubscriptionsSupplied) } if w.subscriptions == nil { - return nil + return common.ErrNilPointer + } + for _, s := range channels { + if w.subscriptions.Get(s) == nil { + return fmt.Errorf("%s websocket: %w: %s", w.exchangeName, ErrSubscriptionNotFound, s) + } } return w.Unsubscriber(channels) } diff --git a/exchanges/subscription/key.go b/exchanges/subscription/key.go deleted file mode 100644 index 0e1dae29986..00000000000 --- a/exchanges/subscription/key.go +++ /dev/null @@ -1 +0,0 @@ -package subscription diff --git a/exchanges/subscription/store.go b/exchanges/subscription/store.go index 365852b1718..8e833c6fd65 100644 --- a/exchanges/subscription/store.go +++ b/exchanges/subscription/store.go @@ -51,16 +51,19 @@ func (s *Store) Get(key any) *Subscription { } // get returns a pointer to subscription or nil if not found -// If key implements MatchableKey then key.Match will be used -// If key is actually a subscription then ensureKeyed will be used and we'll return the +// If the key passed in is a Subscription then its Key will be used; which may be a pointer to itself. +// If key implements MatchableKey then key.Match will be used; Note that *Subscription implements MatchableKey // This method provides no locking protection -// returned subscriptions are implicitly guaranteed to have a key +// returned subscriptions are implicitly guaranteed to have a Key func (s *Store) get(key any) *Subscription { switch v := key.(type) { - case *Subscription: - return s.get(v.EnsureKeyed()) case Subscription: - return s.get(v.EnsureKeyed()) + key = v.EnsureKeyed() + case *Subscription: + key = v.EnsureKeyed() + } + + switch v := key.(type) { case MatchableKey: return s.match(v) default: diff --git a/exchanges/subscription/subscription.go b/exchanges/subscription/subscription.go index 0be695a33a4..cdcbdc1c5fe 100644 --- a/exchanges/subscription/subscription.go +++ b/exchanges/subscription/subscription.go @@ -93,7 +93,7 @@ func (s *Subscription) EnsureKeyed() any { } // Match returns if the two keys match Channels, Assets, Pairs, Interval and Levels: -// Pairs comparison: +// Key Pairs comparison: // 1) Empty pairs then only Subscriptions without pairs match // 2) >=1 pairs then Subscriptions which contain all the pairs match // Such that a subscription for all enabled pairs will be matched when seaching for any one pair @@ -103,8 +103,10 @@ func (s *Subscription) Match(key any) bool { case !ok, s.Channel != b.Channel, s.Asset != b.Asset, - len(s.Pairs) == 0 && len(b.Pairs) != 0, - s.Pairs.ContainsAll(b.Pairs, true) != nil, + len(b.Pairs) == 0 && len(s.Pairs) != 0, + // len(b.Pairs) == 0 && len(s.Pairs) == 0: Okay; continue to next non-pairs check + len(b.Pairs) != 0 && len(s.Pairs) == 0, + len(b.Pairs) != 0 && s.Pairs.ContainsAll(b.Pairs, true) != nil, s.Levels != b.Levels, s.Interval != b.Interval: return false