Bitstamp: Add websocket heartbeat (#1329)

This prevents the frequent (5x / hour) disconn/reconns we're seeing with a
10s or even 20s traffic timeout.
I'd like to base the interval on the traffic timeout / 2, but that's
non-trivial right now, and 8s isn't an excessive default
This commit is contained in:
Gareth Kirwan
2023-08-29 00:22:50 +01:00
committed by GitHub
parent c5240153f9
commit ec312ca0d3

View File

@@ -22,8 +22,11 @@ import (
const (
bitstampWSURL = "wss://ws.bitstamp.net" //nolint // gosec false positive
hbInterval = 8 * time.Second // Connection monitor defaults to 10s inactivity
)
var hbMsg = []byte(`{"event":"bts:heartbeat"}`)
// WsConnect connects to a websocket feed
func (b *Bitstamp) WsConnect() error {
if !b.Websocket.IsEnabled() || !b.IsEnabled() {
@@ -37,6 +40,11 @@ func (b *Bitstamp) WsConnect() error {
if b.Verbose {
log.Debugf(log.ExchangeSys, "%s Connected to Websocket.\n", b.Name)
}
b.Websocket.Conn.SetupPingHandler(stream.PingHandler{
MessageType: websocket.TextMessage,
Message: hbMsg,
Delay: hbInterval,
})
err = b.seedOrderBook(context.TODO())
if err != nil {
b.Websocket.DataHandler <- err
@@ -72,6 +80,8 @@ func (b *Bitstamp) wsHandleData(respRaw []byte) error {
}
switch wsResponse.Event {
case "bts:heartbeat":
return nil
case "bts:subscribe", "bts:subscription_succeeded":
if b.Verbose {
log.Debugf(log.ExchangeSys, "%v - Websocket subscription acknowledgement", b.Name)