diff --git a/v2/futures/client.go b/v2/futures/client.go index 664ea664..126a116f 100644 --- a/v2/futures/client.go +++ b/v2/futures/client.go @@ -74,9 +74,9 @@ type UserDataEventReasonType string type ForceOrderCloseType string // Endpoints -const ( - baseApiMainUrl = "https://fapi.binance.com" - baseApiTestnetUrl = "https://testnet.binancefuture.com" +var ( + BaseApiMainUrl = "https://fapi.binance.com" + BaseApiTestnetUrl = "https://testnet.binancefuture.com" ) // Global enums @@ -207,9 +207,9 @@ func newJSON(data []byte) (j *simplejson.Json, err error) { // getApiEndpoint return the base endpoint of the WS according the UseTestnet flag func getApiEndpoint() string { if UseTestnet { - return baseApiTestnetUrl + return BaseApiTestnetUrl } - return baseApiMainUrl + return BaseApiMainUrl } // NewClient initialize an API client instance with API key and secret key. diff --git a/v2/futures/client_ws.go b/v2/futures/client_ws.go index 6e894eab..9a93268c 100644 --- a/v2/futures/client_ws.go +++ b/v2/futures/client_ws.go @@ -28,9 +28,6 @@ const ( ) var ( - // ErrorWsConnectionClosed defines that connection closed - ErrorWsConnectionClosed = errors.New("ws error: connection closed") - // ErrorWsReadConnectionTimeout defines that connection read timeout expired ErrorWsReadConnectionTimeout = errors.New("ws error: read connection timeout") @@ -58,7 +55,7 @@ type ClientWs struct { requestsList RequestList readC chan []byte readErrChan chan error - reconnectCount atomic.Int64 + reconnectCount int64 } func (c *ClientWs) debug(format string, v ...interface{}) { @@ -203,7 +200,7 @@ func (c *ClientWs) read() { if err != nil { c.debug("read: error reading message '%v'", err) c.reconnectSignal <- struct{}{} - c.readErrChan <- errors.Join(err, ErrorWsConnectionClosed) + c.readErrChan <- err c.debug("read: wait to get connected") <-c.connectionEstablishedSignal @@ -290,7 +287,7 @@ func (c *ClientWs) handleReconnect() { // startReconnect starts reconnect loop with increasing delay func (c *ClientWs) startReconnect(b *backoff.Backoff) *connection { for { - c.reconnectCount.Add(1) + atomic.AddInt64(&c.reconnectCount, 1) conn, err := newConnection() if err != nil { delay := b.Duration() @@ -304,9 +301,7 @@ func (c *ClientWs) startReconnect(b *backoff.Backoff) *connection { } // GetReconnectCount returns reconnect counter value -func (c *ClientWs) GetReconnectCount() int64 { - return c.reconnectCount.Load() -} +func (c *ClientWs) GetReconnectCount() int64 { return atomic.LoadInt64(&c.reconnectCount) } // NewRequestList creates request list func NewRequestList() RequestList { diff --git a/v2/futures/websocket_service.go b/v2/futures/websocket_service.go index 38a7fdf8..a8704742 100644 --- a/v2/futures/websocket_service.go +++ b/v2/futures/websocket_service.go @@ -4,10 +4,10 @@ import ( "encoding/json" "errors" "fmt" + "github.com/bitly/go-simplejson" + "github.com/gorilla/websocket" "strings" "time" - "github.com/gorilla/websocket" - "github.com/bitly/go-simplejson" ) // Endpoints diff --git a/v2/options/websocket_service.go b/v2/options/websocket_service.go index 75cc3fc7..2c80cdcd 100644 --- a/v2/options/websocket_service.go +++ b/v2/options/websocket_service.go @@ -490,13 +490,19 @@ func WsDepthServe(symbol string, levels string, rate *time.Duration, handler WsD // reference: https://binance-docs.github.io/apidocs/voptions/en/#websocket-market-streams // // streamName: you should collaborate stream names through official documentation or other function above defined, -// the legitimacy of parameters needs to be guaranteed by the caller +// +// the legitimacy of parameters needs to be guaranteed by the caller +// // handler: a map of handler function, its key needs to correspond to the handler of the incoming streamname, -// handler's key should be in ["trade", "index", "markPrice", "kline", "ticker", "openInterest", "option_pair", "depth"] +// +// handler's key should be in ["trade", "index", "markPrice", "kline", "ticker", "openInterest", "option_pair", "depth"] +// // for example: -// WsCombinedServe({"ETH-240927-5500-P@depth10"}, map[string]interface{}{"depth": func(*WsDepthEvent) {}}, func(error){}) -// WsCombinedServe({"ETH-240927-5500-P@depth10", "ETH-240927-5500-P@kline_1m"}, -// map[string]interface{}{"depth": func(*WsDepthEvent) {}, "kline": func(*WsKlineEvent){}}, func(error){}) +// +// WsCombinedServe({"ETH-240927-5500-P@depth10"}, map[string]interface{}{"depth": func(*WsDepthEvent) {}}, func(error){}) +// WsCombinedServe({"ETH-240927-5500-P@depth10", "ETH-240927-5500-P@kline_1m"}, +// map[string]interface{}{"depth": func(*WsDepthEvent) {}, "kline": func(*WsKlineEvent){}}, func(error){}) +// // note: the symbol(underlying) of streamName should be upper. func WsCombinedServe(streamName []string, handler map[string]interface{}, errHandler ErrHandler) (doneC, stopC chan struct{}, err error) { if len(streamName) <= 0 || len(handler) <= 0 {