From cfc66dc13e192848db9b5a3c4504ac19341c2440 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 30 Dec 2021 17:18:04 +0800 Subject: [PATCH] bbgo: add session connection notification --- pkg/bbgo/session.go | 10 ++++++++++ pkg/exchange/binance/stream.go | 8 ++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pkg/bbgo/session.go b/pkg/bbgo/session.go index 5b0ceb1bf..c34ae3135 100644 --- a/pkg/bbgo/session.go +++ b/pkg/bbgo/session.go @@ -322,6 +322,16 @@ func (session *ExchangeSession) Init(ctx context.Context, environ *Environment) // forward trade updates and order updates to the order executor session.UserDataStream.OnTradeUpdate(session.OrderExecutor.EmitTradeUpdate) session.UserDataStream.OnOrderUpdate(session.OrderExecutor.EmitOrderUpdate) + + if len(session.Notifiability.notifiers) > 0 { + session.UserDataStream.OnDisconnect(func() { + session.Notifiability.Notify("session %s user data stream disconnected", session.Name) + }) + session.UserDataStream.OnConnect(func() { + session.Notifiability.Notify("session %s user data stream connected", session.Name) + }) + } + session.Account.BindStream(session.UserDataStream) // if metrics mode is enabled, we bind the callbacks to update metrics diff --git a/pkg/exchange/binance/stream.go b/pkg/exchange/binance/stream.go index bf34c47ad..7302fab04 100644 --- a/pkg/exchange/binance/stream.go +++ b/pkg/exchange/binance/stream.go @@ -49,10 +49,10 @@ var defaultDialer = &websocket.Dialer{ // - the re-connector calls connect() to create the new connection object, go to the 1 step. // When stream.Close() is called, a close message must be written to the websocket connection. -const readTimeout = 3 * time.Minute -const writeTimeout = 15 * time.Second -const pingInterval = readTimeout / 3 -const listenKeyKeepAliveInterval = 10 * time.Minute +const readTimeout = 1 * time.Minute +const writeTimeout = 5 * time.Second +const pingInterval = readTimeout / 2 +const listenKeyKeepAliveInterval = 30 * time.Minute const reconnectCoolDownPeriod = 15 * time.Second func init() {