Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized the memory usage when writing chunks #138

Merged
merged 4 commits into from
Apr 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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