Skip to content

Commit 4bf7292

Browse files
Fix rate limit (#1015)
* Rate limit fix 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 * Rename variable * Clean up --------- Co-authored-by: Riccardo Nava <[email protected]>
1 parent 14e0881 commit 4bf7292

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/Curl/MultiCurl.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MultiCurl extends BaseCurl
2424
private $rateLimit = null;
2525
private $rateLimitEnabled = false;
2626
private $rateLimitReached = false;
27-
private $maxRequests = null;
27+
private $maxRequestsPerInterval = null;
2828
private $interval = null;
2929
private $intervalSeconds = null;
3030
private $unit = null;
@@ -579,12 +579,12 @@ public function setRateLimit($rate_limit)
579579
'';
580580
if (!preg_match($rate_limit_pattern, $rate_limit, $matches)) {
581581
throw new \UnexpectedValueException(
582-
'rate limit must be formatted as $max_requests/$interval(s|m|h) ' .
582+
'rate limit must be formatted as $max_requests_per_interval/$interval(s|m|h) ' .
583583
'(e.g. "60/1m" for a maximum of 60 requests per 1 minute)'
584584
);
585585
}
586586

587-
$max_requests = (int)$matches['1'];
587+
$max_requests_per_interval = (int)$matches['1'];
588588
if ($matches['2'] === '') {
589589
$interval = 1;
590590
} else {
@@ -602,9 +602,9 @@ public function setRateLimit($rate_limit)
602602
$interval_seconds = $interval * 3600;
603603
}
604604

605-
$this->rateLimit = (string)$max_requests . '/' . (string)$interval . $unit;
605+
$this->rateLimit = (string)$max_requests_per_interval . '/' . (string)$interval . $unit;
606606
$this->rateLimitEnabled = true;
607-
$this->maxRequests = $max_requests;
607+
$this->maxRequestsPerInterval = $max_requests_per_interval;
608608
$this->interval = $interval;
609609
$this->intervalSeconds = $interval_seconds;
610610
$this->unit = $unit;
@@ -919,23 +919,22 @@ private function hasRequestQuota()
919919
// Calculate if there's request quota since ratelimiting is enabled.
920920
if ($this->rateLimitEnabled) {
921921
// Determine if the limit of requests per interval has been reached.
922-
if ($this->currentRequestCount >= $this->maxRequests) {
922+
if ($this->currentRequestCount >= $this->maxRequestsPerInterval) {
923923
$micro_time = microtime(true);
924924
$elapsed_seconds = $micro_time - $this->currentStartTime;
925925
if ($elapsed_seconds <= $this->intervalSeconds) {
926-
$this->rateLimitReached = true;
926+
// Rate limit reached.
927927
return false;
928-
} elseif ($this->rateLimitReached) {
929-
$this->rateLimitReached = false;
928+
} else {
929+
// Rate limit not reached. Rate limit interval has passed,
930+
// reset counters.
930931
$this->currentStartTime = $micro_time;
931932
$this->currentRequestCount = 0;
932933
}
933934
}
934-
935-
return true;
936-
} else {
937-
return true;
938935
}
936+
937+
return true;
939938
}
940939

941940
/**

tests/PHPCurlClass/PHPMultiCurlClassTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5020,7 +5020,7 @@ public function testSetRateLimitUnits()
50205020
);
50215021
$this->assertEquals(
50225022
$test['expected']['max_requests'],
5023-
\Helper\get_multi_curl_property_value($multi_curl, 'maxRequests')
5023+
\Helper\get_multi_curl_property_value($multi_curl, 'maxRequestsPerInterval')
50245024
);
50255025
$this->assertEquals(
50265026
$test['expected']['interval'],

0 commit comments

Comments
 (0)