diff --git a/src/Interceptor/RetryRequests.php b/src/Interceptor/RetryRequests.php index efc90191..a49ad85e 100644 --- a/src/Interceptor/RetryRequests.php +++ b/src/Interceptor/RetryRequests.php @@ -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; + } } } diff --git a/test/Interceptor/RetryRequestsTest.php b/test/Interceptor/RetryRequestsTest.php index 0edf7cd3..5fe4c09f 100644 --- a/test/Interceptor/RetryRequestsTest.php +++ b/test/Interceptor/RetryRequestsTest.php @@ -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 */ + 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); } }