Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rate limit fix #863

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Rate limit fix #863

wants to merge 1 commit into from

Conversation

ricnava00
Copy link

If $elapsed_seconds > $this->intervalSeconds the first time hasRequestQuota() is called, rateLimitReached is never set to true and the limit is not enforced This can happen if the rate limit is set in a callback function some time after starting

I completely removed $rateLimitReached since its only use was in the removed condition check

Alternatively, it should be possible to reinitialize currentStartTime when setRateLimit is called, but I still don't really see the usefulness of resetting the timer only when the limit was reached in the previous interval

If $elapsed_seconds > $this->intervalSeconds the first time hasRequestQuota() is called, rateLimitReached is never set to true and the limit is not enforced
This can happen if the rate limit is set in a callback function some time after starting

I completely removed $rateLimitReached since its only use was in the removed condition check

Alternatively, it should be possible to reinitialize currentStartTime when setRateLimit is called, but I still don't really see the usefulness of resetting the timer only when the limit was reached in the previous interval
@zachborboa
Copy link
Member

Hi. I don't believe the rate limit is (currently) intended to be set after starting requests.

Here's an example of how setRateLimit can be used:

$multi_curl = new MultiCurl();
$multi_curl->setRateLimit('60/1m');

$multi_curl->addGet('https://httpbin.org/ip');
// etc.

$multi_curl->start();

What's the use case for modifying the rate limit in a callback?

@ricnava00
Copy link
Author

I use it like this

$multi_curl=new MultiCurl();
//$multi_curl->error(...);
//$multi_curl->setRetry(...);
//Other shared settings
foreach(["fast","slow"] as $site)
{
	for($n=0;$n<10;$n++)
	{
		$ch=$multi_curl->addGet("http://".$site."-site.com/?page="+$n);
		if($n==0&&$site=="slow")
		{
			$ch->beforeSend(function() use ($multi_curl)
			{
				$multi_curl->setRateLimit('60/1m');
			});
		}
	}
}
$multi_curl->start();

If I had to call setRateLimit while multi_curl is not running I would either have to create another MultiCurl, copy all the callbacks, decide which one to use in the loop, and start them one after the other, or I would have to duplicate the loop for the fast and slow sites and set the rate limit between the two

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants