如何限制延迟作业以免激怒 Facebook API

发布于 2024-11-05 00:27:41 字数 185 浏览 0 评论 0原文

我正在构建一个将大量使用 Facebook 图形 API 的应用程序。我了解到他们的速率限制为每 600 秒 600 个请求。

我正在使用延迟作业进行所有后台处理。安排延迟作业以保持在 fb api 速率限制以下的好方法是什么?延迟作业是否有任何技巧,或者我是否需要构建一个单独的后台任务处理器才能不超过我的速率限制?

谢谢

I'm building an app that will be hitting the Facebook graph api a lot. I learned they have a rate limit of 600 requests every 600 seconds.

I'm using delayed job for all my background processing. What is a good way to schedule delayed job to stay under the fb api rate limit? Are there any tricks with delayed job or do I need to build a separate background task processor to not go over my rate limit?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国际总奸 2024-11-12 00:27:41

每 600 秒 600 个请求相当于平均每秒 1 个请求。

不是很快!

1) 根据您公司的规模和影响力,我会与 FB 进行调查,看看是否可以为您提高限额。

2)您可以坚持使用DelayedJob,无需重新发明轮子。您只需要更改调度程序即可。

在我的 DelayedJob 安装中,我使用“run_at”列不仅仅用于设置重试作业的时间,还使用它作为首先运行作业的时间。您还可以使用它来限制您的工作。

在 DelayedJob 文件 job.rb 中进行了更改:

# added run_at param
# eg   Delayed::Job.enqueue NewsletterJob.new('lorem ipsum...'), 0,
#                           Delayed::Job.db_time_now + 15.minutes
def self.enqueue(object, priority = 0, run_at = nil)
  unless object.respond_to?(:perform)
    raise ArgumentError, 'Cannot enqueue items which do not respond to perform' 
  end

  Job.create(:payload_object => object, :priority => priority,
    :run_at => run_at)    
end                    

为了实现您的目标,我将跟踪上次 FB api 调用排队的时间,并安排下一个调用的运行时间至少长一秒。

好处:您可以将其他非 FB 任务与 FB api 调用交错。

600 requests every 600 sec is 1 per sec on avg.

Not very fast!

1) Depending on your company's size and heft, I'd investigate with FB to see if you can get the limit raised for you.

2) You can stick with DelayedJob, no need to re-invent the wheel. You just need to change the scheduler.

In my DelayedJob installation, I use the "run_at" column for more than just setting the time to retry the jobs--I also use it as the time to run the job in the first place. You can also use it to throttle your jobs.

Changed in the DelayedJob file job.rb:

# added run_at param
# eg   Delayed::Job.enqueue NewsletterJob.new('lorem ipsum...'), 0,
#                           Delayed::Job.db_time_now + 15.minutes
def self.enqueue(object, priority = 0, run_at = nil)
  unless object.respond_to?(:perform)
    raise ArgumentError, 'Cannot enqueue items which do not respond to perform' 
  end

  Job.create(:payload_object => object, :priority => priority,
    :run_at => run_at)    
end                    

For your goal, I would keep track of the last time a FB api call was enqueued, and schedule the next one to run_at a time at least a sec greater.

Benefit: you would be able to interleave other, non-FB tasks, in with the FB api calls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文