@@ -113,7 +113,12 @@ public function request(string $method, string $url, array $options = []): Respo
113
113
if ($ onProgress = $ options ['on_progress ' ]) {
114
114
// Memoize the last progress to ease calling the callback periodically when no network transfer happens
115
115
$ 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
+
117
122
$ progressInfo = $ info ;
118
123
$ progressInfo ['url ' ] = implode ('' , $ info ['url ' ]);
119
124
unset($ progressInfo ['size_body ' ]);
@@ -127,6 +132,13 @@ public function request(string $method, string $url, array $options = []): Respo
127
132
128
133
$ onProgress ($ lastProgress [0 ], $ lastProgress [1 ], $ progressInfo );
129
134
};
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
+ };
130
142
}
131
143
132
144
// 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
166
178
$ options ['request_headers ' ][] = 'user-agent: Symfony HttpClient/Native ' ;
167
179
}
168
180
181
+ if (0 < $ options ['max_duration ' ]) {
182
+ $ options ['timeout ' ] = min ($ options ['max_duration ' ], $ options ['timeout ' ]);
183
+ }
184
+
169
185
$ context = [
170
186
'http ' => [
171
187
'protocol_version ' => $ options ['http_version ' ] ?: '1.1 ' ,
0 commit comments