Skip to content

Commit

Permalink
rename class
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Mar 5, 2021
1 parent 0ddc7ab commit 21fee76
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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.
*
Expand Down Expand Up @@ -86,34 +93,40 @@ 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);
}
}

/**
* 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;
}

/**
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -122,7 +122,7 @@ public function handle()

public function middleware()
{
return [new CircuitBreaker(2, 10, 0, 'test')];
return [new ThrottlesExceptions(2, 10, 0, 'test')];
}
}

Expand All @@ -139,6 +139,6 @@ public function handle()

public function middleware()
{
return [new CircuitBreaker(2, 10, 0, 'test')];
return [new ThrottlesExceptions(2, 10, 0, 'test')];
}
}

0 comments on commit 21fee76

Please sign in to comment.