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 circuit breaker job middleware for unstable services #36473

Merged
merged 1 commit into from
Mar 6, 2021
Merged

[8.x] Add circuit breaker job middleware for unstable services #36473

merged 1 commit into from
Mar 6, 2021

Conversation

paras-malhotra
Copy link
Contributor

It's fairly common to have HTTP calls to third party services in queued jobs. However, some third party services cannot be relied on and may be unstable.

This PR adds a circuit breaker job middleware for such use cases. Once the failures (job exceptions) reach a certain threshold, the circuit breaker trips (circuit opens) and all further calls are delayed until a certain time interval lapses.

To use this job middleware:

use Illuminate\Queue\Middleware\CircuitBreaker;

public function middleware()
{
    return [
        // Trip the circuit after 10 failed attempts in 10 minutes.
        new CircuitBreaker(10, 10),
    ];
}

By default, failed jobs (that don't exceed the threshold) are retried immediately (with an option to specify a delay) and the key for the rate limiter is the class name (with an option to override this to share a key between jobs that may be using the same service).

If this PR is accepted, I'll follow this up with another PR to implement this for Redis using the DurationLimiter.

@taylorotwell
Copy link
Member

Do you mind if I rename it to ThrottlesExceptions? Just seems to communicate a bit better what it does from the outside.

@paras-malhotra
Copy link
Contributor Author

@taylorotwell I borrowed the name from the circuit breaker pattern but I think ThrottlesExceptions conveys the purpose as well.

@taylorotwell taylorotwell merged commit 0ddc7ab into laravel:8.x Mar 6, 2021
@taylorotwell
Copy link
Member

OK. I have renamed it to ThrottlesExceptions and also added a when method for determining if the exception should be considered a throttable exception by the middleware. If not - it will just re-throw it so the normal exception job handling happens.

public function middleware()
{
    return [
        (new ThrottlesExceptions(5, 1))->when(fn ($e) => $e instanceof SomeException),
    ];
}

@paras-malhotra paras-malhotra deleted the circuit_breaker branch March 6, 2021 05:40
This was referenced Mar 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants