简体   繁体   English

如何为Resque队列添加速率限制?

[英]How to add a rate limit to a Resque queue?

So I have a Resque worker that calls an API, the issue is the API has a rate limit of 2 requests per second. 因此,我有一个调用API的Resque worker,问题是该API的速率限制为每秒2个请求。

Is there a way to add a delay between each job processed in a specific queue? 有没有办法在特定队列中处理的每个作业之间增加延迟?

PS the queue could have thousands of pending jobs. PS队列中可能有数千个待处理的作业。

Why not sleep for a given amount of time at the end of the process? 为什么在过程结束时不睡一段给定的时间? Well, perhaps you want your resque worker to be doing something useful instead. 好吧,也许您希望您的老员工做一些有用的事情。 In CPU time, half a second is a lot of time - you could have done something useful there, like process a job from another queue that's not rate limited. 在CPU时间中,半秒是很多时间-您可以在那里做一些有用的事情,例如从另一个不受速率限制的队列中处理作业。

I have this same problem myself, so I'm motivated to find a solution. 我本人也遇到同样的问题,因此我很想找到解决方案。 It seems like there are two easy-ish ways to do it. 似乎有两种简单的方法可以做到这一点。 The first idea is to use resque scheduler and pre-compute the time to run the job at before inserting it. 第一个想法是使用resque调度程序并在插入作业之前预先计算运行该作业的时间。 This seems error-prone to me. 这对我来说似乎容易出错。 The second is to use a gem like https://github.com/flyerhzm/resque-restriction (disclaimer: just found it through some googling. haven't used it yet) and rate-limit as you pull jobs off the queue. 第二种是使用https://github.com/flyerhzm/resque-restriction之类的宝石(免责声明:只是通过某种谷歌搜索找到的。尚未使用)和速率限制,因为您将工作队列中拉出。 Seems like a robust solution in theory. 理论上似乎是一个可靠的解决方案。 Note that if you can't execute the job yet, it never comes off the queue, so you'll pull something else instead - much more efficient use of your workers. 请注意,如果您仍无法执行该作业,则它永远不会脱离队列,因此您将撤回其他事项-更有效地利用您的工人。

根据我的评论,我建议在每种Resque process方法结束时只执行给定的sleep时间(秒)。

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

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