websocket/exchanges: populate context before multi connection upgrade (#1933)

* websocket/exchanges: populate context before multi connection upgrade

* fix test

* linter: fix

* gk: dial

* gk: nits rm param names

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
This commit is contained in:
Ryan O'Hara-Reid
2025-06-17 13:43:00 +10:00
committed by GitHub
parent 2958e64afe
commit 3e80f1b9e5
64 changed files with 1160 additions and 1124 deletions

View File

@@ -662,7 +662,7 @@ func TestWsAuth(t *testing.T) {
t.Skip(websocket.ErrWebsocketNotEnabled.Error())
}
var dialer gws.Dialer
err := b.Websocket.Conn.Dial(&dialer, http.Header{})
err := b.Websocket.Conn.Dial(t.Context(), &dialer, http.Header{})
require.NoError(t, err)
go b.wsReadData()

View File

@@ -88,8 +88,9 @@ func (b *Bitmex) WsConnect() error {
return websocket.ErrWebsocketNotEnabled
}
ctx := context.TODO()
var dialer gws.Dialer
if err := b.Websocket.Conn.Dial(&dialer, http.Header{}); err != nil {
if err := b.Websocket.Conn.Dial(ctx, &dialer, http.Header{}); err != nil {
return err
}
@@ -97,7 +98,6 @@ func (b *Bitmex) WsConnect() error {
go b.wsReadData()
if b.Websocket.CanUseAuthenticatedEndpoints() {
ctx := context.TODO()
if err := b.websocketSendAuth(ctx); err != nil {
b.Websocket.SetCanUseAuthenticatedEndpoints(false)
log.Errorf(log.ExchangeSys, "%v - authentication failed: %v\n", b.Name, err)
@@ -521,21 +521,23 @@ func (b *Bitmex) GetSubscriptionTemplate(_ *subscription.Subscription) (*templat
// Subscribe subscribes to a websocket channel
func (b *Bitmex) Subscribe(subs subscription.List) error {
ctx := context.TODO()
return common.AppendError(
b.ParallelChanOp(subs.Public(), func(l subscription.List) error { return b.manageSubs(wsSubscribeOp, l) }, len(subs)),
b.ParallelChanOp(subs.Private(), func(l subscription.List) error { return b.manageSubs(wsSubscribeOp, l) }, len(subs)),
b.ParallelChanOp(ctx, subs.Public(), func(ctx context.Context, l subscription.List) error { return b.manageSubs(ctx, wsSubscribeOp, l) }, len(subs)),
b.ParallelChanOp(ctx, subs.Private(), func(ctx context.Context, l subscription.List) error { return b.manageSubs(ctx, wsSubscribeOp, l) }, len(subs)),
)
}
// Unsubscribe sends a websocket message to stop receiving data from the channel
func (b *Bitmex) Unsubscribe(subs subscription.List) error {
ctx := context.TODO()
return common.AppendError(
b.ParallelChanOp(subs.Public(), func(l subscription.List) error { return b.manageSubs(wsUnsubscribeOp, l) }, len(subs)),
b.ParallelChanOp(subs.Private(), func(l subscription.List) error { return b.manageSubs(wsUnsubscribeOp, l) }, len(subs)),
b.ParallelChanOp(ctx, subs.Public(), func(ctx context.Context, l subscription.List) error { return b.manageSubs(ctx, wsUnsubscribeOp, l) }, len(subs)),
b.ParallelChanOp(ctx, subs.Private(), func(ctx context.Context, l subscription.List) error { return b.manageSubs(ctx, wsUnsubscribeOp, l) }, len(subs)),
)
}
func (b *Bitmex) manageSubs(op string, subs subscription.List) error {
func (b *Bitmex) manageSubs(ctx context.Context, op string, subs subscription.List) error {
req := WebsocketRequest{
Command: op,
}
@@ -548,7 +550,7 @@ func (b *Bitmex) manageSubs(op string, subs subscription.List) error {
if err != nil {
return err
}
resps, errs := b.Websocket.Conn.SendMessageReturnResponses(context.TODO(), request.Unset, string(reqJSON), req, len(subs))
resps, errs := b.Websocket.Conn.SendMessageReturnResponses(ctx, request.Unset, string(reqJSON), req, len(subs))
for _, resp := range resps {
if errMsg, _ := jsonparser.GetString(resp, "error"); errMsg != "" {
errs = common.AppendError(errs, errors.New(errMsg))