Skip to content

Commit e3c513d

Browse files
Document new ThrottlesExceptions job middleware
1 parent f1c4847 commit e3c513d

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

queues.md

+56
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [Job Middleware](#job-middleware)
1111
- [Rate Limiting](#rate-limiting)
1212
- [Preventing Job Overlaps](#preventing-job-overlaps)
13+
- [Throttling Exceptions](#throttling-exceptions)
1314
- [Dispatching Jobs](#dispatching-jobs)
1415
- [Delayed Dispatching](#delayed-dispatching)
1516
- [Synchronous Dispatching](#synchronous-dispatching)
@@ -490,6 +491,61 @@ If you wish to immediately delete any overlapping jobs so that they will not be
490491

491492
> {note} The `WithoutOverlapping` middleware requires a cache driver that supports [locks](/docs/{{version}}/cache#atomic-locks). Currently, the `memcached`, `redis`, `dynamodb`, `database`, `file`, and `array` cache drivers support atomic locks.
492493
494+
<a name="throttling-exceptions"></a>
495+
### Throttling Exceptions
496+
497+
Laravel includes a `Illuminate\Queue\Middleware\ThrottlesExceptions` middleware that allows you to throttle exceptions. This is particularly useful for jobs that interact with third party services that are unstable.
498+
499+
Once the job exceptions reach a certain threshold, all further jobs with the same key are delayed until a certain time interval lapses. This is based off the circuit breaker design pattern.
500+
501+
For example, let's imagine you have a queued job that interacts with an unstable third party API. To throttle exceptions, you can return the `ThrottlesExceptions` middleware from your job's `middleware` method:
502+
503+
use Illuminate\Queue\Middleware\ThrottlesExceptions;
504+
505+
/**
506+
* Get the middleware the job should pass through.
507+
*
508+
* @return array
509+
*/
510+
public function middleware()
511+
{
512+
return [new ThrottlesExceptions(10, 10)];
513+
}
514+
515+
The first argument of the middleware is the maximum number of consecutive attempts and the second argument is the time interval. For the job above, if there are 10 failed attempts (exceptions) in 10 minutes, we want to wait for another 10 minutes before retrying or attempting a new job with the same "key".
516+
517+
By default, if the failure threshold is not met, the jobs are retried immediately. You may override this by using the third argument of the middleware constructor. For example, to retry the job after 1 minute:
518+
519+
use Illuminate\Queue\Middleware\ThrottlesExceptions;
520+
521+
/**
522+
* Get the middleware the job should pass through.
523+
*
524+
* @return array
525+
*/
526+
public function middleware()
527+
{
528+
return [new ThrottlesExceptions(10, 10, 1)];
529+
}
530+
531+
By default, the job class is used as the key. You may override this by providing the fourth argument of the middleware constructor:
532+
533+
use Illuminate\Queue\Middleware\ThrottlesExceptions;
534+
535+
/**
536+
* Get the middleware the job should pass through.
537+
*
538+
* @return array
539+
*/
540+
public function middleware()
541+
{
542+
return [new ThrottlesExceptions(10, 10, 1, 'custom-key')];
543+
}
544+
545+
This may be useful if you have multiple jobs interacting with the same third party service, and you wish to share a common throttling "bucket" for all such jobs.
546+
547+
> {tip} If you are using Redis, you may use the `Illuminate\Queue\Middleware\ThrottlesExceptionsWithRedis` middleware, which is fine-tuned for Redis and more efficient than the basic exception throttling middleware.
548+
493549
<a name="dispatching-jobs"></a>
494550
## Dispatching Jobs
495551

0 commit comments

Comments
 (0)