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] Allow passing when callback to Http client retry method #38531

Merged
merged 5 commits into from
Aug 25, 2021
Merged

[8.x] Allow passing when callback to Http client retry method #38531

merged 5 commits into from
Aug 25, 2021

Conversation

dmason30
Copy link
Contributor

@dmason30 dmason30 commented Aug 24, 2021

TL;DR
A little papercut I have, this adds support for passing a $when callback to the Http::retry() method which is already supported by the retry helper that is used to handle the Http client retries. There are no existing tests for Http::retry() so this PR does not contain any.

When Callback already exists in the retry helper
I have just noticed whilst writing this that the $when callback isn't actually documented on the retry helper docs (https://laravel.com/docs/8.x/helpers#method-retry). It is a very useful feature where you can decide whether to continue retrying or to throw the current exception by returning a boolean from the callback:

$response = retry(
    3, // <-- Max tries
    fn () => SomeThrowableAction::make()->execute(), // <-- Code to execute
    400, // <-- Delay between attempts
    fn ($e) => $e instanceof SomeRetryException, // <-- The when callback
 );

Use with Http client
This PR adds the $when support to the Http client, which can be useful if as seen in the example you only want to retry when it gets a connection failure:

$response = \Http::retry(
    3,
    400,
    fn ($e) => $e instanceof ConnectionException, // <-- Added when callback support
 )->get(...);

@taylorotwell taylorotwell merged commit 313b220 into laravel:8.x Aug 25, 2021
@dmason30 dmason30 deleted the http-client-retry-when branch August 25, 2021 13:05
victorvilella pushed a commit to cdsistemas/framework that referenced this pull request Oct 12, 2021
…l#38531)

* Allow passing when callback to http client retry method

* linting

* add callable type

* update phpdoc on factory and facade

* Update PendingRequest.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
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.

None yet

3 participants