处理 API 速率限制? [英] Dealing with API rate limits?

查看:62
本文介绍了处理 API 速率限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用,它被设置为每天对多个 API 进行一次预定调用.这非常有效,但我知道我正在调用的某些 API(例如 Twitter)有速率限制.随着我拨打的电话数量不断增加,有人可以推荐一种方法来限制我的电话,以便我可以每小时/分钟等发送 x 次吗?

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?

我找到了 Glutton Ratelimit gem,有人在用吗,有什么用吗?还有其他我应该看的吗?

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

推荐答案

如果您使用某种后台工作者来执行 API 调用,您可以重新安排任务在下一个时间段重新执行,当速率限制已重置.

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

虽然没有科学的解决方案,例如如果您尝试在一天内执行比速率限制允许的多得多的 API 调用,则每次可能会重新安排查询的风险.但在那之前,一些简单的事情可能会奏效!

No scientific solution though, there's e.g. 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!

这篇关于处理 API 速率限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆