Skip to content

Commit

Permalink
fix: end tracing when receiving last chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
cyve committed Dec 29, 2023
1 parent ff4d2b1 commit 57e6ba0
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/Http/TracedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpClient\Exception\ServerException;
use Symfony\Component\HttpClient\Response\StreamableInterface;
use Symfony\Component\HttpClient\Response\StreamWrapper;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

Expand Down Expand Up @@ -91,22 +92,21 @@ public function getInfo(string $type = null): mixed
return $this->response->getInfo($type);
}

/**
* Do not end tracing when calling toStream(). It will end when self::stream() will receive the last chunk.
*/
public function toStream(bool $throw = true)
{
try {
if ($throw) {
// Ensure headers arrived
$this->response->getHeaders();
}

if ($this->response instanceof StreamableInterface) {
return $this->stream = $this->response->toStream(false);
}
if ($throw) {
// Ensure headers arrived
$this->response->getHeaders();
}

return $this->stream = StreamWrapper::createResource($this->response);
} finally {
$this->endTracing();
if ($this->response instanceof StreamableInterface) {
return $this->stream = $this->response->toStream(false);
}

return $this->stream = StreamWrapper::createResource($this->response);
}

/**
Expand All @@ -129,6 +129,14 @@ public static function stream(HttpClientInterface $client, iterable $responses,
}

foreach ($client->stream($wrappedResponses, $timeout) as $r => $chunk) {
try {
if ($chunk->isLast() || $chunk->isTimeout()) {
$traceableMap[$r]->endTracing();
}
} catch (TransportExceptionInterface) {
$traceableMap[$r]->endTracing();
}

yield $traceableMap[$r] => $chunk;
}
}
Expand Down

0 comments on commit 57e6ba0

Please sign in to comment.