Skip to content

Commit 8483639

Browse files
Not static state, still failing
1 parent e6e052f commit 8483639

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/Symfony/Component/HttpClient/Response/AmpResponse.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Amp\Http\Client\Request;
2020
use Amp\Http\Client\Response;
2121
use Psr\Log\LoggerInterface;
22+
use Revolt\EventLoop;
2223
use Symfony\Component\HttpClient\Chunk\FirstChunk;
2324
use Symfony\Component\HttpClient\Chunk\InformationalChunk;
2425
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
@@ -32,6 +33,7 @@
3233

3334
use function Amp\async;
3435
use function Amp\delay;
36+
use function Amp\Future\awaitFirst;
3537

3638
/**
3739
* @author Nicolas Grekas <p@tchwork.com>
@@ -49,8 +51,6 @@ final class AmpResponse implements ResponseInterface, StreamableInterface
4951
private ?array $options;
5052
private \Closure $onProgress;
5153

52-
private static ?DeferredCancellation $delay = null;
53-
5454
/**
5555
* @internal
5656
*/
@@ -105,7 +105,7 @@ public function __construct(AmpClientState $multi, Request $request, array $opti
105105
};
106106

107107
$multi->lastTimeout = null;
108-
$multi->openHandles[$id] = $id;
108+
$multi->openHandles[$id] = new DeferredFuture();
109109
++$multi->responseCount;
110110

111111
$this->canary = new Canary(static function () use ($canceller, $multi, $id) {
@@ -181,18 +181,31 @@ private static function perform(ClientState $multi, array &$responses = null): v
181181
*/
182182
private static function select(ClientState $multi, float $timeout): int
183183
{
184-
self::$delay ??= new DeferredCancellation();
185-
delay($timeout, true, self::$delay->getCancellation());
184+
$delay = new DeferredFuture();
185+
$id = EventLoop::delay($timeout, $delay->complete(...));
186+
187+
awaitFirst((function () use ($delay, $multi) {
188+
yield $delay->getFuture();
189+
190+
foreach ($multi->openHandles as $deferred) {
191+
yield $deferred->getFuture();
192+
}
193+
})());
186194

187-
return null === self::$delay ? 1 : 0;
195+
try {
196+
return $delay->isComplete() ? 0 : 1;
197+
} finally {
198+
EventLoop::cancel($id);
199+
}
188200
}
189201

190202
private static function generateResponse(Request $request, AmpClientState $multi, string $id, array &$info, array &$headers, DeferredCancellation $canceller, array &$options, \Closure $onProgress, &$handle, ?LoggerInterface $logger, float &$pause): void
191203
{
192204
$request->setInformationalResponseHandler(static function (Response $response) use ($multi, $id, &$info, &$headers) {
193205
self::addResponseHeaders($response, $info, $headers);
194206
$multi->handlesActivity[$id][] = new InformationalChunk($response->getStatus(), $response->getHeaders());
195-
self::stopLoop();
207+
$multi->openHandles[$id]->complete();
208+
$multi->openHandles[$id] = new DeferredFuture();
196209
});
197210

198211
try {
@@ -209,7 +222,7 @@ private static function generateResponse(Request $request, AmpClientState $multi
209222
if ('HEAD' === $response->getRequest()->getMethod() || \in_array($info['http_code'], [204, 304], true)) {
210223
$multi->handlesActivity[$id][] = null;
211224
$multi->handlesActivity[$id][] = null;
212-
self::stopLoop();
225+
$multi->openHandles[$id]->complete();
213226

214227
return;
215228
}
@@ -221,7 +234,8 @@ private static function generateResponse(Request $request, AmpClientState $multi
221234
$body = $response->getBody();
222235

223236
while (true) {
224-
self::stopLoop();
237+
$multi->openHandles[$id]->complete();
238+
$multi->openHandles[$id] = new DeferredFuture();
225239

226240
if (0 < $pause) {
227241
delay($pause, true, $canceller->getCancellation());
@@ -244,7 +258,7 @@ private static function generateResponse(Request $request, AmpClientState $multi
244258
$info['download_content_length'] = $info['size_download'];
245259
}
246260

247-
self::stopLoop();
261+
$multi->openHandles[$id]->complete();
248262
}
249263

250264
private static function followRedirects(Request $originRequest, AmpClientState $multi, array &$info, array &$headers, DeferredCancellation $canceller, array $options, \Closure $onProgress, &$handle, ?LoggerInterface $logger, float &$pause): ?Response
@@ -421,12 +435,4 @@ private static function getPushedResponse(Request $request, AmpClientState $mult
421435
return $response;
422436
}
423437
}
424-
425-
private static function stopLoop(): void
426-
{
427-
if (null !== self::$delay) {
428-
self::$delay->cancel();
429-
self::$delay = null;
430-
}
431-
}
432438
}

0 commit comments

Comments
 (0)