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

@@ -59,12 +59,13 @@ var comms = make(chan websocket.Response)
// WsConnect initiates a websocket connection
func (g *Gemini) WsConnect() error {
ctx := context.TODO()
if !g.Websocket.IsEnabled() || !g.IsEnabled() {
return websocket.ErrWebsocketNotEnabled
}
var dialer gws.Dialer
err := g.Websocket.Conn.Dial(&dialer, http.Header{})
err := g.Websocket.Conn.Dial(ctx, &dialer, http.Header{})
if err != nil {
return err
}
@@ -74,7 +75,7 @@ func (g *Gemini) WsConnect() error {
go g.wsFunnelConnectionData(g.Websocket.Conn)
if g.Websocket.CanUseAuthenticatedEndpoints() {
err := g.WsAuth(context.TODO(), &dialer)
err := g.WsAuth(ctx, &dialer)
if err != nil {
log.Errorf(log.ExchangeSys, "%v - websocket authentication failed: %v\n", g.Name, err)
g.Websocket.SetCanUseAuthenticatedEndpoints(false)
@@ -98,15 +99,17 @@ func (g *Gemini) GetSubscriptionTemplate(_ *subscription.Subscription) (*templat
// Subscribe sends a websocket message to receive data from the channel
func (g *Gemini) Subscribe(subs subscription.List) error {
return g.manageSubs(subs, wsSubscribeOp)
ctx := context.TODO()
return g.manageSubs(ctx, subs, wsSubscribeOp)
}
// Unsubscribe sends a websocket message to stop receiving data from the channel
func (g *Gemini) Unsubscribe(subs subscription.List) error {
return g.manageSubs(subs, wsUnsubscribeOp)
ctx := context.TODO()
return g.manageSubs(ctx, subs, wsUnsubscribeOp)
}
func (g *Gemini) manageSubs(subs subscription.List, op wsSubOp) error {
func (g *Gemini) manageSubs(ctx context.Context, subs subscription.List, op wsSubOp) error {
req := wsSubscribeRequest{
Type: op,
Subscriptions: make([]wsSubscriptions, 0, len(subs)),
@@ -118,7 +121,7 @@ func (g *Gemini) manageSubs(subs subscription.List, op wsSubOp) error {
})
}
if err := g.Websocket.Conn.SendJSONMessage(context.TODO(), request.Unset, req); err != nil {
if err := g.Websocket.Conn.SendJSONMessage(ctx, request.Unset, req); err != nil {
return err
}
@@ -165,7 +168,7 @@ func (g *Gemini) WsAuth(ctx context.Context, dialer *gws.Dialer) error {
headers.Add("X-GEMINI-SIGNATURE", hex.EncodeToString(hmac))
headers.Add("Cache-Control", "no-cache")
err = g.Websocket.AuthConn.Dial(dialer, headers)
err = g.Websocket.AuthConn.Dial(ctx, dialer, headers)
if err != nil {
return fmt.Errorf("%v Websocket connection %v error. Error %v", g.Name, endpoint, err)
}