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

binance: echo ping data for binance #2985

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 12 additions & 2 deletions client/comms/wsconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ type WsCfg struct {
DisableAutoReconnect bool

ConnectHeaders http.Header

// EchoPingData will echo any data from pings as the pong data.
EchoPingData bool
}

// wsConn represents a client websocket connection.
Expand Down Expand Up @@ -258,7 +261,9 @@ func (conn *wsConn) connect(ctx context.Context) error {
return err
}

ws.SetPingHandler(func(string) error {
echoPing := conn.cfg.EchoPingData

ws.SetPingHandler(func(appData string) error {
now := time.Now()

// Set the deadline for the next ping.
Expand All @@ -268,8 +273,13 @@ func (conn *wsConn) connect(ctx context.Context) error {
return err
}

var data []byte
if echoPing {
data = []byte(appData)
}

// Respond with a pong.
err = ws.WriteControl(websocket.PongMessage, []byte{}, now.Add(writeWait))
err = ws.WriteControl(websocket.PongMessage, data, now.Add(writeWait))
if err != nil {
// read loop handles reconnect
conn.log.Errorf("pong write error: %v", err)
Expand Down
8 changes: 5 additions & 3 deletions client/mm/libxc/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1390,8 +1390,9 @@ func (bnc *binance) getUserDataStream(ctx context.Context) (err error) {
}

conn, err := comms.NewWsConn(&comms.WsCfg{
URL: bnc.wsURL + "/ws/" + listenKey,
PingWait: time.Minute * 4,
URL: bnc.wsURL + "/ws/" + listenKey,
PingWait: time.Minute * 4,
EchoPingData: true,
ReconnectSync: func() {
bnc.log.Debugf("Binance reconnected")
},
Expand Down Expand Up @@ -1601,7 +1602,8 @@ func (bnc *binance) connectToMarketDataStream(ctx context.Context, baseID, quote
// minutes. If the websocket server does not receive a pong frame
// back from the connection within a 10 minute period, the connection
// will be disconnected. Unsolicited pong frames are allowed.
PingWait: time.Minute * 4,
PingWait: time.Minute * 4,
EchoPingData: true,
ReconnectSync: func() {
bnc.log.Debugf("Binance reconnected")
},
Expand Down
Loading