调节/限速红宝石机械化 [英] Regulating / rate limiting ruby mechanize

查看:90
本文介绍了调节/限速红宝石机械化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

所以这个:

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

我曾以为这会起作用,但现在该类中的每个方法都会休眠2秒钟,就像机械化实例被触摸并告诉其保持2秒钟一样.我将尝试使用post connect钩子,但是很明显,我需要更详细的说明,但是此时我不知道要做什么.

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.

代码是更多的解释,因此如果您有兴趣,请关注以下内容: https://github.com/blueblank/reddit_modbot ,否则我的问题是如何在API指定的特定时间范围内有效且有效地将Mechanize实例的速率限制在一定的范围内(如果超出限制,将导致请求和禁令被丢弃).另外,我猜想我还需要更好地将机械化实例集成到我的类中,对此上的所有指针也应如此.

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.

推荐答案

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

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}

这篇关于调节/限速红宝石机械化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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