Open Swoole 4.7.1 released: introduce a new concurrency mode, bug fixes and enhancements

Published:

Swoole v4.7.1 is released with a new concurrency mode, bug fixes and enhancements.


The new concurrency mode

A new concurrency mode is introduced within Open Swoole 4.7.1: process level concurrency with coroutines support.

The requests are blocked at the process level while you can use coroutines within the request to reduce latency.

You can enable this new concurrency mode with the following settings:

Co::set(['max_concurrency' => 1]);

For example, the global variable $count in the following code is considered not safe when coroutines are enabled. Because it is shared by all the coroutines and requests executed within the Linux process. When there are more than one requests are executed concurrently, you may see the unexpected result of the counter value.

$server->on("Receive", function (Server $serv, $fd, $reactorId, $data)
{
    global $count;
    $count = 0;
    co::sleep(0.05);
    $count += 1;
    $serv->send($fd, "$count\r\n\r\n");
});

The new concurrency model makes sure only one request is executed at the same time, while you can still use coroutines within each request.

This makes it easier for the existing traditional PHP frameworks to adopt Open Swoole coroutines. You can manage the status of global variables within the lifecycle of a request, initialize or reset them at the beginning of the request and destroy them at the end of the request.

Now Open Swoole supports multiple concurrency modes: process level concurrency using task workers (the mode used in Laravel Octane), coroutines level concurrency (when coroutines are enabled), process-level concurrency with coroutines support.

Instead of dispatching heavy tasks to other Linux processes, you can create coroutines within the process to execute external IO operations.

This is more like the concurrency mode of PHP-FPM with PHP fibres available to real-world applications.

Set a maximum concurrency level at each Linux process worker

You can also set a maximum top-level (request level) concurrency limitation to better control the capacity and reliability of your Swoole server.

Co::set(['max_concurrency' => 100]);

List of changes in v4.7.1

New feature
---
* Introduce a new concurrency mode (#4330) (@doubaokun)

Enhancement
---
* Supported query /etc/hosts for System::dnsLookup (#4341) (#4349) (@zmyWL) (@NathanFreeman)
* Supported boost context support for mips64 (#4358) (@dixyes)
* Supported CURLOPT_RESOLVE option for SWOOLE_HOOK_CURL (swoole/library#107) (@sy-records)
* Supported CURLOPT_NOPROGRESS for SWOOLE_HOOK_CURL (swoole/library#117) (@sy-records)
* Supported boost context support for riscv64 (#4375) (@dixyes)

Fixed
---
* Fixed memory error on shutdown (PHP-8.1) (#4325) (@twose)
* Fixed not serializable classes for 8.1.0beta1 (#4335) (@remicollet)
* Fixed multiple coroutines recursively creating directories (#4337) (@NathanFreeman)
* Fixed native curl bugs (#4360) (@matyhtf)
* Fixed PDOStatement::bindParam() expects parameter 1 to be string (swoole/library#116) (@sy-records)

You can upgrade to Swoole v4.7.1 now:

pecl install openswoole

If you need to install Swoole or look at other update methods, checkout the installation documentation and how to update Swoole.