mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-04 23:16:54 +00:00
Bitfinex: Websocket subscription improvements (#1353)
* Websockets: Add keys to websocket subscriptions * This switches all RO uses of the mutex to use a RLock method. * The mutex used for discrete field access has had scope drift from name 'connectionMutex' so rename to more appropriate fieldsMutex * The mutex used for Set/CanUseAuthEndpoints moves from the subscriptions endpoint to the fieldsMutex * Add GetSubscription by key * Expose stream.Matcher type * Bitfinex: Subscribe and Unsubscribe atomicly * Fix Auth failures ignored * This change makes it so that Subscribe and Unsubscribe wait for success ** Tells the DataHandler about errors ** Errors are returned to consumers * Subscribes concurrently to the channels * It also simplifies the chanId to stream mapping * Removes unable to locate chanID: %d errors which are just noise * Paves the way for unified channelSubscription id handling * Adds support for subId for Book subscriptions, which is more robust * Vastly simplifies what we need to test TestWsSubscribedResponse This test was working to ensure that the various fancy key parsing mechanisms all worked. Now that we use subId, we just need a thorough test of that * Expose Match.Set in order to capture websocket incoming data Can't see another way of doing this. Doesn't seem too bad * Allow tests to run with auth or WS These flags made it difficult to run the tests whilst working on websockets * Enable API auth and WS in testconfig This change minimises the changes requires for a full test run against live endpoints, so that new contributors have a clearer testing path. I cannot see any reason to turn WS off and Auth endpoints off when we're not going to run API tests without Creds being set, and we're not going to do live fire tests without canManipulateRealOrders * TestWsSubscribe and various fixes ** Enables the websocket for live non-authed integration tests by default ** Adds an integration test for subscriptions ** Changes the Ws tests to respect canManipulateRealOrders ** Uses WsConnect instead of setupWs; fixes seqNo config not sent for WS tests ** Allows api creds to live in config/testdata.json which might be less likely to accidentally commit, and less obtrusive * Bitfinex: Support period and timeframe for Candles * Fixes manual Subscribe() symbol or key formatting * Unifies handling of params for DefaultSubscriptions and manual subsrciptions * Bitfinex: Handle conf and info WS channel events * Bitfinex: Better tests for subscriptions * fixup! Websockets: Add keys to websocket subscriptions * fixup! Bitfinex: Subscribe and Unsubscribe atomicly * fixup! Websockets: Add keys to websocket subscriptions * Websockets: Add Pending subscription status Add a status tracker so that Sub/Unsub can prevent duplicates, and also fixes when first message comes before we have added the sub to the tracker * Websockets: Add State instead of pending This change allows more clarity about the current state and checks for specifically already Unsubing * Bitfinex: Fix first sub message maybe lost The only link we have between a sub req and the sub resp is the subID. And the only link we have between a sub message and the sub is the chanID. We can't derive a link using Pair or anything else. This meant that by sending the resp and its chanID down the IncomingData channel, we allowed the channel reader to maybe process the next message, the first message on the channel, before the runtime executed the switch back to subscribeToChan waiting on the chan. To fix this, we key initially on subId.(string), and then replace it with chanId.(int64) when we have it *inside* the wsHandleData so we know we've procedurally handled it before the next message. subscribeToChan is then free to remove the subId keyed Sub regardless of error or not If there's an error, we don't need to inline handling because there won't be any second update. Expands test coverage to make sure those subId keyed subscriptions are removed. * Websocket: Validate state in SetChanState * fixup! Bitfinex: Fix first sub message maybe lost * Websockets: Rename RemoveUnsuccessfulSubs Implementation doesn't imply Unsuccessful or need to. This change supports the registering of Pending subs * Bitfinex: Fix race in Tests
This commit is contained in:
@@ -21,7 +21,6 @@ import (
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
|
||||
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
|
||||
)
|
||||
|
||||
@@ -101,12 +100,16 @@ const (
|
||||
|
||||
bitfinexChecksumFlag = 131072
|
||||
bitfinexWsSequenceFlag = 65536
|
||||
|
||||
// CandlesTimeframeKey configures the timeframe in stream.ChannelSubscription.Params
|
||||
CandlesTimeframeKey = "_timeframe"
|
||||
// CandlesPeriodKey configures the aggregated period in stream.ChannelSubscription.Params
|
||||
CandlesPeriodKey = "_period"
|
||||
)
|
||||
|
||||
// Bitfinex is the overarching type across the bitfinex package
|
||||
type Bitfinex struct {
|
||||
exchange.Base
|
||||
WebsocketSubdChannels map[int]*stream.ChannelSubscription
|
||||
}
|
||||
|
||||
// GetPlatformStatus returns the Bifinex platform status
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -12,9 +12,11 @@ import (
|
||||
|
||||
var (
|
||||
errSetCannotBeEmpty = errors.New("set cannot be empty")
|
||||
errSubNotFound = errors.New("could not find matching subscription")
|
||||
errTypeAssert = errors.New("type assertion failed")
|
||||
errNoSeqNo = errors.New("no sequence number")
|
||||
errUnknownError = errors.New("unknown error")
|
||||
errParamNotAllowed = errors.New("param not allowed")
|
||||
errParsingWSField = errors.New("error parsing WS field")
|
||||
)
|
||||
|
||||
// AccountV2Data stores account v2 data
|
||||
@@ -659,6 +661,12 @@ const (
|
||||
wsTicker = "ticker"
|
||||
wsTrades = "trades"
|
||||
wsError = "error"
|
||||
wsEventSubscribed = "subscribed"
|
||||
wsEventUnsubscribed = "unsubscribed"
|
||||
wsEventAuth = "auth"
|
||||
wsEventError = "error"
|
||||
wsEventConf = "conf"
|
||||
wsEventInfo = "info"
|
||||
)
|
||||
|
||||
// WsAuthRequest container for WS auth request
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/buger/jsonparser"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/thrasher-corp/gocryptotrader/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/common/convert"
|
||||
@@ -132,76 +133,31 @@ func (b *Bitfinex) wsHandleData(respRaw []byte) error {
|
||||
}
|
||||
switch d := result.(type) {
|
||||
case map[string]interface{}:
|
||||
event := d["event"]
|
||||
switch event {
|
||||
case "subscribed":
|
||||
chanID, ok := d["chanId"].(float64)
|
||||
if !ok {
|
||||
return errors.New("unable to type assert chanId")
|
||||
}
|
||||
channel, ok := d["channel"].(string)
|
||||
if !ok {
|
||||
return errors.New("unable to type assert channel")
|
||||
}
|
||||
symbol, ok := d["symbol"].(string)
|
||||
if !ok {
|
||||
key, ok := d["key"].(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("subscribed to channel but no symbol or key: %v", channel)
|
||||
}
|
||||
if channel != wsCandles {
|
||||
// status channel not implemented at all yet.
|
||||
return fmt.Errorf("%v channel subscription keys: %w", channel, common.ErrNotYetImplemented)
|
||||
}
|
||||
var err error
|
||||
symbol, err = symbolFromCandleKey(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := b.WsAddSubscriptionChannel(int(chanID), channel, symbol); err != nil {
|
||||
return err
|
||||
}
|
||||
case "unsubscribed":
|
||||
chanID, ok := d["chanId"].(float64)
|
||||
if !ok {
|
||||
return errors.New("unable to type assert chanId")
|
||||
}
|
||||
delete(b.WebsocketSubdChannels, int(chanID))
|
||||
case "auth":
|
||||
status, ok := d["status"].(string)
|
||||
if !ok {
|
||||
return errors.New("unable to type assert status")
|
||||
}
|
||||
if status == "OK" {
|
||||
b.Websocket.DataHandler <- d
|
||||
} else if status == "fail" {
|
||||
if code, ok := d["code"].(string); ok {
|
||||
return fmt.Errorf("websocket unable to AUTH. Error code: %s",
|
||||
code)
|
||||
}
|
||||
return errors.New("websocket unable to auth")
|
||||
}
|
||||
}
|
||||
return b.handleWSEvent(respRaw)
|
||||
case []interface{}:
|
||||
var chanID int
|
||||
if f, ok := d[0].(float64); !ok {
|
||||
chanIDFloat, ok := d[0].(float64)
|
||||
if !ok {
|
||||
return common.GetTypeAssertError("float64", d[0], "chanID")
|
||||
} else { //nolint:revive // using lexical variable requires else statement
|
||||
chanID = int(f)
|
||||
}
|
||||
chanID := int(chanIDFloat)
|
||||
|
||||
eventType, hasEventType := d[1].(string)
|
||||
|
||||
if chanID != 0 {
|
||||
if c, ok := b.WebsocketSubdChannels[chanID]; ok {
|
||||
return b.handleWSChannelUpdate(c, chanID, eventType, d)
|
||||
if c := b.Websocket.GetSubscription(chanID); c != nil {
|
||||
return b.handleWSChannelUpdate(c, eventType, d)
|
||||
}
|
||||
return fmt.Errorf("unable to locate chanID: %d", chanID)
|
||||
if b.Verbose {
|
||||
log.Warnf(log.ExchangeSys, "%s %s; dropped WS message: %s", b.Name, stream.ErrSubscriptionNotFound, respRaw)
|
||||
}
|
||||
// We didn't have a mapping for this chanID; This probably means we have unsubscribed OR
|
||||
// received our first message before processing the sub chanID
|
||||
// In either case it's okay. No point in erroring because there's nothing we can do about it, and it happens often
|
||||
return nil
|
||||
}
|
||||
|
||||
if !hasEventType {
|
||||
return errors.New("WS message without eventType or chanID")
|
||||
return errors.New("WS message without eventType")
|
||||
}
|
||||
|
||||
switch eventType {
|
||||
@@ -469,9 +425,107 @@ func (b *Bitfinex) wsHandleData(respRaw []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bitfinex) handleWSChannelUpdate(c *stream.ChannelSubscription, chanID int, eventType string, d []interface{}) error {
|
||||
func (b *Bitfinex) handleWSEvent(respRaw []byte) error {
|
||||
event, err := jsonparser.GetUnsafeString(respRaw, "event")
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w 'event': %w from message: %s", errParsingWSField, err, respRaw)
|
||||
}
|
||||
switch event {
|
||||
case wsEventSubscribed:
|
||||
return b.handleWSSubscribed(respRaw)
|
||||
case wsEventUnsubscribed:
|
||||
chanID, err := jsonparser.GetUnsafeString(respRaw, "chanId")
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w 'chanId': %w from message: %s", errParsingWSField, err, respRaw)
|
||||
}
|
||||
if !b.Websocket.Match.IncomingWithData("unsubscribe:"+chanID, respRaw) {
|
||||
return fmt.Errorf("%v channel unsubscribe listener not found", chanID)
|
||||
}
|
||||
case wsEventError:
|
||||
if subID, err := jsonparser.GetUnsafeString(respRaw, "subId"); err == nil {
|
||||
if !b.Websocket.Match.IncomingWithData("subscribe:"+subID, respRaw) {
|
||||
return fmt.Errorf("%v channel subscribe listener not found", subID)
|
||||
}
|
||||
} else if chanID, err := jsonparser.GetUnsafeString(respRaw, "chanId"); err == nil {
|
||||
if !b.Websocket.Match.IncomingWithData("unsubscribe:"+chanID, respRaw) {
|
||||
return fmt.Errorf("%v channel unsubscribe listener not found", chanID)
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("unknown channel error; Message: %s", respRaw)
|
||||
}
|
||||
case wsEventAuth:
|
||||
status, err := jsonparser.GetUnsafeString(respRaw, "status")
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w 'status': %w from message: %s", errParsingWSField, err, respRaw)
|
||||
}
|
||||
if status == "OK" {
|
||||
var glob map[string]interface{}
|
||||
if err := json.Unmarshal(respRaw, &glob); err != nil {
|
||||
return fmt.Errorf("unable to Unmarshal auth resp; Error: %w Msg: %v", err, respRaw)
|
||||
}
|
||||
// TODO - Send a better value down the channel
|
||||
b.Websocket.DataHandler <- glob
|
||||
} else {
|
||||
errCode, err := jsonparser.GetInt(respRaw, "code")
|
||||
if err != nil {
|
||||
log.Errorf(log.ExchangeSys, "%s %s 'code': %s from message: %s", b.Name, errParsingWSField, err, respRaw)
|
||||
}
|
||||
return fmt.Errorf("WS auth subscription error; Status: %s Error Code: %d", status, errCode)
|
||||
}
|
||||
case wsEventInfo:
|
||||
// Nothing to do with info for now.
|
||||
// version or platform.status might be useful in the future.
|
||||
case wsEventConf:
|
||||
status, err := jsonparser.GetUnsafeString(respRaw, "status")
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w 'status': %w from message: %s", errParsingWSField, err, respRaw)
|
||||
}
|
||||
if status != "OK" {
|
||||
return fmt.Errorf("WS configure channel error; Status: %s", status)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("unknown WS event msg: %s", respRaw)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleWSSubscribed parses a subscription response and registers the chanID key immediately, before updating subscribeToChan via IncomingWithData chan
|
||||
// wsHandleData happens sequentially, so by rekeying on chanID immediately we ensure the first message is not dropped
|
||||
func (b *Bitfinex) handleWSSubscribed(respRaw []byte) error {
|
||||
subID, err := jsonparser.GetUnsafeString(respRaw, "subId")
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w 'subId': %w from message: %s", errParsingWSField, err, respRaw)
|
||||
}
|
||||
|
||||
c := b.Websocket.GetSubscription(subID)
|
||||
if c == nil {
|
||||
return fmt.Errorf("%w: %w subID: %s", stream.ErrSubscriptionFailure, stream.ErrSubscriptionNotFound, subID)
|
||||
}
|
||||
|
||||
chanID, err := jsonparser.GetInt(respRaw, "chanId")
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %w 'chanId': %w; Channel: %s Pair: %s", stream.ErrSubscriptionFailure, errParsingWSField, err, c.Channel, c.Currency)
|
||||
}
|
||||
|
||||
// Note: chanID's int type avoids conflicts with the string type subID key because of the type difference
|
||||
c.Key = int(chanID)
|
||||
|
||||
// subscribeToChan removes the old subID keyed Subscription
|
||||
b.Websocket.AddSuccessfulSubscriptions(*c)
|
||||
|
||||
if b.Verbose {
|
||||
log.Debugf(log.ExchangeSys, "%s Subscribed to Channel: %s Pair: %s ChannelID: %d\n", b.Name, c.Channel, c.Currency, chanID)
|
||||
}
|
||||
if !b.Websocket.Match.IncomingWithData("subscribe:"+subID, respRaw) {
|
||||
return fmt.Errorf("%v channel subscribe listener not found", subID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bitfinex) handleWSChannelUpdate(c *stream.ChannelSubscription, eventType string, d []interface{}) error {
|
||||
if eventType == wsChecksum {
|
||||
return b.handleWSChecksum(chanID, d)
|
||||
return b.handleWSChecksum(c, d)
|
||||
}
|
||||
|
||||
if eventType == wsHeartbeat {
|
||||
@@ -480,7 +534,7 @@ func (b *Bitfinex) handleWSChannelUpdate(c *stream.ChannelSubscription, chanID i
|
||||
|
||||
switch c.Channel {
|
||||
case wsBook:
|
||||
return b.handleWSBookUpdate(c, chanID, d)
|
||||
return b.handleWSBookUpdate(c, d)
|
||||
case wsCandles:
|
||||
return b.handleWSCandleUpdate(c, d)
|
||||
case wsTicker:
|
||||
@@ -492,7 +546,7 @@ func (b *Bitfinex) handleWSChannelUpdate(c *stream.ChannelSubscription, chanID i
|
||||
return fmt.Errorf("%s unhandled channel update: %s", b.Name, c.Channel)
|
||||
}
|
||||
|
||||
func (b *Bitfinex) handleWSChecksum(chanID int, d []interface{}) error {
|
||||
func (b *Bitfinex) handleWSChecksum(c *stream.ChannelSubscription, d []interface{}) error {
|
||||
var token int
|
||||
if f, ok := d[2].(float64); !ok {
|
||||
return common.GetTypeAssertError("float64", d[2], "checksum")
|
||||
@@ -509,6 +563,11 @@ func (b *Bitfinex) handleWSChecksum(chanID int, d []interface{}) error {
|
||||
seqNo = int64(f)
|
||||
}
|
||||
|
||||
chanID, ok := c.Key.(int)
|
||||
if !ok {
|
||||
return common.GetTypeAssertError("int", c.Key, "ChanID") // Should be impossible
|
||||
}
|
||||
|
||||
cMtx.Lock()
|
||||
checksumStore[chanID] = &checksum{
|
||||
Token: token,
|
||||
@@ -518,7 +577,7 @@ func (b *Bitfinex) handleWSChecksum(chanID int, d []interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bitfinex) handleWSBookUpdate(c *stream.ChannelSubscription, chanID int, d []interface{}) error {
|
||||
func (b *Bitfinex) handleWSBookUpdate(c *stream.ChannelSubscription, d []interface{}) error {
|
||||
var newOrderbook []WebsocketBook
|
||||
obSnapBundle, ok := d[1].([]interface{})
|
||||
if !ok {
|
||||
@@ -604,7 +663,7 @@ func (b *Bitfinex) handleWSBookUpdate(c *stream.ChannelSubscription, chanID int,
|
||||
Amount: amountRate})
|
||||
}
|
||||
|
||||
if err := b.WsUpdateOrderbook(c.Currency, c.Asset, newOrderbook, chanID, int64(sequenceNo), fundingRate); err != nil {
|
||||
if err := b.WsUpdateOrderbook(c, c.Currency, c.Asset, newOrderbook, int64(sequenceNo), fundingRate); err != nil {
|
||||
return fmt.Errorf("updating orderbook error: %s",
|
||||
err)
|
||||
}
|
||||
@@ -1405,8 +1464,7 @@ func (b *Bitfinex) wsHandleOrder(data []interface{}) {
|
||||
b.Websocket.DataHandler <- &od
|
||||
}
|
||||
|
||||
// WsInsertSnapshot add the initial orderbook snapshot when subscribed to a
|
||||
// channel
|
||||
// WsInsertSnapshot add the initial orderbook snapshot when subscribed to a channel
|
||||
func (b *Bitfinex) WsInsertSnapshot(p currency.Pair, assetType asset.Item, books []WebsocketBook, fundingRate bool) error {
|
||||
if len(books) == 0 {
|
||||
return errors.New("no orderbooks submitted")
|
||||
@@ -1450,7 +1508,7 @@ func (b *Bitfinex) WsInsertSnapshot(p currency.Pair, assetType asset.Item, books
|
||||
|
||||
// WsUpdateOrderbook updates the orderbook list, removing and adding to the
|
||||
// orderbook sides
|
||||
func (b *Bitfinex) WsUpdateOrderbook(p currency.Pair, assetType asset.Item, book []WebsocketBook, channelID int, sequenceNo int64, fundingRate bool) error {
|
||||
func (b *Bitfinex) WsUpdateOrderbook(c *stream.ChannelSubscription, p currency.Pair, assetType asset.Item, book []WebsocketBook, sequenceNo int64, fundingRate bool) error {
|
||||
orderbookUpdate := orderbook.Update{
|
||||
Asset: assetType,
|
||||
Pair: p,
|
||||
@@ -1506,13 +1564,18 @@ func (b *Bitfinex) WsUpdateOrderbook(p currency.Pair, assetType asset.Item, book
|
||||
}
|
||||
}
|
||||
|
||||
chanID, ok := c.Key.(int)
|
||||
if !ok {
|
||||
return common.GetTypeAssertError("int", c.Key, "ChanID") // Should be impossible
|
||||
}
|
||||
|
||||
cMtx.Lock()
|
||||
checkme := checksumStore[channelID]
|
||||
checkme := checksumStore[chanID]
|
||||
if checkme == nil {
|
||||
cMtx.Unlock()
|
||||
return b.Websocket.Orderbook.Update(&orderbookUpdate)
|
||||
}
|
||||
checksumStore[channelID] = nil
|
||||
checksumStore[chanID] = nil
|
||||
cMtx.Unlock()
|
||||
|
||||
if checkme.Sequence+1 == sequenceNo {
|
||||
@@ -1528,9 +1591,7 @@ func (b *Bitfinex) WsUpdateOrderbook(p currency.Pair, assetType asset.Item, book
|
||||
|
||||
if err = validateCRC32(ob, checkme.Token); err != nil {
|
||||
log.Errorf(log.WebsocketMgr, "%s websocket orderbook update error, will resubscribe orderbook: %v", b.Name, err)
|
||||
if suberr := b.resubOrderbook(p, assetType); suberr != nil {
|
||||
log.Errorf(log.ExchangeSys, "%s error resubscribing orderbook: %v", b.Name, suberr)
|
||||
}
|
||||
b.resubOrderbook(c)
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -1540,37 +1601,22 @@ func (b *Bitfinex) WsUpdateOrderbook(p currency.Pair, assetType asset.Item, book
|
||||
|
||||
// resubOrderbook resubscribes the orderbook after a consistency error, probably a failed checksum,
|
||||
// which forces a fresh snapshot. If we don't do this the orderbook will keep erroring and drifting.
|
||||
func (b *Bitfinex) resubOrderbook(p currency.Pair, assetType asset.Item) error {
|
||||
if err := b.Websocket.Orderbook.FlushOrderbook(p, assetType); err != nil {
|
||||
return err
|
||||
// Flushing the orderbook happens immediately, but the ReSub itself is a go routine to avoid blocking the WS data channel
|
||||
func (b *Bitfinex) resubOrderbook(c *stream.ChannelSubscription) {
|
||||
if err := b.Websocket.Orderbook.FlushOrderbook(c.Currency, c.Asset); err != nil {
|
||||
log.Errorf(log.ExchangeSys, "%s error flushing orderbook: %v", b.Name, err)
|
||||
}
|
||||
|
||||
c, err := b.chanForSub(wsBook, assetType, p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return b.Websocket.ResubscribeToChannel(c)
|
||||
}
|
||||
|
||||
// chanForSub returns an existing channel subscription for a given channel/asset/pair
|
||||
func (b *Bitfinex) chanForSub(cName string, assetType asset.Item, pair currency.Pair) (*stream.ChannelSubscription, error) {
|
||||
want := &stream.ChannelSubscription{
|
||||
Channel: cName,
|
||||
Currency: pair,
|
||||
Asset: assetType,
|
||||
}
|
||||
subs := b.Websocket.GetSubscriptions()
|
||||
for i := range subs {
|
||||
if subs[i].Equal(want) {
|
||||
return &subs[i], nil
|
||||
// Resub will block so we have to do this in a goro
|
||||
go func() {
|
||||
if err := b.Websocket.ResubscribeToChannel(c); err != nil {
|
||||
log.Errorf(log.ExchangeSys, "%s error resubscribing orderbook: %v", b.Name, err)
|
||||
}
|
||||
}
|
||||
return nil, errSubNotFound
|
||||
}()
|
||||
}
|
||||
|
||||
// GenerateDefaultSubscriptions Adds default subscriptions to websocket to be handled by ManageSubscriptions()
|
||||
func (b *Bitfinex) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription, error) {
|
||||
var wsPairFormat = currency.PairFormat{Uppercase: true}
|
||||
var channels = []string{wsBook, wsTrades, wsTicker, wsCandles}
|
||||
|
||||
var subscriptions []stream.ChannelSubscription
|
||||
@@ -1592,29 +1638,8 @@ func (b *Bitfinex) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription,
|
||||
params["len"] = "100"
|
||||
}
|
||||
|
||||
prefix := "t"
|
||||
if assets[i] == asset.MarginFunding {
|
||||
prefix = "f"
|
||||
}
|
||||
|
||||
needsDelimiter := enabledPairs[k].Len() > 6
|
||||
|
||||
var formattedPair string
|
||||
if needsDelimiter {
|
||||
formattedPair = enabledPairs[k].Format(currency.PairFormat{Uppercase: true, Delimiter: ":"}).String()
|
||||
} else {
|
||||
formattedPair = wsPairFormat.Format(enabledPairs[k])
|
||||
}
|
||||
|
||||
if channels[j] == wsCandles {
|
||||
// TODO: Add ability to select timescale && funding period
|
||||
fundingPeriod := ""
|
||||
if assets[i] == asset.MarginFunding {
|
||||
fundingPeriod = ":p30"
|
||||
}
|
||||
params["key"] = "trade:1m:" + prefix + formattedPair + fundingPeriod
|
||||
} else {
|
||||
params["symbol"] = prefix + formattedPair
|
||||
if channels[j] == wsCandles && assets[i] == asset.MarginFunding {
|
||||
params[CandlesPeriodKey] = "30"
|
||||
}
|
||||
|
||||
subscriptions = append(subscriptions, stream.ChannelSubscription{
|
||||
@@ -1630,31 +1655,6 @@ func (b *Bitfinex) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription,
|
||||
return subscriptions, nil
|
||||
}
|
||||
|
||||
// Subscribe sends a websocket message to receive data from the channel
|
||||
func (b *Bitfinex) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error {
|
||||
var errs error
|
||||
for i := range channelsToSubscribe {
|
||||
req := make(map[string]interface{})
|
||||
req["event"] = "subscribe"
|
||||
req["channel"] = channelsToSubscribe[i].Channel
|
||||
|
||||
for k, v := range channelsToSubscribe[i].Params {
|
||||
// Resubscribing channels might already have this set
|
||||
if k != "chanId" {
|
||||
req[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
err := b.Websocket.Conn.SendJSONMessage(req)
|
||||
if err != nil {
|
||||
errs = common.AppendError(errs, err)
|
||||
continue
|
||||
}
|
||||
b.Websocket.AddSuccessfulSubscriptions(channelsToSubscribe[i])
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
// ConfigureWS to send checksums and sequence numbers
|
||||
func (b *Bitfinex) ConfigureWS() error {
|
||||
return b.Websocket.Conn.SendJSONMessage(map[string]interface{}{
|
||||
@@ -1663,37 +1663,193 @@ func (b *Bitfinex) ConfigureWS() error {
|
||||
})
|
||||
}
|
||||
|
||||
// Unsubscribe sends a websocket message to stop receiving data from the channel
|
||||
func (b *Bitfinex) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription) error {
|
||||
var errs error
|
||||
for i := range channelsToUnsubscribe {
|
||||
idAny, ok := channelsToUnsubscribe[i].Params["chanId"]
|
||||
if !ok {
|
||||
errs = common.AppendError(errs, fmt.Errorf("cannot unsubscribe from a channel without an id"))
|
||||
continue
|
||||
}
|
||||
chanID, ok := idAny.(int)
|
||||
if !ok {
|
||||
errs = common.AppendError(errs, fmt.Errorf("chanId is not an int"))
|
||||
continue
|
||||
}
|
||||
// Subscribe sends a websocket message to receive data from channels
|
||||
func (b *Bitfinex) Subscribe(channels []stream.ChannelSubscription) error {
|
||||
return b.parallelChanOp(channels, b.subscribeToChan)
|
||||
}
|
||||
|
||||
req := map[string]interface{}{
|
||||
"event": "unsubscribe",
|
||||
"chanId": chanID,
|
||||
}
|
||||
// Unsubscribe sends a websocket message to stop receiving data from channels
|
||||
func (b *Bitfinex) Unsubscribe(channels []stream.ChannelSubscription) error {
|
||||
return b.parallelChanOp(channels, b.unsubscribeFromChan)
|
||||
}
|
||||
|
||||
err := b.Websocket.Conn.SendJSONMessage(req)
|
||||
if err != nil {
|
||||
errs = common.AppendError(errs, err)
|
||||
continue
|
||||
}
|
||||
// We do this before the unsubscribed event comes back so we can subscribe again when called from ResubcribeToChannel
|
||||
b.Websocket.RemoveSuccessfulUnsubscriptions(channelsToUnsubscribe[i])
|
||||
// parallelChanOp performs a single method call in parallel across streams and waits to return any errors
|
||||
func (b *Bitfinex) parallelChanOp(channels []stream.ChannelSubscription, m func(*stream.ChannelSubscription) error) error {
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(len(channels))
|
||||
errC := make(chan error, len(channels))
|
||||
|
||||
for i := range channels {
|
||||
go func(c *stream.ChannelSubscription) {
|
||||
defer wg.Done()
|
||||
if err := m(c); err != nil {
|
||||
errC <- err
|
||||
}
|
||||
}(&channels[i])
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
close(errC)
|
||||
|
||||
var errs error
|
||||
for err := range errC {
|
||||
errs = common.AppendError(errs, err)
|
||||
}
|
||||
|
||||
return errs
|
||||
}
|
||||
|
||||
// subscribeToChan handles a single subscription and parses the result
|
||||
// on success it adds the subscription to the websocket
|
||||
func (b *Bitfinex) subscribeToChan(c *stream.ChannelSubscription) error {
|
||||
req, err := subscribeReq(c)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %w; Channel: %s Pair: %s", stream.ErrSubscriptionFailure, err, c.Channel, c.Currency)
|
||||
}
|
||||
|
||||
// subId is a single round-trip identifier that provides linking sub requests to chanIDs
|
||||
// Although docs only mention subId for wsBook, it works for all chans
|
||||
subID := strconv.FormatInt(b.Websocket.Conn.GenerateMessageID(false), 10)
|
||||
req["subId"] = subID
|
||||
|
||||
// Add a temporary Key so we can find this Sub when we get the resp without delay or context switch
|
||||
// Otherwise we might drop the first messages after the subscribed resp
|
||||
c.Key = subID // Note subID string type avoids conflicts with later chanID key
|
||||
|
||||
c.State = stream.ChannelSubscribing
|
||||
err = b.Websocket.AddSubscription(c)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w Channel: %s Pair: %s Error: %w", stream.ErrSubscriptionFailure, c.Channel, c.Currency, err)
|
||||
}
|
||||
|
||||
// Always remove the temporary subscription keyed by subID
|
||||
defer b.Websocket.RemoveSubscriptions(*c)
|
||||
|
||||
respRaw, err := b.Websocket.Conn.SendMessageReturnResponse("subscribe:"+subID, req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %w; Channel: %s Pair: %s", stream.ErrSubscriptionFailure, err, c.Channel, c.Currency)
|
||||
}
|
||||
|
||||
if err = b.getErrResp(respRaw); err != nil {
|
||||
wErr := fmt.Errorf("%w: %w; Channel: %s Pair: %s", stream.ErrSubscriptionFailure, err, c.Channel, c.Currency)
|
||||
b.Websocket.DataHandler <- wErr
|
||||
return wErr
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// subscribeReq returns a map of request params for subscriptions
|
||||
func subscribeReq(c *stream.ChannelSubscription) (map[string]interface{}, error) {
|
||||
req := map[string]interface{}{
|
||||
"event": "subscribe",
|
||||
"channel": c.Channel,
|
||||
}
|
||||
|
||||
for k, v := range c.Params {
|
||||
switch k {
|
||||
case CandlesPeriodKey, CandlesTimeframeKey:
|
||||
// Skip these internal Params
|
||||
case "key", "symbol":
|
||||
// Ensure user's Params aren't silently overwritten
|
||||
return nil, fmt.Errorf("%s %w", k, errParamNotAllowed)
|
||||
default:
|
||||
req[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
prefix := "t"
|
||||
if c.Asset == asset.MarginFunding {
|
||||
prefix = "f"
|
||||
}
|
||||
|
||||
needsDelimiter := c.Currency.Len() > 6
|
||||
|
||||
var formattedPair string
|
||||
if needsDelimiter {
|
||||
formattedPair = c.Currency.Format(currency.PairFormat{Uppercase: true, Delimiter: ":"}).String()
|
||||
} else {
|
||||
formattedPair = currency.PairFormat{Uppercase: true}.Format(c.Currency)
|
||||
}
|
||||
|
||||
if c.Channel == wsCandles {
|
||||
timeframe := "1m"
|
||||
if t, ok := c.Params[CandlesTimeframeKey]; ok {
|
||||
if timeframe, ok = t.(string); !ok {
|
||||
return nil, common.GetTypeAssertError("string", t, "Subscription.CandlesTimeframeKey")
|
||||
}
|
||||
}
|
||||
fundingPeriod := ""
|
||||
if p, ok := c.Params[CandlesPeriodKey]; ok {
|
||||
s, cOk := p.(string)
|
||||
if !cOk {
|
||||
return nil, common.GetTypeAssertError("string", p, "Subscription.CandlesPeriodKey")
|
||||
}
|
||||
fundingPeriod = ":p" + s
|
||||
}
|
||||
req["key"] = "trade:" + timeframe + ":" + prefix + formattedPair + fundingPeriod
|
||||
} else {
|
||||
req["symbol"] = prefix + formattedPair
|
||||
}
|
||||
|
||||
return req, nil
|
||||
}
|
||||
|
||||
// unsubscribeFromChan sends a websocket message to stop receiving data from a channel
|
||||
func (b *Bitfinex) unsubscribeFromChan(c *stream.ChannelSubscription) error {
|
||||
chanID, ok := c.Key.(int)
|
||||
if !ok {
|
||||
return common.GetTypeAssertError("int", c.Key, "chanID")
|
||||
}
|
||||
|
||||
req := map[string]interface{}{
|
||||
"event": "unsubscribe",
|
||||
"chanId": chanID,
|
||||
}
|
||||
|
||||
respRaw, err := b.Websocket.Conn.SendMessageReturnResponse("unsubscribe:"+strconv.Itoa(chanID), req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := b.getErrResp(respRaw); err != nil {
|
||||
wErr := fmt.Errorf("%w from ChanId: %v; %w", stream.ErrUnsubscribeFailure, chanID, err)
|
||||
b.Websocket.DataHandler <- wErr
|
||||
return wErr
|
||||
}
|
||||
|
||||
b.Websocket.RemoveSubscriptions(*c)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getErrResp takes a json response string and looks for an error event type
|
||||
// If found it parses the error code and message as a wrapped error and returns it
|
||||
// It might log parsing errors about the nature of the error
|
||||
// If the error message is not defined it will return a wrapped errUnknownError
|
||||
func (b *Bitfinex) getErrResp(resp []byte) error {
|
||||
event, err := jsonparser.GetUnsafeString(resp, "event")
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w 'event': %w from message: %s", errParsingWSField, err, resp)
|
||||
}
|
||||
if event != "error" {
|
||||
return nil
|
||||
}
|
||||
errCode, err := jsonparser.GetInt(resp, "code")
|
||||
if err != nil {
|
||||
log.Errorf(log.ExchangeSys, "%s %s 'code': %s from message: %s", b.Name, errParsingWSField, err, resp)
|
||||
}
|
||||
|
||||
var apiErr error
|
||||
if msg, e2 := jsonparser.GetString(resp, "msg"); e2 != nil {
|
||||
log.Errorf(log.ExchangeSys, "%s %s 'msg': %s from message: %s", b.Name, errParsingWSField, e2, resp)
|
||||
apiErr = errUnknownError
|
||||
} else {
|
||||
apiErr = errors.New(msg)
|
||||
}
|
||||
return fmt.Errorf("%w (code: %d)", apiErr, errCode)
|
||||
}
|
||||
|
||||
// WsSendAuth sends a authenticated event payload
|
||||
func (b *Bitfinex) WsSendAuth(ctx context.Context) error {
|
||||
creds, err := b.GetCredentials(ctx)
|
||||
@@ -1726,56 +1882,6 @@ func (b *Bitfinex) WsSendAuth(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// WsAddSubscriptionChannel adds a confirmed channel subscription mapping from id to original params
|
||||
func (b *Bitfinex) WsAddSubscriptionChannel(chanID int, channel, symbol string) error {
|
||||
assetType, pair, err := assetPairFromSymbol(symbol)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var c *stream.ChannelSubscription
|
||||
s := b.Websocket.GetSubscriptions()
|
||||
for i := range s {
|
||||
if strings.EqualFold(s[i].Channel, channel) && s[i].Currency.Equal(pair) && s[i].Asset == assetType {
|
||||
c = &s[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if c == nil {
|
||||
log.Errorf(log.ExchangeSys,
|
||||
"%s Could not find an existing channel subscription: %s Pair: %s ChannelID: %d Asset: %s\n",
|
||||
b.Name,
|
||||
channel,
|
||||
pair,
|
||||
chanID,
|
||||
assetType)
|
||||
c = &stream.ChannelSubscription{
|
||||
Channel: channel,
|
||||
Currency: pair,
|
||||
Asset: assetType,
|
||||
}
|
||||
}
|
||||
|
||||
if c.Params == nil {
|
||||
c.Params = map[string]interface{}{}
|
||||
}
|
||||
|
||||
c.Params["chanId"] = chanID
|
||||
|
||||
b.WebsocketSubdChannels[chanID] = c
|
||||
|
||||
if b.Verbose {
|
||||
log.Debugf(log.ExchangeSys,
|
||||
"%s Subscribed to Channel: %s Pair: %s ChannelID: %d\n",
|
||||
b.Name,
|
||||
channel,
|
||||
pair,
|
||||
chanID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WsNewOrder authenticated new order request
|
||||
func (b *Bitfinex) WsNewOrder(data *WsNewOrderRequest) (string, error) {
|
||||
data.CustomID = b.Websocket.AuthConn.GenerateMessageID(false)
|
||||
@@ -2087,42 +2193,3 @@ subSort:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
func assetPairFromSymbol(symbol string) (asset.Item, currency.Pair, error) {
|
||||
assetType := asset.Spot
|
||||
|
||||
if symbol == "" {
|
||||
return assetType, currency.EMPTYPAIR, nil
|
||||
}
|
||||
|
||||
switch symbol[0] {
|
||||
case 'f':
|
||||
assetType = asset.MarginFunding
|
||||
case 't':
|
||||
assetType = asset.Spot
|
||||
default:
|
||||
return assetType, currency.EMPTYPAIR, fmt.Errorf("unknown pair prefix: %v", symbol[0])
|
||||
}
|
||||
|
||||
pair, err := currency.NewPairFromString(symbol[1:])
|
||||
|
||||
return assetType, pair, err
|
||||
}
|
||||
|
||||
// symbolFromCandleKey extracts the symbol or pair from a subscribed channel key
|
||||
// e.g. trade:1h:tBTC, trade:1h:tBTC:CNHT, trade:1m:fBTC:p30 and trade:1m:fBTC:a30:p2:p30
|
||||
func symbolFromCandleKey(key string) (string, error) {
|
||||
parts := strings.Split(key, ":")
|
||||
if len(parts) < 3 {
|
||||
return "", fmt.Errorf("subscription key has too few parts, need 3: %v", key)
|
||||
}
|
||||
parts = parts[2:]
|
||||
if parts[0][0] == 'f' {
|
||||
// Margin Funding subscription has one currency, and suffixes
|
||||
return parts[0], nil
|
||||
}
|
||||
if len(parts) > 2 {
|
||||
return "", fmt.Errorf("subscription key has too many parts for trade types: %v", key)
|
||||
}
|
||||
return strings.Join(parts, ":"), nil
|
||||
}
|
||||
|
||||
@@ -60,7 +60,6 @@ func (b *Bitfinex) SetDefaults() {
|
||||
b.Name = "Bitfinex"
|
||||
b.Enabled = true
|
||||
b.Verbose = true
|
||||
b.WebsocketSubdChannels = make(map[int]*stream.ChannelSubscription)
|
||||
b.API.CredentialsValidator.RequiresKey = true
|
||||
b.API.CredentialsValidator.RequiresSecret = true
|
||||
|
||||
|
||||
5
exchanges/bitfinex/testdata/getErrResp.json
vendored
Normal file
5
exchanges/bitfinex/testdata/getErrResp.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{"bird": "great eared nightjar", "you_are_welcome":true}
|
||||
{"event": {}}
|
||||
{"event": "sneezegasm"}
|
||||
{"event": "error"}
|
||||
{"event": "error", "msg":"redcoats", "code":42}
|
||||
Reference in New Issue
Block a user