Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libxc/binance: Handle invalid listen key error #3139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions client/mm/libxc/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const (
// /sapi/v1/capital/config/getall endpoint.
fakeBinanceURL = "http://localhost:37346"
fakeBinanceWsURL = "ws://localhost:37346"

bnErrCodeInvalidListenKey = -1125
)

// binanceOrderBook manages an orderbook for a single market. It keeps
Expand Down Expand Up @@ -453,6 +455,23 @@ type withdrawInfo struct {
lotSize uint64
}

type BinanceCodedErr struct {
Code int `json:"code"`
Msg string `json:"msg"`
}

func (e *BinanceCodedErr) Error() string {
return fmt.Sprintf("code = %d, msg = %q", e.Code, e.Msg)
}

func errHasBnCode(err error, code int) bool {
var bnErr *BinanceCodedErr
if errors.As(err, &bnErr) && bnErr.Code == code {
return true
}
return false
}

type binance struct {
log dex.Logger
marketsURL string
Expand Down Expand Up @@ -1324,13 +1343,11 @@ func (bnc *binance) request(ctx context.Context, method, endpoint string, query,

req.Header = header

var errPayload struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
if err := dexnet.Do(req, thing, dexnet.WithSizeLimit(1<<24), dexnet.WithErrorParsing(&errPayload)); err != nil {
bnc.log.Errorf("request error from endpoint %s %q with query = %q, body = %q", method, endpoint, queryString, bodyString)
return fmt.Errorf("%w, bn code = %d, msg = %q", err, errPayload.Code, errPayload.Msg)
var bnErr BinanceCodedErr
if err := dexnet.Do(req, thing, dexnet.WithSizeLimit(1<<24), dexnet.WithErrorParsing(&bnErr)); err != nil {
bnc.log.Errorf("request error from endpoint %s %q with query = %q, body = %q, bn coded error: %v",
method, endpoint, queryString, bodyString, &bnErr)
return errors.Join(err, &bnErr)
}
return nil
}
Expand Down Expand Up @@ -1559,6 +1576,11 @@ func (bnc *binance) getUserDataStream(ctx context.Context) (err error) {
q.Add("listenKey", bnc.listenKey.Load().(string))
// Doing a PUT on a listenKey will extend its validity for 60 minutes.
if err := bnc.request(ctx, http.MethodPut, "/api/v3/userDataStream", q, nil, true, false, nil); err != nil {
if errHasBnCode(err, bnErrCodeInvalidListenKey) {
bnc.log.Warnf("Invalid listen key. Reconnecting...")
doReconnect()
return
}
bnc.log.Errorf("Error sending keep-alive request: %v. Trying again in 10 seconds", err)
retryKeepAlive = time.After(time.Second * 10)
return
Expand Down
Loading