pkg/exchange: use individual rate limit

This commit is contained in:
edwin 2024-10-02 17:23:20 +08:00
parent 9963f5c14d
commit fc4a9769c9
3 changed files with 26 additions and 10 deletions

View File

@ -70,7 +70,7 @@ type WalletBalances struct {
} `json:"coin"` } `json:"coin"`
} }
//go:generate GetRequest -url "/v5/account/wallet-balance" -type GetWalletBalancesRequest -responseDataType .WalletBalancesResponse //go:generate GetRequest -url "/v5/account/wallet-balance" -type GetWalletBalancesRequest -responseDataType .WalletBalancesResponse -rateLimiter 1+15/1s
type GetWalletBalancesRequest struct { type GetWalletBalancesRequest struct {
client requestgen.AuthenticatedAPIClient client requestgen.AuthenticatedAPIClient

View File

@ -1,4 +1,4 @@
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Result -url /v5/account/wallet-balance -type GetWalletBalancesRequest -responseDataType .WalletBalancesResponse"; DO NOT EDIT. // Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Result -url /v5/account/wallet-balance -type GetWalletBalancesRequest -responseDataType .WalletBalancesResponse -rateLimiter 1+15/1s"; DO NOT EDIT.
package bybitapi package bybitapi
@ -6,11 +6,14 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"golang.org/x/time/rate"
"net/url" "net/url"
"reflect" "reflect"
"regexp" "regexp"
) )
var GetWalletBalancesRequestLimiter = rate.NewLimiter(15.000000150000002, 1)
func (g *GetWalletBalancesRequest) AccountType(accountType AccountType) *GetWalletBalancesRequest { func (g *GetWalletBalancesRequest) AccountType(accountType AccountType) *GetWalletBalancesRequest {
g.accountType = accountType g.accountType = accountType
return g return g
@ -150,6 +153,9 @@ func (g *GetWalletBalancesRequest) GetPath() string {
// Do generates the request object and send the request object to the API endpoint // Do generates the request object and send the request object to the API endpoint
func (g *GetWalletBalancesRequest) Do(ctx context.Context) (*WalletBalancesResponse, error) { func (g *GetWalletBalancesRequest) Do(ctx context.Context) (*WalletBalancesResponse, error) {
if err := GetWalletBalancesRequestLimiter.Wait(ctx); err != nil {
return nil, err
}
// no body params // no body params
var params interface{} var params interface{}
@ -173,15 +179,29 @@ func (g *GetWalletBalancesRequest) Do(ctx context.Context) (*WalletBalancesRespo
} }
var apiResponse APIResponse var apiResponse APIResponse
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err type responseUnmarshaler interface {
Unmarshal(data []byte) error
}
if unmarshaler, ok := interface{}(&apiResponse).(responseUnmarshaler); ok {
if err := unmarshaler.Unmarshal(response.Body); err != nil {
return nil, err
}
} else {
// The line below checks the content type, however, some API server might not send the correct content type header,
// Hence, this is commented for backward compatibility
// response.IsJSON()
if err := response.DecodeJSON(&apiResponse); err != nil {
return nil, err
}
} }
type responseValidator interface { type responseValidator interface {
Validate() error Validate() error
} }
validator, ok := interface{}(apiResponse).(responseValidator)
if ok { if validator, ok := interface{}(&apiResponse).(responseValidator); ok {
if err := validator.Validate(); err != nil { if err := validator.Validate(); err != nil {
return nil, err return nil, err
} }

View File

@ -518,10 +518,6 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
} }
func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error) { func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error) {
if err := sharedRateLimiter.Wait(ctx); err != nil {
return nil, fmt.Errorf("query account balances rate limiter wait error: %w", err)
}
req := e.client.NewGetWalletBalancesRequest() req := e.client.NewGetWalletBalancesRequest()
accounts, err := req.Do(ctx) accounts, err := req.Do(ctx)
if err != nil { if err != nil {