Open Swoole 4.6.3 released: set Content-Length header, compression_min_length and more


Swoole v4.6.3 is released with multiple new features and enhancement today.

Namespace alias functions are added for go and defer

Two new functions with namespace are added: Swoole\Coroutine\go and Swoole\Coroutine\defer.

These functions can be used when you have disabled the short named functions go, defer.

Example of Swoole\Coroutine\go and Swoole\Coroutine\defer:


use function Swoole\Coroutine\go;
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\defer;

run(function () {
    defer(function () {
        echo "co1\n";
    go(function () {
        echo "co2\n";


To specify the minimum length of the response to compress, use the compression_min_length directive. The default is 20 bytes.


$http->set(['compression_min_length' => 128]);

Content-Length header support

From Open Swoole 4.6.3, you can set the Content-Length header if the content length is known or you like to change the default Content-Length header calculated by Swoole Server.

Don't add the Content-Length header if your application doesn't require. Swoole server automatically set the Content-Length header based on the response length. Setting a wrong Content-Length may cause a non-complete response or server hanging.


use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;

$http = new Server('', 9000);

$http->on('Request', function (Request $request, Response $response) {
    $msg = 'Hello, Swoole';
    $response->header('Content-Length', 5);
    //$response->header('Content-Length', strlen($msg));


List of changes in v4.6.3

New APIs
+ Added Swoole\Coroutine\go function (swoole/library@82f63be) (@matyhtf)
+ Added Swoole\Coroutine\defer function (swoole/library@92fd0de) (@matyhtf)

+ Added option compression_min_length for HTTP Server (#4033) (@matyhtf)
+ Allowed setting content-length HTTP header in application layer (#4041) (@doubaokun)

* Fixed coredump when program reach file open limitation (swoole/swoole-src@709813f) (@matyhtf)
* Fixed JIT being disabled (#4029) (@twose)
* Fixed Response::create() bug (swoole/swoole-src@a630b5b) (@matyhtf)
* Fixed task process id false positives on ARM (#4040) (@doubaokun)
* Fixed README (#4046) (@asheroto)
* Fixed native-curl crash on PHP8 (#4042) (#4045) (@Yurunsoft) (@matyhtf)
* Fixed mem error (#4050) (@matyhtf)

* Optimized ssl_connect/ssl_shutdown (#4030) (@matyhtf)
* Exit the process directly when a fatal error occurs (#4053) (@matyhtf)

You can upgrade to Swoole v4.6.3 now:

pecl install openswoole