Skip to content

adjust decider to not retry client errors #125

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

Merged
merged 1 commit into from
Dec 9, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.0 (unreleased)

### Changed
- RetryPlugin will no longer retry requests when the response failed with a HTTP code < 500.
- Abstract method `HttpClientPool::chooseHttpClient()` has now an explicit return type (`Http\Client\Common\HttpClientPoolItem`)
- Interface method `Plugin::handleRequest(...)` has now an explicit return type (`Http\Promise\Promise`)

Expand Down
22 changes: 22 additions & 0 deletions spec/Plugin/RetryPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ public function it_throws_exception_on_multiple_exceptions(RequestInterface $req
$promise->shouldThrow($exception2)->duringWait();
}

public function it_does_not_retry_client_errors(RequestInterface $request, ResponseInterface $response)
{
$exception = new Exception\HttpException('Exception', $request->getWrappedObject(), $response->getWrappedObject());

$seen = false;
$next = function (RequestInterface $receivedRequest) use ($request, $exception, &$seen) {
if (!Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) {
throw new \Exception('Unexpected request received');
}
if ($seen) {
throw new \Exception('This should only be called once');
}
$seen = true;

return new HttpRejectedPromise($exception);
};

$promise = $this->handleRequest($request, $next, function () {});
$promise->shouldReturnAnInstanceOf(HttpRejectedPromise::class);
$promise->shouldThrow($exception)->duringWait();
}

public function it_returns_response_on_second_try(RequestInterface $request, ResponseInterface $response)
{
$exception = new Exception\NetworkException('Exception 1', $request->getWrappedObject());
Expand Down
6 changes: 4 additions & 2 deletions src/Plugin/RetryPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Http\Client\Common\Plugin;
use Http\Client\Exception;
use Http\Client\Exception\HttpException;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -56,7 +57,8 @@ public function __construct(array $config = [])
$resolver->setDefaults([
'retries' => 1,
'decider' => function (RequestInterface $request, Exception $e) {
return true;
// do not retry client errors
return !$e instanceof HttpException || $e->getCode() >= 500;
},
'delay' => __CLASS__.'::defaultDelay',
]);
Expand Down Expand Up @@ -101,7 +103,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
$time = call_user_func($this->delay, $request, $exception, $this->retryStorage[$chainIdentifier]);
usleep($time);

// Retry in synchrone
// Retry synchronously
++$this->retryStorage[$chainIdentifier];
$promise = $this->handleRequest($request, $next, $first);

Expand Down