Skip to content

Commit

Permalink
php: add cancellaction for the upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ata-no-one committed Aug 2, 2024
1 parent 42c5f58 commit f566f99
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 2 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 6 additions & 2 deletions php/src/vaas/Vaas.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Amp\ByteStream\ReadableResourceStream;
use Amp\ByteStream\ReadableStream;
use Amp\DeferredCancellation;
use Amp\Http\Client\HttpClient;
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\HttpException;
Expand Down Expand Up @@ -498,6 +499,7 @@ public function setUploadTimeout(int $UploadTimeoutInSeconds): self
*/
private function UploadStream(ReadableStream $fileStream, string $url, string $uploadToken, int $fileSize): void
{
$cancellation = new DeferredCancellation();
$times = 0;
$pingTimer = EventLoop::repeat(5, function () use(&$times) {
$this->_logger->debug("pinging " . $times++);
Expand All @@ -513,9 +515,10 @@ private function UploadStream(ReadableStream $fileStream, string $url, string $u
$request->addHeader("Content-Length", $fileSize);
$request->addHeader("Authorization", $uploadToken);

$response = $this->_httpClient->request($request, new TimeoutCancellation($this->_uploadTimeoutInSeconds));
$response = $this->_httpClient->request(
$request, new TimeoutCancellation($this->_uploadTimeoutInSeconds), $cancellation->getCancellation());
if ($response->getStatus() > 399) {
$reason = $response->getBody()->buffer();
$reason = $response->getBody()->buffer($cancellation->getCancellation());
throw new UploadFailedException($reason, $response->getStatus());
}
} catch (\Exception $e) {
Expand All @@ -525,6 +528,7 @@ private function UploadStream(ReadableStream $fileStream, string $url, string $u
throw new VaasClientException($e->getMessage());
} finally {
EventLoop::cancel($pingTimer);
$cancellation->cancel();
}
}
}

0 comments on commit f566f99

Please sign in to comment.