websocket: Remove GenerateMessageID (#2008)

* Exchanges: Remove example BespokeGenerateMessageID

* Okx: Replace conn.RequestIDGenerator with MesssageID

Continued overall direction to remove the closed-loop of e => conn => e
roundtrip for message ids

* Exchanges: Add MessageSequence

This method removes the either/or nature of message id generation.
We don't tie the message ids to connections, or to anything.
Consumers just call whichever they want, or even combine them as they
want.
Anything more complicated will need a separate installation anyway

* GateIO: Split usage of MessageID and MessageSequence

* Binance: Switch to UUID message IDs

* Kraken: Switch to e.MessageSequence

* Kucoin: Switch to MessageID

* HitBTC: Switch to UUIDv7 for ws message ID

* Bybit: Switch to UUIDv7 for ws message ID

* Bitfinex: Switch to UUIDv7 and MessageSequence

Tested CID - It accepts 53 bits only for an int, so MessageSequence
makes sense. Can't use MessageID

* Websocket: Remove now unused MessageID function

Moved all MessageID usage into funcs and onto base methods, to remove
the closed loop of message IDs

* Docs: Update guidance for message signatures
This commit is contained in:
Gareth Kirwan
2025-10-24 07:14:24 +07:00
committed by GitHub
parent 0f70cfd8b6
commit bda9bbec66
33 changed files with 137 additions and 224 deletions

View File

@@ -29,8 +29,7 @@ import (
type Exchange struct {
exchange.Base
messageIDSeq common.Counter
account accountTypeHolder
account accountTypeHolder
}
const (

View File

@@ -2936,7 +2936,6 @@ type FixtureConnection struct {
websocket.Connection
}
func (d *FixtureConnection) GenerateMessageID(bool) int64 { return 1337 }
func (d *FixtureConnection) SetupPingHandler(request.EndpointLimit, websocket.PingHandler) {}
func (d *FixtureConnection) Dial(context.Context, *gws.Dialer, http.Header) error { return d.dialError }

View File

@@ -102,7 +102,7 @@ func (e *Exchange) WsConnect(ctx context.Context, conn websocket.Connection) err
// WebsocketAuthenticatePrivateConnection sends an authentication message to the private websocket for inbound account
// data
func (e *Exchange) WebsocketAuthenticatePrivateConnection(ctx context.Context, conn websocket.Connection) error {
req, err := e.GetAuthenticationPayload(ctx, strconv.FormatInt(conn.GenerateMessageID(false), 10))
req, err := e.GetAuthenticationPayload(ctx, e.MessageID())
if err != nil {
return err
}
@@ -170,7 +170,7 @@ func (e *Exchange) GetAuthenticationPayload(ctx context.Context, requestID strin
}, nil
}
func (e *Exchange) handleSubscriptions(conn websocket.Connection, operation string, subs subscription.List) (args []SubscriptionArgument, err error) {
func (e *Exchange) handleSubscriptions(_ websocket.Connection, operation string, subs subscription.List) (args []SubscriptionArgument, err error) {
subs, err = subs.ExpandTemplates(e)
if err != nil {
return
@@ -181,7 +181,7 @@ func (e *Exchange) handleSubscriptions(conn websocket.Connection, operation stri
args = append(args, SubscriptionArgument{
auth: b[0].Authenticated,
Operation: operation,
RequestID: strconv.FormatInt(conn.GenerateMessageID(false), 10),
RequestID: e.MessageID(),
Arguments: b.QualifiedChannels(),
associatedSubs: b,
})
@@ -719,7 +719,7 @@ func hasPotentialDelimiter(a asset.Item) bool {
// TODO: Remove this function when template expansion is across all assets
func (e *Exchange) submitDirectSubscription(ctx context.Context, conn websocket.Connection, a asset.Item, operation string, channelsToSubscribe subscription.List) error {
payloads, err := e.directSubscriptionPayload(conn, a, operation, channelsToSubscribe)
payloads, err := e.directSubscriptionPayload(a, operation, channelsToSubscribe)
if err != nil {
return err
}
@@ -757,17 +757,17 @@ func (e *Exchange) submitDirectSubscription(ctx context.Context, conn websocket.
}
// TODO: Remove this function when template expansion is across all assets
func (e *Exchange) directSubscriptionPayload(conn websocket.Connection, assetType asset.Item, operation string, channelsToSubscribe subscription.List) ([]SubscriptionArgument, error) {
func (e *Exchange) directSubscriptionPayload(assetType asset.Item, operation string, channelsToSubscribe subscription.List) ([]SubscriptionArgument, error) {
var args []SubscriptionArgument
arg := SubscriptionArgument{
Operation: operation,
RequestID: strconv.FormatInt(conn.GenerateMessageID(false), 10),
RequestID: e.MessageID(),
Arguments: []string{},
}
authArg := SubscriptionArgument{
auth: true,
Operation: operation,
RequestID: strconv.FormatInt(conn.GenerateMessageID(false), 10),
RequestID: e.MessageID(),
Arguments: []string{},
}
@@ -812,7 +812,7 @@ func (e *Exchange) directSubscriptionPayload(conn websocket.Connection, assetTyp
args = append(args, arg)
arg = SubscriptionArgument{
Operation: operation,
RequestID: strconv.FormatInt(conn.GenerateMessageID(false), 10),
RequestID: e.MessageID(),
Arguments: []string{},
}
}

View File

@@ -75,9 +75,6 @@ func (e *Exchange) sendWebsocketTradeRequest(ctx context.Context, op, orderLinkI
return nil, err
}
tn := time.Now()
requestID := strconv.FormatInt(outbound.GenerateMessageID(false), 10)
// Set up a listener to wait for the response to come back from the inbound connection. The request is sent through
// the outbound trade connection, the response can come back through the inbound private connection before the
// outbound connection sends its acknowledgement.
@@ -86,9 +83,10 @@ func (e *Exchange) sendWebsocketTradeRequest(ctx context.Context, op, orderLinkI
return nil, err
}
requestID := e.MessageID()
outResp, err := outbound.SendMessageReturnResponse(ctx, limit, requestID, WebsocketGeneralPayload{
RequestID: requestID,
Header: map[string]string{"X-BAPI-TIMESTAMP": strconv.FormatInt(tn.UnixMilli(), 10)},
Header: map[string]string{"X-BAPI-TIMESTAMP": strconv.FormatInt(time.Now().UnixMilli(), 10)},
Operation: op,
Arguments: []any{payload},
})

View File

@@ -255,7 +255,6 @@ func (e *Exchange) Setup(exch *config.Exchange) error {
Handler: func(_ context.Context, conn websocket.Connection, resp []byte) error {
return e.wsHandleData(conn, asset.Spot, resp)
},
RequestIDGenerator: e.messageIDSeq.IncrementAndGet,
}); err != nil {
return err
}
@@ -277,7 +276,6 @@ func (e *Exchange) Setup(exch *config.Exchange) error {
Handler: func(_ context.Context, conn websocket.Connection, resp []byte) error {
return e.wsHandleData(conn, asset.Options, resp)
},
RequestIDGenerator: e.messageIDSeq.IncrementAndGet,
}); err != nil {
return err
}
@@ -305,8 +303,7 @@ func (e *Exchange) Setup(exch *config.Exchange) error {
Handler: func(_ context.Context, conn websocket.Connection, resp []byte) error {
return e.wsHandleData(conn, asset.USDTMarginedFutures, resp)
},
RequestIDGenerator: e.messageIDSeq.IncrementAndGet,
MessageFilter: asset.USDTMarginedFutures, // Unused but it allows us to differentiate between the two linear futures types.
MessageFilter: asset.USDTMarginedFutures, // Unused but it allows us to differentiate between the two linear futures types.
}); err != nil {
return err
}
@@ -334,8 +331,7 @@ func (e *Exchange) Setup(exch *config.Exchange) error {
Handler: func(_ context.Context, conn websocket.Connection, resp []byte) error {
return e.wsHandleData(conn, asset.USDCMarginedFutures, resp)
},
RequestIDGenerator: e.messageIDSeq.IncrementAndGet,
MessageFilter: asset.USDCMarginedFutures, // Unused but it allows us to differentiate between the two linear futures types.
MessageFilter: asset.USDCMarginedFutures, // Unused but it allows us to differentiate between the two linear futures types.
}); err != nil {
return err
}
@@ -357,7 +353,6 @@ func (e *Exchange) Setup(exch *config.Exchange) error {
Handler: func(_ context.Context, conn websocket.Connection, resp []byte) error {
return e.wsHandleData(conn, asset.CoinMarginedFutures, resp)
},
RequestIDGenerator: e.messageIDSeq.IncrementAndGet,
}); err != nil {
return err
}
@@ -376,7 +371,6 @@ func (e *Exchange) Setup(exch *config.Exchange) error {
Handler: func(_ context.Context, conn websocket.Connection, resp []byte) error {
return e.wsHandleTradeData(conn, resp)
},
RequestIDGenerator: e.messageIDSeq.IncrementAndGet,
Authenticate: e.WebsocketAuthenticateTradeConnection,
MessageFilter: OutboundTradeConnection,
SubscriptionsNotRequired: true,
@@ -400,7 +394,6 @@ func (e *Exchange) Setup(exch *config.Exchange) error {
Subscriber: e.authSubscribe,
Unsubscriber: e.authUnsubscribe,
Handler: e.wsHandleAuthenticatedData,
RequestIDGenerator: e.messageIDSeq.IncrementAndGet,
Authenticate: e.WebsocketAuthenticatePrivateConnection,
MessageFilter: InboundPrivateConnection,
})