Skip to content

Commit

Permalink
Websockets: Add channel.EnsureKeyed() for conv.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Sep 29, 2023
1 parent 7c715d3 commit 76e0d6d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
5 changes: 4 additions & 1 deletion exchanges/kraken/kraken_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,10 @@ func (k *Kraken) subscribeToChan(c *stream.ChannelSubscription) error {
c.Asset = asset.Spot
}

key := stream.DefaultChannelKey{c.Channel, c.Currency, c.Asset}
key, ok := c.EnsureKeyed().(stream.DefaultChannelKey)
if !ok {
return common.GetTypeAssertError("stream.DefaultChannelKey", c.Key, "subscription.Key") // Should be impossible
}

if c.Channel == krakenWsOrderbook {
if depth, err := depthFromChan(c); err != nil {
Expand Down
29 changes: 17 additions & 12 deletions exchanges/stream/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,15 +941,7 @@ func (w *Websocket) AddSuccessfulSubscriptions(channels ...ChannelSubscription)
w.subscriptions = subscriptionMap{}
}
for i := range channels {
key := channels[i].Key
if key == nil {
key = DefaultChannelKey{
Channel: channels[i].Channel,
Asset: channels[i].Asset,
Currency: channels[i].Currency,
}
channels[i].Key = key
}
key := channels[i].EnsureKeyed()
w.subscriptions[key] = channels[i]
}
w.subscriptionMutex.Unlock()
Expand All @@ -970,10 +962,23 @@ func (w *Websocket) RemoveSuccessfulUnsubscriptions(channels ...ChannelSubscript
}
}

// EnsureKeyed sets the default key on a channel if it doesn't have one
// Returns key for convenience
func (c *ChannelSubscription) EnsureKeyed() any {
if c.Key == nil {
c.Key = DefaultChannelKey{
Channel: c.Channel,
Asset: c.Asset,
Currency: c.Currency,
}
}
return c.Key
}

// Equal two WebsocketChannelSubscription to determine equality
func (w *ChannelSubscription) Equal(s *ChannelSubscription) bool {
return strings.EqualFold(w.Channel, s.Channel) &&
w.Currency.Equal(s.Currency)
func (c *ChannelSubscription) Equal(b *ChannelSubscription) bool {
return strings.EqualFold(c.Channel, b.Channel) &&
c.Currency.Equal(b.Currency)
}

// GetSubscription returns a pointer to a copy of the subscription at the key provided
Expand Down

0 comments on commit 76e0d6d

Please sign in to comment.