Open Swoole 4.7.0 released: multiple new APIs, features, fixes and enhancements

Published:

Swoole v4.7.0 is released with multiple new APIs, features, fixes and enhancements.


Backward compatibility break at Swoole Table

For performance reason, from v4.7.0, you can't use object of type Swoole\Table as array.

Async DNS with C-ares DNS library

Starting from v4.7.0, asynchronous DNS library C-ares is supported. C-ares is a C library for asynchronous DNS requests (including name resolves).

The feature is not enabled by default, you can compile Swoole with the new option --enable-cares to enable C-ares library on Swoole.

If C-ares is not enabled, the async DNS queries within Swoole are simulated with a pool of blocking processes to process.

Once C-ares is enabled in Swoole, all the DNS queries produced by the Swoole DNS API System::dnsLookup, System::gethostbyname or Swoole coroutine clients are asynchronous, including the MySQL client, Redis Client, HTTP Client, CURL etc.

Example:

<?php
use Swoole\Coroutine\System;
use function Swoole\Coroutine\run;

run(function() {
    var_dump(System::dnsLookup('www.google.com', 3, AF_INET6));
});
use Swoole\Coroutine\System;
use function Swoole\Coroutine\run;

run(function() {
    $ip = System::gethostbyname("www.google.com", AF_INET, 0.5);
    echo $ip;
});


New API: Process\Pool::detach()

Starting from v4.7.0, you can isolate a process created and managed by a process pool with API Process\Pool::detach().

You can find more about Process\Pool::detach() at /docs/modules/swoole-process-pool-detach.


New API: Co::cancel()

Starting from v4.7.0, you can proactively stop the execution of a running coroutine with a new API Co::cancel().

You can find more about Co::cancel() at /docs/modules/swoole-coroutine-cancel.


New Swoole Http Client options

  • http_compression
  • body_decompression

You can find more at /docs/modules/swoole-coroutine-http-client-set.


New load balancing modes

With v4.7.0 there are two new load-balancing and dispatching modes (SWOOLE_DISPATCH_CO_REQ_LB) for the coroutine server:

  • Lowest Coroutine Number Mode On Connect (8): Dispatches to the process with lowest total coroutines number when the connection is established.
  • Lowest Coroutine Number Mode On Request (9): Dispatches to the process with lowest total coroutines number for each request.

You can enable these modes according to /docs/modules/swoole-server/configuration:

<?php
$server->set([
    'dispatch_mode'=> 8,
    // Or...
    'dispatch_mode'=> 9,
]);

Support timeout option at ConnectionPool::get()

You can find more at /docs/modules/connection-pool-get.


Heartbeat configuration on different TCP ports

Starting from v4.7.0, you can set different heartbeat options on different ports if the TCP server is listening to multiple different ports.

<?php
use Swoole\Server;

$server = new Server('127.0.0.1', 9501, SWOOLE_BASE);

// Base heartbeat time
$server->set([
    'heartbeat_check_interval' => 1,
    'heartbeat_idle_time' => 1,
]);

$server->on('Connect', function($server, $fd) {
    $time = date('Y-m-d H:i:s');
    echo "[{$time}] Client#{$fd}: Connect.\n";
});

$server->on('Receive', function($server, $fd, $reactorId, $data) {
    $server->send($fd, 'ok');
});

$server->on('Close', function($server, $fd) {
    $time = date('Y-m-d H:i:s');
    echo "[{$time}] Client#{$fd}: Close.\n";
});

// Change heartbeat time for port2
$port2 = $server->listen('127.0.0.1', 9502, SWOOLE_SOCK_TCP);
$port2->set([
    'heartbeat_idle_time' => 2,
]);

// Change heartbeat time for port3
$port3 = $server->listen('127.0.0.1', 9503, SWOOLE_SOCK_TCP);
$port3->set([
    'heartbeat_idle_time' => 10,
]);

$server->start();

You can find more server configuration options at /docs/modules/swoole-server/configuration


List of changes in v4.7.0

New APIs
---
* Added Process\Pool::detach() (#4221) (@matyhtf)
* Added onDisconnect callback for Swoole\Server (#4230) (@matyhtf)
* Added Coroutine::cancel() (#4247) (#4249) (@matyhtf)
* Added http_compression/body_decompression options for Http Client (#4299) (@matyhtf)

Enhancement
---
* Supported mysql client prepare field type identification (#4238) (@Yurunsoft)
* Supported c-ares, Refactor DNS (#4275) (@matyhtf)
* Supported setting different idle time for each port (#4290) (@matyhtf)
* Supported SW_DISPATCH_CO_CONN_LB and SW_DISPATCH_CO_REQ_LB for Swoole\Server dispatch_mode (#4318) (@matyhtf)
* Supported timeout for Swoole\ConnectionPool::get (swoole/library#108) (@leocavalcante)
* Supported CURLOPT_PRIVATE for Hook Curl (swoole/library#112) (@sy-records)
* Optimized PDOStatementProxy::setFetchMode function prototype (swoole/library#109) (@yespire)

Fixed
---
* Fixed uncaught thread creation exception when creating a large number of coroutines (swoole/swoole-src@8ce5041) (@matyhtf)
* Fixed the "make install" missing php_swoole.h header file (#4239) (@sy-records)
* Fixed EVENT_HANDSHAKE BC (#4248) (@sy-records)
* Fixed SW_LOCK_CHECK_RETURN (#4302) (@zmyWL)
* Fixed problems with Swoole\Atomic\Long M1 chip (swoole/swoole-src@e6fae2e) (@matyhtf)
* Fixed missing return value of Coroutine\go (swoole/library@1ed49db) (@matyhtf)
* Fixed StringObject consistency between other methods and its immutability (swoole/library#111) (@leocavalcante)
* Fixed StringObject substr error (swoole/library#113) (@sy-records)

Kernel
---
* Did not hook disabled functions (#4283) (@twose)

Test
---
* Added Cygwin build (#4222) (@sy-records)
* Added alpine 3.13 and 3.14 into building test (#4309) (@limingxinleo)

You can upgrade to Swoole v4.7.0 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.