From 21fee7649e1b48a7701b8ba860218741c2c3bcef Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 5 Mar 2021 17:04:38 -0600 Subject: [PATCH] rename class --- ...uitBreaker.php => ThrottlesExceptions.php} | 48 ++++++++++++++----- ...erTest.php => ThrottlesExceptionsTest.php} | 8 ++-- 2 files changed, 40 insertions(+), 16 deletions(-) rename src/Illuminate/Queue/Middleware/{CircuitBreaker.php => ThrottlesExceptions.php} (78%) rename tests/Integration/Queue/{CircuitBreakerTest.php => ThrottlesExceptionsTest.php} (94%) diff --git a/src/Illuminate/Queue/Middleware/CircuitBreaker.php b/src/Illuminate/Queue/Middleware/ThrottlesExceptions.php similarity index 78% rename from src/Illuminate/Queue/Middleware/CircuitBreaker.php rename to src/Illuminate/Queue/Middleware/ThrottlesExceptions.php index eb764a957561..edb3dd164d33 100644 --- a/src/Illuminate/Queue/Middleware/CircuitBreaker.php +++ b/src/Illuminate/Queue/Middleware/ThrottlesExceptions.php @@ -6,10 +6,10 @@ use Illuminate\Container\Container; use Throwable; -class CircuitBreaker +class ThrottlesExceptions { /** - * The maximum number of attempts allowed before the circuit is opened. + * The maximum number of attempts allowed before rate limiting applies. * * @var int */ @@ -36,6 +36,13 @@ class CircuitBreaker */ protected $key; + /** + * The callback that determines if rate limiting should apply. + * + * @var callable + */ + protected $whenCallback; + /** * The prefix of the rate limiter key. * @@ -86,6 +93,10 @@ public function handle($job, $next) $this->limiter->clear($jobKey); } catch (Throwable $throwable) { + if ($this->whenCallback && ! call_user_func($this->whenCallback, $throwable)) { + throw $throwable; + } + $this->limiter->hit($jobKey, $this->decayMinutes * 60); return $job->release($this->retryAfterMinutes * 60); @@ -93,27 +104,29 @@ public function handle($job, $next) } /** - * Set the prefix of the rate limiter key. + * Specify a callback that should determine if rate limiting behavior should apply. * - * @param string $prefix + * @param callable $callback * @return $this */ - public function withPrefix(string $prefix) + public function when(callable $callback) { - $this->prefix = $prefix; + $this->whenCallback = $callback; return $this; } /** - * Get the number of seconds that should elapse before the job is retried. + * Set the prefix of the rate limiter key. * - * @param string $key - * @return int + * @param string $prefix + * @return $this */ - protected function getTimeUntilNextRetry($key) + public function withPrefix(string $prefix) { - return $this->limiter->availableIn($key) + 3; + $this->prefix = $prefix; + + return $this; } /** @@ -124,6 +137,17 @@ protected function getTimeUntilNextRetry($key) */ protected function getKey($job) { - return md5($this->prefix.(empty($this->key) ? get_class($job) : $this->key)); + return $this->prefix.md5(empty($this->key) ? get_class($job) : $this->key); + } + + /** + * Get the number of seconds that should elapse before the job is retried. + * + * @param string $key + * @return int + */ + protected function getTimeUntilNextRetry($key) + { + return $this->limiter->availableIn($key) + 3; } } diff --git a/tests/Integration/Queue/CircuitBreakerTest.php b/tests/Integration/Queue/ThrottlesExceptionsTest.php similarity index 94% rename from tests/Integration/Queue/CircuitBreakerTest.php rename to tests/Integration/Queue/ThrottlesExceptionsTest.php index 977a00a76c0f..b51190e41654 100644 --- a/tests/Integration/Queue/CircuitBreakerTest.php +++ b/tests/Integration/Queue/ThrottlesExceptionsTest.php @@ -8,14 +8,14 @@ use Illuminate\Contracts\Queue\Job; use Illuminate\Queue\CallQueuedHandler; use Illuminate\Queue\InteractsWithQueue; -use Illuminate\Queue\Middleware\CircuitBreaker; +use Illuminate\Queue\Middleware\ThrottlesExceptions; use Mockery as m; use Orchestra\Testbench\TestCase; /** * @group integration */ -class CircuitBreakerTest extends TestCase +class ThrottlesExceptionsTest extends TestCase { protected function tearDown(): void { @@ -122,7 +122,7 @@ public function handle() public function middleware() { - return [new CircuitBreaker(2, 10, 0, 'test')]; + return [new ThrottlesExceptions(2, 10, 0, 'test')]; } } @@ -139,6 +139,6 @@ public function handle() public function middleware() { - return [new CircuitBreaker(2, 10, 0, 'test')]; + return [new ThrottlesExceptions(2, 10, 0, 'test')]; } }