Dealing with API rate limits?

I've an app that's set up to make scheduled calls to a number of APIs once a day. This works very nicely but i'm aware that some of the APIs i'm calling (Twitter for example) have a rate limit. As the number of calls i'm making is set to continually grow, can anyone recommend a way to throttle my calls so I can send in bursts of x per hour/minute etc?

I've found the Glutton Ratelimit gem, is anyone using this and is it any good? Are there others I should be looking at?


If you're using some kind of background worker to perform your API calls, you could reschedule the task to be reperformed in the next time slot, when the rate limits have been reset.

class TwitterWorker
  include Sidekiq::Worker

  def perform(status_id)
    status = Twitter.status(status_id)
    # ...

  rescue Twitter::Error::TooManyRequests
    # Reschedule the query to be performed in the next time slot
    TwitterWorker.perform_in(15.minutes, status_id)
  end
end

No scientific solution though, there's eg the risk that a query might be rescheduled each time if you try to perform much more API calls in a day than the rate limit allows for. But until then, something easy might do the trick!


Another solution is to buy proxies which allow you to send request with different IP addresses

Use standard http lib http://ruby-doc.org/stdlib-2.0/libdoc/net/http/rdoc/Net/HTTP.html#method-c-Proxy

I am not sure that you will not be blocked but maybe it is worth to try. Randomly choosen IP should increase your limits


Unless you're making concurrent requests there's not much to it.

  • Figure out how much delay you need per request
  • Check the time before the request, subtract from the time after the request and sleep the rest.
  • With concurrent requests you can be more accurate, I once blogged about that here

    链接地址: http://www.djcxy.com/p/71490.html

    上一篇: 由于Visual Studio使用xml编辑器打开它们,因此无法运行加载测试

    下一篇: 处理API速率限制?