question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

effective rate limiting

See original GitHub issue

I’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:open
  • Created 6 years ago
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
timmoltercommented, Jul 24, 2017

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?

1reaction
npomfretcommented, Jul 24, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found