@@ -113,7 +113,12 @@ public function request(string $method, string $url, array $options = []): Respo
113113 if ($ onProgress = $ options ['on_progress ' ]) {
114114 // Memoize the last progress to ease calling the callback periodically when no network transfer happens
115115 $ lastProgress = [0 , 0 ];
116- $ onProgress = static function (...$ progress ) use ($ onProgress , &$ lastProgress , &$ info ) {
116+ $ maxDuration = 0 < $ options ['max_duration ' ] ? $ options ['max_duration ' ] : INF ;
117+ $ onProgress = static function (...$ progress ) use ($ onProgress , &$ lastProgress , &$ info , $ maxDuration ) {
118+ if ($ info ['total_time ' ] >= $ maxDuration ) {
119+ throw new TransportException (sprintf ('Max duration was reached for "%s". ' , implode ('' , $ info ['url ' ])));
120+ }
121+
117122 $ progressInfo = $ info ;
118123 $ progressInfo ['url ' ] = implode ('' , $ info ['url ' ]);
119124 unset($ progressInfo ['size_body ' ]);
@@ -127,6 +132,13 @@ public function request(string $method, string $url, array $options = []): Respo
127132
128133 $ onProgress ($ lastProgress [0 ], $ lastProgress [1 ], $ progressInfo );
129134 };
135+ } elseif (0 < $ options ['max_duration ' ]) {
136+ $ maxDuration = $ options ['max_duration ' ];
137+ $ onProgress = static function () use (&$ info , $ maxDuration ): void {
138+ if ($ info ['total_time ' ] >= $ maxDuration ) {
139+ throw new TransportException (sprintf ('Max duration was reached for "%s". ' , implode ('' , $ info ['url ' ])));
140+ }
141+ };
130142 }
131143
132144 // Always register a notification callback to compute live stats about the response
@@ -166,6 +178,10 @@ public function request(string $method, string $url, array $options = []): Respo
166178 $ options ['request_headers ' ][] = 'user-agent: Symfony HttpClient/Native ' ;
167179 }
168180
181+ if (0 < $ options ['max_duration ' ]) {
182+ $ options ['timeout ' ] = min ($ options ['max_duration ' ], $ options ['timeout ' ]);
183+ }
184+
169185 $ context = [
170186 'http ' => [
171187 'protocol_version ' => $ options ['http_version ' ] ?: '1.1 ' ,
0 commit comments