Skip to content

Commit

Permalink
feat: make futures urls configurable from app (#630)
Browse files Browse the repository at this point in the history
* fix: formatting

* feat: export base futures api urls

* fix: backward comp with golang 1.18

---------

Co-authored-by: Artur Abelian <[email protected]>
  • Loading branch information
vigodsky and Artur Abelian authored Nov 12, 2024
1 parent 5c80e99 commit 596cf99
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
10 changes: 5 additions & 5 deletions v2/futures/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 4 additions & 9 deletions v2/futures/client_ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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{}) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions v2/futures/websocket_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions v2/options/websocket_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 596cf99

Please sign in to comment.