Skip to content
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
13 changes: 7 additions & 6 deletions src/Interceptor/RetryRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,24 @@ public function request(
): Response {
$attempt = 1;

do {
while (true) {
$clonedRequest = clone $request;

try {
return $httpClient->request($request, $cancellation);
} catch (HttpException $exception) {
if ($request->isIdempotent() || $request->isUnprocessed()) {
if (($request->isIdempotent() || $request->isUnprocessed()) && $attempt++ <= $this->retryLimit) {
// Request was deemed retryable by connection, so carry on.
$request = $clonedRequest;

// A trace spans every fiber resumed in the same tick: don't hold it during the next attempt.
unset($exception);

continue;
}

throw $exception;
}
} while ($attempt++ <= $this->retryLimit);

/** @psalm-suppress PossiblyUndefinedVariable */
throw $exception;
}
}
}
60 changes: 41 additions & 19 deletions test/Interceptor/RetryRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,60 @@
namespace Amp\Http\Client\Interceptor;

use Amp\Cancellation;
use Amp\Http\Client\Connection\Stream;
use Amp\Http\Client\DelegateHttpClient;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\NetworkInterceptor;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\NullCancellation;
use Amp\PHPUnit\AsyncTestCase;

class RetryRequestsTest extends InterceptorTest
class RetryRequestsTest extends AsyncTestCase
{
public function testRetry(): void
public function testFailedAttemptIsReleasedBeforeTheNextOne(): void
{
$this->givenApplicationInterceptor(new AddRequestHeader('foo', 'before'));
$this->givenApplicationInterceptor(new RetryRequests(2));
$this->givenApplicationInterceptor(new AddRequestHeader('foo', 'after'));
$this->givenNetworkInterceptor(new class implements NetworkInterceptor {
public function requestViaNetwork(Request $request, Cancellation $cancellation, Stream $stream): Response
$client = new class implements DelegateHttpClient {
/** @var list<bool> */
public array $previousAttemptAlive = [];

private ?\WeakReference $previousAttempt = null;

public function request(Request $request, Cancellation $cancellation): Response
{
static $i = 0;
$this->previousAttemptAlive[] = $this->previousAttempt?->get() !== null;

if (++$i === 1) {
throw new HttpException('Failed');
}
$exception = new HttpException('Request failed');
$this->previousAttempt = \WeakReference::create($exception);

return $stream->request($request, $cancellation);
throw $exception;
}
});
};

$interceptor = new RetryRequests(2);

try {
$interceptor->request(new Request('http://localhost'), new NullCancellation(), $client);
self::fail('Expected the request to fail');
} catch (HttpException) {
self::assertSame([false, false, false], $client->previousAttemptAlive);
}
}

$request = new Request('http://example.com');
public function testLastFailureIsThrownAfterRetryLimit(): void
{
$client = new class implements DelegateHttpClient {
public int $attempts = 0;

public function request(Request $request, Cancellation $cancellation): Response
{
throw new HttpException('Attempt ' . ++$this->attempts);
}
};

$this->whenRequestIsExecuted($request);
$interceptor = new RetryRequests(2);

$this->thenRequestHasHeader('foo', 'before', 'after');
$this->expectException(HttpException::class);
$this->expectExceptionMessage('Attempt 3');

self::assertNotSame($request, $this->getRequest());
$interceptor->request(new Request('http://localhost'), new NullCancellation(), $client);
}
}
Loading