Comments (7)
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.
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.
@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
Line 217 in 2f14b48
from go-binance.
@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.
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.
@0cv can you please provide an example how to use it with go-binance?
from go-binance.
@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)
- how to get permission? HOT 2
- Crypto Box? HOT 1
- <APIError> code=-1106, msg=Parameter 'reduceonly' sent when not required. HOT 1
- how to keep listenKey alive? HOT 1
- when listenKey expired get error:json: cannot unmarshal string into Go struct field WsUserDataEvent.E of type int64 HOT 2
- can not get balance
- unexpected end of JSON input
- is threre subToMaster or masterToSub method?
- is there demo code to use subtoSub?
- something wrong happened when I get account asset? HOT 3
- [feat] support biance non-global HOT 2
- Support Query Margin Available Inventory
- Please do a new release HOT 10
- How to get x-mbx-used-weight-1m? HOT 2
- binance.WebsocketTimeout = time.Second * 5 Not working !
- Support for ed25519 secret/signing
- [error] msg=Invalid API-key, IP, or permissions for action. HOT 1
- [info] how to increase precision
- [Question] Trade service for delivery (futures coin) to fetch recent trades
- [issue] WsBLVTKlineServe is not returning anything
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-binance.