调节/速率限制红宝石机械化

发布于 2025-01-04 05:51:59 字数 518 浏览 3 评论 0原文

我需要调节 Mechanize 实例与 API 连接的频率(每 2 秒一次,因此限制连接到该或更多)

所以这样:

instance.pre_connect_hooks << Proc.new { sleep 2 }

我原以为这会起作用,而且确实如此,但现在该类中的每个方法都在休眠持续 2 秒,就好像机械化实例被触摸并被告知保持 2 秒一样。我将尝试一个后连接挂钩,但显然我需要一些更复杂的东西,但目前我不知道是什么。

代码是更多解释,因此如果您有兴趣,请继续:https://github.com/blueblank/reddit_modbot ,否则我的问题涉及如何高效且有效地将 Mechanize 实例限制在 API 指定的特定时间范围内(超出该限制会导致请求丢失和禁止)。另外,我猜我也需要更好地将机械化实例集成到我的类中,对此的任何指示也将受到赞赏。

I need to regulate how often a Mechanize instance connects with an API (once every 2 seconds, so limit connections to that or more)

So this:

instance.pre_connect_hooks << Proc.new { sleep 2 }

I had thought this would work, and it sort of does BUT now every method in that class sleeps for 2 seconds, as if the mechanize instance is touched and told to hold 2 seconds. I'm going to try a post connect hook, but it is obvious I need something a bit more elaborate, but what I don't know what at this point.

Code is more explanation so if you are interested following along: https://github.com/blueblank/reddit_modbot, otherwise my question concerns how to efficiently and effectively rate limit a Mechanize instance to within a specific time frame specified by an API (where overstepping that limit results in dropped requests and bans). Also, I'm guessing I need to better integrate a mechanize instance to my class as well, any pointers on that appreciated as well.

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

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

发布评论

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

评论(2

拥抱我好吗 2025-01-11 05:51:59

每次连接时都会调用前连接钩子和后连接钩子,因此如果存在某种重定向,则可能会为一个请求触发多次。尝试history_added,它只被调用一次:

instance.history_added = Proc.new {sleep 2}

Pre and post connect hooks are called on every connect, so if there is some redirection it could trigger many times for one request. Try history_added which only gets called once:

instance.history_added = Proc.new {sleep 2}
永不分离 2025-01-11 05:51:59

我使用 SlowWeb 来限制对特定 URL 的调用。

require 'slowweb'
SlowWeb.limit('example.com', 10, 60)

在这种情况下,对 example.com 域的调用限制为每 60 秒 10 个请求。

I use SlowWeb to rate limit calls to a specific URL.

require 'slowweb'
SlowWeb.limit('example.com', 10, 60)

In this case calls to example.com domain are limited to 10 requests every 60 seconds.

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