Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add dontRelease option to RateLimited and RateLimitedWithRedis job middleware #35010

Merged
merged 1 commit into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Illuminate/Queue/Middleware/RateLimited.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class RateLimited
*/
protected $limiterName;

/**
* Indicates if the job should be released if the limit is exceeded.
*
* @var bool
*/
public $shouldRelease = true;

/**
* Create a new middleware instance.
*
Expand Down Expand Up @@ -68,6 +75,18 @@ public function handle($job, $next)
);
}

/**
* Do not release the job back to the queue if limit is exceeded.
*
* @return $this
*/
public function dontRelease()
{
$this->shouldRelease = false;

return $this;
}

/**
* Handle a rate limited job.
*
Expand All @@ -80,7 +99,9 @@ protected function handleJob($job, $next, array $limits)
{
foreach ($limits as $limit) {
if ($this->limiter->tooManyAttempts($limit->key, $limit->maxAttempts)) {
return $job->release($this->getTimeUntilNextRetry($limit->key));
return $this->shouldRelease
? $job->release($this->getTimeUntilNextRetry($limit->key))
: false;
}

$this->limiter->hit($limit->key, $limit->decayMinutes * 60);
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Queue/Middleware/RateLimitedWithRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ protected function handleJob($job, $next, array $limits)
{
foreach ($limits as $limit) {
if ($this->tooManyAttempts($limit->key, $limit->maxAttempts, $limit->decayMinutes)) {
return $job->release($this->getTimeUntilNextRetry($limit->key));
return $this->shouldRelease
? $job->release($this->getTimeUntilNextRetry($limit->key))
: false;
}
}

Expand Down
39 changes: 39 additions & 0 deletions tests/Integration/Queue/RateLimitedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ public function testRateLimitedJobsAreNotExecutedOnLimitReached()
$this->assertJobWasReleased(RateLimitedTestJob::class);
}

public function testRateLimitedJobsCanBeSkippedOnLimitReached()
{
$rateLimiter = $this->app->make(RateLimiter::class);

$rateLimiter->for('test', function ($job) {
return Limit::perHour(1);
});

$this->assertJobRanSuccessfully(RateLimitedDontReleaseTestJob::class);
$this->assertJobWasSkipped(RateLimitedDontReleaseTestJob::class);
}

public function testJobsCanHaveConditionalRateLimits()
{
$rateLimiter = $this->app->make(RateLimiter::class);
Expand Down Expand Up @@ -105,6 +117,25 @@ protected function assertJobWasReleased($class)

$this->assertFalse($class::$handled);
}

protected function assertJobWasSkipped($class)
{
$class::$handled = false;
$instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);

$job = m::mock(Job::class);

$job->shouldReceive('hasFailed')->once()->andReturn(false);
$job->shouldReceive('isReleased')->once()->andReturn(false);
$job->shouldReceive('isDeletedOrReleased')->once()->andReturn(false);
$job->shouldReceive('delete')->once();

$instance->call($job, [
'command' => serialize($command = new $class),
]);

$this->assertFalse($class::$handled);
}
}

class RateLimitedTestJob
Expand Down Expand Up @@ -139,3 +170,11 @@ public function isAdmin()
return false;
}
}

class RateLimitedDontReleaseTestJob extends RateLimitedTestJob
{
public function middleware()
{
return [(new RateLimited('test'))->dontRelease()];
}
}
41 changes: 41 additions & 0 deletions tests/Integration/Queue/RateLimitedWithRedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ public function testRateLimitedJobsAreNotExecutedOnLimitReached()
$this->assertJobWasReleased($testJob);
}

public function testRateLimitedJobsCanBeSkippedOnLimitReached()
{
$rateLimiter = $this->app->make(RateLimiter::class);

$testJob = new RedisRateLimitedDontReleaseTestJob;

$rateLimiter->for($testJob->key, function ($job) {
return Limit::perMinute(1);
});

$this->assertJobRanSuccessfully($testJob);
$this->assertJobWasSkipped($testJob);
}

public function testJobsCanHaveConditionalRateLimits()
{
$rateLimiter = $this->app->make(RateLimiter::class);
Expand Down Expand Up @@ -134,6 +148,25 @@ protected function assertJobWasReleased($testJob)

$this->assertFalse($testJob::$handled);
}

protected function assertJobWasSkipped($testJob)
{
$testJob::$handled = false;
$instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);

$job = m::mock(Job::class);

$job->shouldReceive('hasFailed')->once()->andReturn(false);
$job->shouldReceive('isReleased')->once()->andReturn(false);
$job->shouldReceive('isDeletedOrReleased')->once()->andReturn(false);
$job->shouldReceive('delete')->once();

$instance->call($job, [
'command' => serialize($testJob),
]);

$this->assertFalse($testJob::$handled);
}
}

class RedisRateLimitedTestJob
Expand Down Expand Up @@ -175,3 +208,11 @@ public function isAdmin()
return false;
}
}

class RedisRateLimitedDontReleaseTestJob extends RedisRateLimitedTestJob
{
public function middleware()
{
return [(new RateLimitedWithRedis($this->key))->dontRelease()];
}
}