zendframework / zend-expressive-swoole

Swoole support for Expressive applications
BSD 3-Clause "New" or "Revised" License
158 stars 14 forks source link

Adjust response chunk size #57

Closed samuelnogueira closed 5 years ago

samuelnogueira commented 5 years ago

Apparently, despite stating in Swoole documentation that Swoole\Http\Response::write accepts up to 2M (2097152 bytes) of data, a WARNING swServer_tcp_send (ERROR 1202): More than the output buffer size[2097152], please use the sendfile. warning will be raised when trying to write anything more than 2097142 bytes (note the difference between 2097152 and 2097142).

This happens regardless of response status code, headers, cookies and previously sent body fragments.

This isn't directly an issue with zend-expressive-swoole library, but this problem isn't neither handled by the library, or documented.

Code to reproduce the issue

<?php

use Zend\Diactoros\Response\TextResponse;
use Zend\Expressive\Swoole\SwooleEmitter;

require dirname(__DIR__) . '/vendor/autoload.php';

$server = new \Swoole\Http\Server('0.0.0.0', 8080);
$server->on('request', function ($request, $response) {
    $emitter = new SwooleEmitter($response);
    $emitter->emit(new TextResponse(str_repeat('a', 2 * 1024 * 1024)));
});
$server->start();

Expected results

curl http://localhost:8080/ returns a bunch of a's

Actual results

WARNING swServer_tcp_send (ERROR 1202): More than the output buffer size[2097152], please use the sendfile. Plus empty HTTP response.

huangzhhui commented 5 years ago
$server->set([
    'buffer_output_size' => 32 * 1024 *1024, // 32M
])
geerteltink commented 5 years ago

If I understand it correctly you can set all server options like these in the configuration with:

'swoole-http-server' => [
    'options' => [
       'buffer_output_size' => 32 * 1024 *1024
    ]
]
samuelnogueira commented 5 years ago

Thank you @huangzhhui and @xtreamwayz , that works!

Just a note, setting buffer_output_size to anything below\Zend\Expressive\Swoole\SwooleEmitter::CHUNK_SIZE + 10 is still not enough to prevent swoole from throwing those warnings. I'm guessing this has to do with some additional bytes being written to the output buffer by Swoole\Http\Server here: https://github.com/swoole/swoole-src/blob/cc7a013b8761383befed70a3e999310a0306ea4e/swoole_http_server.cc#L1534

Given this workaround fixes an unexpected behavior which isn't directly related with this library, should any action be taken here?

weierophinney commented 5 years ago

@samuelnogueira — regarding:

Given this workaround fixes an unexpected behavior which isn't directly related with this library, should any action be taken here?

I think we can direct folks to the workarounds illustrated in the comments in this issue. Alternately, file an issue upstream to the Swoole source repository.