15
15
use Psr \Log \NullLogger ;
16
16
use Symfony \Component \HttpClient \Response \AsyncContext ;
17
17
use Symfony \Component \HttpClient \Response \AsyncResponse ;
18
+ use Symfony \Component \HttpClient \Response \MockResponse ;
18
19
use Symfony \Component \HttpClient \Retry \ExponentialBackOff ;
19
20
use Symfony \Component \HttpClient \Retry \HttpCodeDecider ;
20
21
use Symfony \Component \HttpClient \Retry \RetryBackOffInterface ;
@@ -57,16 +58,14 @@ public function request(string $method, string $url, array $options = []): Respo
57
58
return new AsyncResponse ($ this ->client , $ method , $ url , $ options , function (ChunkInterface $ chunk , AsyncContext $ context ) use ($ method , $ url , $ options , &$ retryCount ) {
58
59
$ exception = null ;
59
60
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 ()) {
63
62
yield $ chunk ;
64
63
65
64
return ;
66
65
}
67
66
68
67
// only retry first chunk
69
- if (!$ clonedChunk ->isFirst ()) {
68
+ if (!$ chunk ->isFirst ()) {
70
69
$ context ->passthru ();
71
70
yield $ chunk ;
72
71
@@ -76,20 +75,20 @@ public function request(string $method, string $url, array $options = []): Respo
76
75
// catch TransportExceptionInterface to send it to strategy.
77
76
}
78
77
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 )) {
80
79
$ context ->passthru ();
81
80
yield $ chunk ;
82
81
83
82
return ;
84
83
}
85
84
86
85
$ context ->setInfo ('retry_count ' , $ retryCount );
87
- $ response ->cancel ();
86
+ $ context -> getResponse () ->cancel ();
88
87
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 );
90
89
++$ retryCount ;
91
90
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 ), [
93
92
'retryCount ' => $ retryCount ,
94
93
'delay ' => $ delay ,
95
94
]);
@@ -99,9 +98,9 @@ public function request(string $method, string $url, array $options = []): Respo
99
98
});
100
99
}
101
100
102
- private function getDelayFromHeader (ResponseInterface $ response ): ?int
101
+ private function getDelayFromHeader (array $ headers ): ?int
103
102
{
104
- if (null !== $ after = $ response -> getHeaders ( false ) ['retry-after ' ][0 ] ?? null ) {
103
+ if (null !== $ after = $ headers ['retry-after ' ][0 ] ?? null ) {
105
104
if (is_numeric ($ after )) {
106
105
return (int ) $ after * 1000 ;
107
106
}
0 commit comments