Skip to content

Commit

Permalink
formatting and fix key usage
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Mar 9, 2021
1 parent a4678ce commit 37e48ba
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
52 changes: 38 additions & 14 deletions src/Illuminate/Queue/Middleware/ThrottlesExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

class ThrottlesExceptions
{
/**
* The developer specified key that the rate limiter should use.
*
* @var string
*/
protected $key;

/**
* The maximum number of attempts allowed before rate limiting applies.
*
Expand All @@ -27,14 +34,7 @@ class ThrottlesExceptions
*
* @var int
*/
protected $retryAfterMinutes;

/**
* The rate limiter key.
*
* @var string
*/
protected $key;
protected $retryAfterMinutes = 0;

/**
* The callback that determines if rate limiting should apply.
Expand All @@ -48,7 +48,7 @@ class ThrottlesExceptions
*
* @var string
*/
protected $prefix = 'circuit_breaker:';
protected $prefix = 'laravel_throttles_exceptions:';

/**
* The rate limiter instance.
Expand All @@ -62,15 +62,13 @@ class ThrottlesExceptions
*
* @param int $maxAttempts
* @param int $decayMinutes
* @param int $retryAfterMinutes
* @param string $key
* @return void
*/
public function __construct($maxAttempts = 10, $decayMinutes = 10, $retryAfterMinutes = 0, string $key = '')
public function __construct($maxAttempts = 10, $decayMinutes = 10)
{
$this->maxAttempts = $maxAttempts;
$this->decayMinutes = $decayMinutes;
$this->retryAfterMinutes = $retryAfterMinutes;
$this->key = $key;
}

/**
Expand Down Expand Up @@ -129,6 +127,19 @@ public function withPrefix(string $prefix)
return $this;
}

/**
* Specify the number of seconds a job should be delayed when it is released (before it has reached its max exceptions).
*
* @param int $backoff
* @return $this
*/
public function backoff($backoff)
{
$this->retryAfterMinutes = $backoff;

return $this;
}

/**
* Get the cache key associated for the rate limiter.
*
Expand All @@ -137,7 +148,20 @@ public function withPrefix(string $prefix)
*/
protected function getKey($job)
{
return $this->prefix.md5(empty($this->key) ? get_class($job) : $this->key);
return $this->key ? $this->prefix.$this->key : $this->prefix.$job->job->uuid();
}

/**
* Set the value that the rate limiter should be keyed by.
*
* @param string $key
* @return $this
*/
public function by($key)
{
$this->key = $key;

return $this;
}

/**
Expand Down
7 changes: 5 additions & 2 deletions tests/Integration/Queue/ThrottlesExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ protected function assertJobWasReleasedImmediately($class)
$job->shouldReceive('release')->with(0)->once();
$job->shouldReceive('isReleased')->andReturn(true);
$job->shouldReceive('isDeletedOrReleased')->once()->andReturn(true);
$job->shouldReceive('uuid')->andReturn('simple-test-uuid');

$instance->call($job, [
'command' => serialize($command = new $class),
Expand All @@ -79,6 +80,7 @@ protected function assertJobWasReleasedWithDelay($class)
})->once();
$job->shouldReceive('isReleased')->andReturn(true);
$job->shouldReceive('isDeletedOrReleased')->once()->andReturn(true);
$job->shouldReceive('uuid')->andReturn('simple-test-uuid');

$instance->call($job, [
'command' => serialize($command = new $class),
Expand All @@ -98,6 +100,7 @@ protected function assertJobRanSuccessfully($class)
$job->shouldReceive('isReleased')->andReturn(false);
$job->shouldReceive('isDeletedOrReleased')->once()->andReturn(false);
$job->shouldReceive('delete')->once();
$job->shouldReceive('uuid')->andReturn('simple-test-uuid');

$instance->call($job, [
'command' => serialize($command = new $class),
Expand All @@ -122,7 +125,7 @@ public function handle()

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

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

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

0 comments on commit 37e48ba

Please sign in to comment.