bug #38353 [RateLimiter] Call all compound limiters on failure and added IO blocking (wouterj)

This PR was merged into the 5.2-dev branch.

Discussion
----------

[RateLimiter] Call all compound limiters on failure and added IO blocking

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Two small improvements that I missed in #38257:

* Added `wait()` method, to be able to do logic like this:
   ```php
   while (!($limit = $limiter->consume())->isAccepted()) {
       $limit->wait();
   }
   ```
* Fixed the `CompoundLimiter` to always call all limiters (even when one already blocks). This was the original behavior of the compound limiter and imho the best behavior (at least, it's always consistent and doesn't rely on the order of limiters - which is unsorted).

Commits
-------

0279f88e6c Call all compound limiters on failure and added IO blocking
This commit is contained in:
Fabien Potencier 2020-09-30 13:43:13 +02:00
commit f06f2f018e
2 changed files with 5 additions and 4 deletions

View File

@ -37,10 +37,6 @@ final class CompoundLimiter implements LimiterInterface
foreach ($this->limiters as $limiter) {
$limit = $limiter->consume($tokens);
if (0 === $limit->getRemainingTokens()) {
return $limit;
}
if (null === $minimalLimit || $limit->getRemainingTokens() < $minimalLimit->getRemainingTokens()) {
$minimalLimit = $limit;
}

View File

@ -43,4 +43,9 @@ class Limit
{
return $this->availableTokens;
}
public function wait(): void
{
sleep(($this->retryAfter->getTimestamp() - time()) * 1e6);
}
}