1515use Psr \Log \NullLogger ;
1616use Symfony \Component \HttpClient \Response \AsyncContext ;
1717use Symfony \Component \HttpClient \Response \AsyncResponse ;
18+ use Symfony \Component \HttpClient \Response \MockResponse ;
1819use Symfony \Component \HttpClient \Retry \ExponentialBackOff ;
1920use Symfony \Component \HttpClient \Retry \HttpCodeDecider ;
2021use Symfony \Component \HttpClient \Retry \RetryBackOffInterface ;
@@ -57,16 +58,14 @@ public function request(string $method, string $url, array $options = []): Respo
5758 return new AsyncResponse ($ this ->client , $ method , $ url , $ options , function (ChunkInterface $ chunk , AsyncContext $ context ) use ($ method , $ url , $ options , &$ retryCount ) {
5859 $ exception = null ;
5960 try {
60- // clone the chunk to let the original value of `$didthrow`
61- $ clonedChunk = clone $ chunk ;
62- if ($ clonedChunk ->isTimeout () || null !== $ clonedChunk ->getInformationalStatus ()) {
61+ if ($ chunk ->isTimeout () || null !== $ chunk ->getInformationalStatus ()) {
6362 yield $ chunk ;
6463
6564 return ;
6665 }
6766
6867 // only retry first chunk
69- if (!$ clonedChunk ->isFirst ()) {
68+ if (!$ chunk ->isFirst ()) {
7069 $ context ->passthru ();
7170 yield $ chunk ;
7271
@@ -76,20 +75,20 @@ public function request(string $method, string $url, array $options = []): Respo
7675 // catch TransportExceptionInterface to send it to strategy.
7776 }
7877
79- if ($ retryCount >= $ this ->maxRetries || !$ this ->decider ->shouldRetry ($ method , $ url , $ options , $ response = $ context ->getResponse ( ), $ exception )) {
78+ if ($ retryCount >= $ this ->maxRetries || !$ this ->decider ->shouldRetry ($ method , $ url , $ options , $ partialResponse = new MockResponse ( $ context ->getContent (), [ ' http_code ' => $ statusCode = $ context -> getStatusCode (), ' headers ' => $ headers = $ context -> getHeaders ()] ), $ exception )) {
8079 $ context ->passthru ();
8180 yield $ chunk ;
8281
8382 return ;
8483 }
8584
8685 $ context ->setInfo ('retry_count ' , $ retryCount );
87- $ response ->cancel ();
86+ $ context -> getResponse () ->cancel ();
8887
89- $ delay = $ this ->getDelayFromHeader ($ response ) ?? $ this ->strategy ->getDelay ($ retryCount , $ method , $ url , $ options , $ response = $ context -> getResponse () , $ exception );
88+ $ delay = $ this ->getDelayFromHeader ($ headers ) ?? $ this ->strategy ->getDelay ($ retryCount , $ method , $ url , $ options , $ partialResponse , $ exception );
9089 ++$ retryCount ;
9190
92- $ this ->logger ->info ('Error returned by the server. Retrying #{retryCount} using {delay} ms delay: ' .($ exception ? $ exception ->getMessage () : 'StatusCode: ' .$ response -> getStatusCode () ), [
91+ $ this ->logger ->info ('Error returned by the server. Retrying #{retryCount} using {delay} ms delay: ' .($ exception ? $ exception ->getMessage () : 'StatusCode: ' .$ statusCode ), [
9392 'retryCount ' => $ retryCount ,
9493 'delay ' => $ delay ,
9594 ]);
@@ -99,9 +98,9 @@ public function request(string $method, string $url, array $options = []): Respo
9998 });
10099 }
101100
102- private function getDelayFromHeader (ResponseInterface $ response ): ?int
101+ private function getDelayFromHeader (array $ headers ): ?int
103102 {
104- if (null !== $ after = $ response -> getHeaders ( false ) ['retry-after ' ][0 ] ?? null ) {
103+ if (null !== $ after = $ headers ['retry-after ' ][0 ] ?? null ) {
105104 if (is_numeric ($ after )) {
106105 return (int ) $ after * 1000 ;
107106 }
0 commit comments