Files
gocryptotrader/exchanges/subscription/keys.go
Gareth Kirwan 33e82c170f Kraken: Subscription improvements (#1587)
* Convert: Fix TimeFromUnixTimestampDecimal using local

All parsed times should be in UTC

* Subscriptions: Add IgnoringAssetsKey

* Tests: Pass tb to curried WS handlers

* Websocket: Make ErrNoMessageListener a public error

* Kraken: Fix URLMap ignored for websocket URLs

* Kraken: Move SeedAssets from Setup to Bootstrap

Having SeedAssets in Setup is cruel and unusual because it calls the
API. Most other interactive data seeding happens in Bootstrap.

This made it so that fixing and creating unit tests for Kraken was
painfully slow, particularly on flaky internet.

* Kraken: Remove convert test

Duplicate of convert_test.go TestTimeFromUnixTimestampDecimal

* Kraken: Test config upgrades

* Kraken: Sub Channel improvements

* Use Websocket subscriptionChannels instead of local slice
* Remove ChannelID - Deprecated in docs
* Simplify ping handlers and hardcodes message
* Add Depth as configurable orderbook channel param
* Simplify auth/non-auth channel updates
* Add configurable Book depth
* Add configurable Candle timeframes

Kraken: Simplify all WS handlers with reqId

* Kraken: Subscription templating

* Generate N+ subs for pairs
If we generate one sub for all pairs, but then fan it out in the
responses, we end up with a mis-match between the sub store and
GenerateSubs, and when we do FlushChannels it will try to resub
everything again.

* Kraken: Rename channelName var throughout

Avoid shadowing func of same name

* Kraken: Add TestEnforceStandardChannelNames

* Websocket: Fix Resubscribe erroring Duplicate
2024-10-08 10:34:10 +11:00

125 lines
3.5 KiB
Go

package subscription
import (
"fmt"
"github.com/thrasher-corp/gocryptotrader/currency"
)
// MatchableKey interface should be implemented by Key types which want a more complex matching than a simple key equality check
// The Subscription method allows keys to compare against keys of other types
type MatchableKey interface {
Match(MatchableKey) bool
GetSubscription() *Subscription
String() string
}
// ExactKey is key type for subscriptions where all the pairs in a Subscription must match exactly
type ExactKey struct {
*Subscription
}
var _ MatchableKey = ExactKey{} // Enforce ExactKey must implement MatchableKey
// GetSubscription returns the underlying subscription
func (k ExactKey) GetSubscription() *Subscription {
return k.Subscription
}
// String implements Stringer; returns the Asset, Channel and Pairs
// Does not provide concurrency protection on the subscription it points to
func (k ExactKey) String() string {
s := k.Subscription
if s == nil {
return "Uninitialised ExactKey"
}
p := s.Pairs.Format(currency.PairFormat{Uppercase: true, Delimiter: "/"})
return fmt.Sprintf("%s %s %s", s.Channel, s.Asset, p.Join())
}
// Match implements MatchableKey
// Returns true if the key fields exactly matches the subscription, including all Pairs
// Does not check QualifiedChannel or Params
func (k ExactKey) Match(eachKey MatchableKey) bool {
if eachKey == nil {
return false
}
eachSub := eachKey.GetSubscription()
return eachSub != nil &&
eachSub.Channel == k.Channel &&
eachSub.Asset == k.Asset &&
eachSub.Pairs.Equal(k.Pairs) &&
eachSub.Levels == k.Levels &&
eachSub.Interval == k.Interval
}
// IgnoringPairsKey is a key type for finding subscriptions to group together for requests
type IgnoringPairsKey struct {
*Subscription
}
var _ MatchableKey = IgnoringPairsKey{} // Enforce IgnoringPairsKey must implement MatchableKey
// GetSubscription returns the underlying subscription
func (k IgnoringPairsKey) GetSubscription() *Subscription {
return k.Subscription
}
// String implements Stringer; returns the asset and Channel name but no pairs
func (k IgnoringPairsKey) String() string {
s := k.Subscription
if s == nil {
return "Uninitialised IgnoringPairsKey"
}
return fmt.Sprintf("%s %s", s.Channel, s.Asset)
}
// Match implements MatchableKey
func (k IgnoringPairsKey) Match(eachKey MatchableKey) bool {
if eachKey == nil {
return false
}
eachSub := eachKey.GetSubscription()
return eachSub != nil &&
eachSub.Channel == k.Channel &&
eachSub.Asset == k.Asset &&
eachSub.Levels == k.Levels &&
eachSub.Interval == k.Interval
}
// IgnoringAssetKey is a key type for finding subscriptions to group together for requests
type IgnoringAssetKey struct {
*Subscription
}
var _ MatchableKey = IgnoringAssetKey{} // Enforce IgnoringAssetKey must implement MatchableKey
// GetSubscription returns the underlying subscription
func (k IgnoringAssetKey) GetSubscription() *Subscription {
return k.Subscription
}
// String implements Stringer; returns the asset and Channel name but no pairs
func (k IgnoringAssetKey) String() string {
s := k.Subscription
if s == nil {
return "Uninitialised IgnoringAssetKey"
}
return fmt.Sprintf("%s %s", s.Channel, s.Pairs)
}
// Match implements MatchableKey
func (k IgnoringAssetKey) Match(eachKey MatchableKey) bool {
if eachKey == nil {
return false
}
eachSub := eachKey.GetSubscription()
return eachSub != nil &&
eachSub.Channel == k.Channel &&
eachSub.Pairs.Equal(k.Pairs) &&
eachSub.Levels == k.Levels &&
eachSub.Interval == k.Interval
}