Code Monkey home page Code Monkey logo

Comments (7)

adshao avatar adshao commented on June 3, 2024 1

Hi @Krisa , in my opinion, it's not a job of SDK, you should check the rate limit in your app using the SDK.

from go-binance.

riaan53 avatar riaan53 commented on June 3, 2024

It would be nice but should just allow custom rate limiters by implementing an interface. I use this lib in a distributed env and using redis for rate limiting.

from go-binance.

0cv avatar 0cv commented on June 3, 2024

@adshao sorry, which SDK do you refer to?

Not sure we're referring to the same thing, but I'm not sure how this could possibly be implemented in a good way outside this package:
1- The current rate limit is returned in the HTTP headers as described in the Binance doc through X-MBX-USED-WEIGHT-(intervalNum)(intervalLetter) and I don't think there is a way to read this value outside the package. If there was an easy way to retrieve this value, it would be a good first step though.
2- Obviously I could catch errors when it's already too late, and implement for each different request a rate limiter as described in my OP. That looks completely sub-optimal though. I was instead thinking this shall be implemented somewhere in that function

func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption) (data []byte, err error) {

from go-binance.

adshao avatar adshao commented on June 3, 2024

@Krisa SDK is this go-binance package.
Thanks for the advise, you are right, currently there is no way to get response instance, so that we can't check current rate limit which is returned in response header.
Maybe we can define a interface for all service data to implement, for example:

type Response struct {
    Header net.http.Header
}

then parse response header into Header field.
So that users can get the response header to check rate limit.
Is there any better advise?

from go-binance.

0cv avatar 0cv commented on June 3, 2024

Ok, closing this issue as actually, this can be implemented as of now by using the RoundTripper and overriding the HTTPClient. For reference:
https://stackoverflow.com/questions/47465927/use-same-headers-for-every-request https://github.com/adshao/go-binance/blob/master/client.go#L137

from go-binance.

boskiv avatar boskiv commented on June 3, 2024

@0cv can you please provide an example how to use it with go-binance?

from go-binance.

0cv avatar 0cv commented on June 3, 2024

@boskiv
Something like

var weightMaxPerMinute int
var usedWeight int

// transport binance transport client
type transport struct {
	UnderlyingTransport http.RoundTripper
}

// RoundTrip implement http roundtrip
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
	resp, err := t.UnderlyingTransport.RoundTrip(req)
	if resp != nil && resp.Header != nil {
		usedWeight, _ = strconv.Atoi(resp.Header.Get("X-Mbx-Used-Weight-1m"))
	}

	return resp, err
}

func init() {
        binanceClient = binance.NewClient(apiKey, secretKey)
	c := http.Client{Transport: &transport{UnderlyingTransport: http.DefaultTransport}}
	binanceClient.HTTPClient = &c
}

Then before every call to the API, you exec checkRateLimit

func checkRateLimit() {
	if usedWeight > weightMaxPerMinute {
		now := time.Now()
		time.Sleep(time.Until(now.Add(time.Duration(60-now.Second()) * time.Second)))
	}
}

But from my experience, maybe I was doing something wrong or something was not correct on Binance side, but the practical limits were not really the same as the theoretical limits (1200 per minute). It's a long time I've not used it, but I think I had something around 900 or 1000 for weightMaxPerMinute

from go-binance.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.