Skip to content

Commit

Permalink
Optimized the memory usage when writing chunks (#138)
Browse files Browse the repository at this point in the history
* optimize the memory usage when writing chunks

* php8 property

* Update .gitignore

* Update SwooleClient.php

Co-authored-by: 谢彪 <xiebiao@kucoin.com>
Co-authored-by: Dries Vints <dries@vints.io>
Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
4 people authored Apr 8, 2021
1 parent 4215ec7 commit efc4b0a
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Swoole/SwooleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

class SwooleClient implements Client, ServesStaticFiles
{
public function __construct(protected int $chunkSize = 1048576)
{
}

/**
* Marshal the given request context into an Illuminate request.
*
Expand Down Expand Up @@ -164,14 +168,16 @@ protected function sendResponseContent(Response $response, SwooleResponse $swool

$content = $response->getContent();

if (strlen($content) <= 8192) {
$length = strlen($content);

if ($length <= $this->chunkSize) {
$swooleResponse->end($content);

return;
}

foreach (str_split($content, 8192) as $chunk) {
$swooleResponse->write($chunk);
for ($offset = 0; $offset < $length; $offset += $this->chunkSize) {
$swooleResponse->write(substr($content, $offset, $this->chunkSize));
}

$swooleResponse->end();
Expand Down

0 comments on commit efc4b0a

Please sign in to comment.