effective rate limiting
See original GitHub issueI’m finding that I’m exceeding rate limits occasionally. Has anyone got an effective way of dealing with them?
I see there s a nice little RateLimiter class in guava. But i think to use it properly it has to be baked in at the lowest level where the RestProxyFactory is invoked. It needs to be applied at the request level, and shared across the AccountService, TradeService and MarketDataService. Wrapping the services in another rate limiting proxy might work. Something like this:
protected TheRockAccountServiceRaw(Exchange exchange) {
super(exchange);
ExchangeSpecification spec = exchange.getExchangeSpecification();
this.apiKey = spec.getApiKey();
this.signatureCreator = new TheRockDigest(spec.getSecretKey());
TheRockAuthenticated theApi = RestProxyFactory.createProxy(TheRockAuthenticated.class, spec.getSslUri());
RateLimiter rateLimiter = spec.rateLimiter();
if(rateLimiter != null) {
this.theRockAuthenticated = rateLimit(theApi, rateLimiter, TheRockAuthenticated.class);
} else {
this.theRockAuthenticated = theApi;
}
}
private static <API> API rateLimit(API theApi, RateLimiter rateLimiter, Class<API> theClass) {
return (API) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{theClass}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
rateLimiter.acquire();
return method.invoke(theApi, args);
}
});
}
...
@timmolter thoughts?
Issue Analytics
- State:
- Created 6 years ago
- Comments:13 (9 by maintainers)
Top Results From Across the Web
Rate-limiting strategies and techniques - Google Cloud
Rate limiting is generally put in place as a defensive measure for services. Shared services need to protect themselves from excessive use— ...
Read more >What is Rate Limiting | Types & Algorithms - Imperva
Rate limiting is a technique to restrict the number of requests made to network resources at one time. Learn how it works.
Read more >Rate limiting - Wikipedia
In computer networks, rate limiting is used to control the rate of requests sent or received by a network interface controller.
Read more >What is Rate Limiting? Meaning & Definition - Wallarm
Rate limiting in API is setting a limit on traffic exchange and API usage to ensure that the system is not overloaded.
Read more >Rate Limiting: A Vital Tool for Modern Cyber Security
Rate limiting is the restriction of traffic based on the frequency of requests arriving from a specific traffic source. To enforce rate-limit restrictions,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yeah, you’re right. I can see integrating something like this into RESCU in a way similar to the nonce factory, where different exchanges have different implementations of rate limiting logic. @mmazi what do you think?
A reason to do it at this base level (once an appropriate model is in place) is that some implementations make multiple API calls for a single high level method call. So putting your rate limiter in your application won’t work as effectively.