Skip to content

Commit

Permalink
Shared REST rate limit definitions with Websocket service, set lookup…
Browse files Browse the repository at this point in the history
… item to nil for systems that do not require rate limiting; add glorious nit
  • Loading branch information
shazbert committed Dec 5, 2024
1 parent f8729d2 commit 4baa7a2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
10 changes: 4 additions & 6 deletions exchanges/gateio/gateio_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
)

const (
gateioWebsocketEndpoint = "wss://api.gateio.ws/ws/v4/"
gateioWebsocketRateLimit = 120 * time.Millisecond
gateioWebsocketEndpoint = "wss://api.gateio.ws/ws/v4/"

spotPingChannel = "spot.ping"
spotPongChannel = "spot.pong"
Expand Down Expand Up @@ -90,7 +88,7 @@ func (g *Gateio) WsConnectSpot(ctx context.Context, conn stream.Connection) erro
if err != nil {
return err
}
conn.SetupPingHandler(request.Unset, stream.PingHandler{
conn.SetupPingHandler(websocketRateLimitNotNeededEPL, stream.PingHandler{
Websocket: true,
Delay: time.Second * 15,
Message: pingMessage,
Expand Down Expand Up @@ -587,7 +585,7 @@ func (g *Gateio) manageSubs(ctx context.Context, event string, conn stream.Conne
if err != nil {
return err
}
result, err := conn.SendMessageReturnResponse(ctx, request.Unset, msg.ID, msg)
result, err := conn.SendMessageReturnResponse(ctx, websocketRateLimitNotNeededEPL, msg.ID, msg)
if err != nil {
return err
}
Expand Down Expand Up @@ -698,7 +696,7 @@ func (g *Gateio) handleSubscription(ctx context.Context, conn stream.Connection,
}
var errs error
for k := range payloads {
result, err := conn.SendMessageReturnResponse(ctx, request.Unset, payloads[k].ID, payloads[k])
result, err := conn.SendMessageReturnResponse(ctx, websocketRateLimitNotNeededEPL, payloads[k].ID, payloads[k])
if err != nil {
errs = common.AppendError(errs, err)
continue
Expand Down
6 changes: 1 addition & 5 deletions exchanges/gateio/gateio_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,14 @@ func (g *Gateio) Setup(exch *config.Exchange) error {
FillsFeed: g.Features.Enabled.FillsFeed,
TradeFeed: g.Features.Enabled.TradeFeed,
UseMultiConnectionManagement: true,
RateLimitDefinitions: packageRateLimits,
})
if err != nil {
return err
}
// Spot connection
err = g.Websocket.SetupNewConnection(&stream.ConnectionSetup{
URL: gateioWebsocketEndpoint,
RateLimit: request.NewWeightedRateLimitByDuration(gateioWebsocketRateLimit),
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
Handler: g.WsHandleSpotData,
Expand All @@ -226,7 +226,6 @@ func (g *Gateio) Setup(exch *config.Exchange) error {
// Futures connection - USDT margined
err = g.Websocket.SetupNewConnection(&stream.ConnectionSetup{
URL: futuresWebsocketUsdtURL,
RateLimit: request.NewWeightedRateLimitByDuration(gateioWebsocketRateLimit),
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
Handler: func(ctx context.Context, incoming []byte) error {
Expand All @@ -245,7 +244,6 @@ func (g *Gateio) Setup(exch *config.Exchange) error {
// Futures connection - BTC margined
err = g.Websocket.SetupNewConnection(&stream.ConnectionSetup{
URL: futuresWebsocketBtcURL,
RateLimit: request.NewWeightedRateLimitByDuration(gateioWebsocketRateLimit),
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
Handler: func(ctx context.Context, incoming []byte) error {
Expand All @@ -265,7 +263,6 @@ func (g *Gateio) Setup(exch *config.Exchange) error {
// Futures connection - Delivery - USDT margined
err = g.Websocket.SetupNewConnection(&stream.ConnectionSetup{
URL: deliveryRealUSDTTradingURL,
RateLimit: request.NewWeightedRateLimitByDuration(gateioWebsocketRateLimit),
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
Handler: func(ctx context.Context, incoming []byte) error {
Expand All @@ -284,7 +281,6 @@ func (g *Gateio) Setup(exch *config.Exchange) error {
// Futures connection - Options
return g.Websocket.SetupNewConnection(&stream.ConnectionSetup{
URL: optionsWebsocketURL,
RateLimit: request.NewWeightedRateLimitByDuration(gateioWebsocketRateLimit),
ResponseCheckTimeout: exch.WebsocketResponseCheckTimeout,
ResponseMaxLimit: exch.WebsocketResponseMaxLimit,
Handler: g.WsHandleOptionsData,
Expand Down
10 changes: 9 additions & 1 deletion exchanges/gateio/ratelimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ const (
optionsOrderEPL
optionsCancelOrderEPL
optionsTradingHistoryEPL

websocketRateLimitNotNeededEPL
)

// package level rate limits for REST API
Expand Down Expand Up @@ -254,7 +256,7 @@ var packageRateLimits = request.RateLimitDefinitions{
walletSavedAddressesEPL: standardRateLimit(),
walletTradingFeeEPL: standardRateLimit(),
walletTotalBalanceEPL: personalAccountRateLimit(),
walletWithdrawEPL: request.NewRateLimitWithWeight(time.Second*3, 1, 1), // 1r/3s
walletWithdrawEPL: withdrawFromWalletRateLimit(),
walletCancelWithdrawEPL: standardRateLimit(),

subAccountEPL: personalAccountRateLimit(),
Expand Down Expand Up @@ -376,6 +378,8 @@ var packageRateLimits = request.RateLimitDefinitions{
optionsTradingHistoryEPL: standardRateLimit(),

privateUnifiedSpotEPL: standardRateLimit(),

websocketRateLimitNotNeededEPL: nil, // no rate limit for certain websocket functions
}

func standardRateLimit() *request.RateLimiterWithWeight {
Expand Down Expand Up @@ -409,3 +413,7 @@ func deliverySubmitCancelAmendRateLimit() *request.RateLimiterWithWeight {
func optionsSubmitCancelAmendRateLimit() *request.RateLimiterWithWeight {
return request.NewRateLimitWithWeight(time.Second, 200, 1)
}

func withdrawFromWalletRateLimit() *request.RateLimiterWithWeight {
return request.NewRateLimitWithWeight(time.Second*3, 1, 1)
}

0 comments on commit 4baa7a2

Please sign in to comment.