简体   繁体   English

处理 API 速率限制?

[英]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.我有一个应用程序,设置为每天一次对多个 API 进行预定调用。 This works very nicely but i'm aware that some of the APIs i'm calling (Twitter for example) have a rate limit.这工作得很好,但我知道我调用的一些 API(例如 Twitter)有速率限制。 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?随着我拨打的电话数量不断增加,任何人都可以推荐一种方法来限制我的电话,以便我可以每小时/分钟发送 x 等突发事件吗?

I've found the Glutton Ratelimit gem, is anyone using this and is it any good?我找到了Glutton Ratelimit gem,有人在用它吗?它有什么用吗? 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.如果您使用某种后台工作程序来执行 API 调用,您可以重新安排任务以在下一个时间段重新执行,此时速率限制已被重置。

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.但是,没有科学的解决方案,例如,如果您尝试在一天内执行比速率限制允许的更多的 API 调用,则可能会每次重新安排查询的风险。 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另一种解决方案是购买允许您使用不同 IP 地址发送请求的代理

Use standard http lib http://ruby-doc.org/stdlib-2.0/libdoc/net/http/rdoc/Net/HTTP.html#method-c-Proxy使用标准 http 库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随机选择的 IP 应该会增加您的限制

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.检查请求之前的时间,从请求之后的时间中减去,其余的sleep

With concurrent requests you can be more accurate, I once blogged about that here使用并发请求,您可以更准确, 我曾经在这里写过博客

I know this is an old question, but wanted to mention something in case it helps others with the same question.我知道这是一个老问题,但想提一点,以防其他人遇到同样的问题。

If the work can be queued to jobs using resque, then you could use the gem I've just released which pauses a queue when you hit a rate_limit - and unpauses it some time later.如果可以使用 resque 将工作排队到作业,那么您可以使用我刚刚发布的 gem,它会在您达到 rate_limit 时暂停队列 - 并在一段时间后取消暂停。

https://github.com/pavoni/resque-rate_limited_queue https://github.com/pavoni/resque-rate_limited_queue

shopify 的 gem ruby ​​-limiter 似乎很棒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM