Skip to content

[13.x] Allow passing a Closure to ThrottlesExceptions middleware#60103

Merged
taylorotwell merged 1 commit into
laravel:13.xfrom
cosmastech:throttles-exception-backoff
May 13, 2026
Merged

[13.x] Allow passing a Closure to ThrottlesExceptions middleware#60103
taylorotwell merged 1 commit into
laravel:13.xfrom
cosmastech:throttles-exception-backoff

Conversation

@cosmastech
Copy link
Copy Markdown
Contributor

@cosmastech cosmastech commented May 13, 2026

I noticed a clanker put up some code today like this:

public function middleware(): array
{
    return [
        (new ThrottlesExceptions(maxAttempts: 10, decaySeconds: 300))
            ->byJob()
            ->when(
                fn (Throwable $throwable): bool => $throwable instanceof ExternalAPIException 
                    && $throwable->isRateLimited()
            )
            ->backoff(2),
    ];
}

The ExternalAPIException has a property for reading the Retry-After header, and it would be great to be able to employ that, like so:

    return [
        (new ThrottlesExceptions(maxAttempts: 10, decaySeconds: 300))
            ->byJob()
            ->when(
                fn (Throwable $throwable): bool => $throwable instanceof ExternalAPIException 
                    && $throwable->isRateLimited()
            )
            ->backoff(fn (Throwable $throwable) => $throwable instanceof ExternalAPIException 
                ? $throwable->retryAfter() / 60
                : 2
            ),
    ];

@taylorotwell taylorotwell merged commit 18ddccd into laravel:13.x May 13, 2026
54 checks passed
@cosmastech cosmastech deleted the throttles-exception-backoff branch May 13, 2026 14:18
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