2
头图

It has been nearly two months since the last version v4.7.1 released, and the v4.8.0 version has finally been released.

This version contains new features, BUG fixes, and backward incompatible changes.

Incompatible changes

In base mode, the onStart callback will always be called back when the first worker process (worker id is 0) starts, and is executed before onWorkerStart. The coroutine API can always be used in the onStart function. When Worker-0 restarts with a fatal error, it will call back onStart again

In the previous version, onStart will be called back in Worker-0 when there is only one worker process. When there are multiple worker processes, they are executed in the Manager process.

admin_server

The important feature in this version is the addition of admin_server , which is used to provide API services, which can be used to view the current service information in the Swoole Dashboard panel, such as extensions loaded by PHP, files, classes, functions, constants, and Swoole Related processes, coroutines, connection information, etc.

//创建Server对象,监听 127.0.0.1:9501 端口
$server = new Swoole\Server('127.0.0.1', 9501);

$server->set([
    'admin_server' => '0.0.0.0:9502', // 启用 admin_server 服务
    'worker_num' => 2,
    'task_worker_num' => 3
]);

//监听连接进入事件
$server->on('Connect', function ($server, $fd) {
    echo "Client: Connect.\n";
});

//监听数据接收事件
$server->on('Receive', function ($server, $fd, $reactor_id, $data) {
    $server->send($fd, "Server: {$data}");
});

//监听连接关闭事件
$server->on('Close', function ($server, $fd) {
    echo "Client: Close.\n";
});

//启动服务器
$server->start();

After updating the Swoole v4.8.0 version, go to https://dashboard.swoole.com/ to experience it.

When logging in, configure the local admin_server or cloud address, like: http://127.0.0.1:9502/ , after logging in, you can also configure other addresses in the upper right corner.

Note: A few functions are limited, you need to install ext-swoole_plus

In addition, some new APIs have been added: Table::stats , Coroutine::join etc. Let's take a look at it in detail:

Coroutine::join

Concurrently execute multiple coroutines.

Swoole\Coroutine::join(array $cid_array, float $timeout = -1): bool

$timeout is the total timeout time, it will return immediately after timeout. But the running coroutine will continue to execute to completion without stopping

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

run(function () {
    $status = Coroutine::join([
        go(function () use (&$result) {
            $result['baidu'] = strlen(file_get_contents('https://www.baidu.com/'));
        }),
        go(function () use (&$result) {
            $result['zhihu'] = strlen(file_get_contents('https://www.zhihu.com/'));
        })
    ], 1);
    var_dump($result, $status);
});

addCommand/command

Swoole Dashboard's API is based on addCommand . The code is located in library . In addition to command provided in the library, there are also some swoole extensions.

Of course, it can also be customized:

Swoole\Server->addCommand(string $name, int $accepted_process_types, callable $callback)

$server->addCommand('test_getpid', SWOOLE_SERVER_COMMAND_MASTER | SWOOLE_SERVER_COMMAND_EVENT_WORKER,
    function ($server) {
        return json_encode(['pid' => posix_getpid()]);
});

command method is used to call the defined interface in the server:

Swoole\Server->command(string $name, int $process_id, int $process_type, $data, bool $json_decode = true)

$server->command('test_getpid', 0, SWOOLE_SERVER_COMMAND_MASTER, ['type' => 'master']);

onBeforeShutdown

Added the onBeforeShutdown event callback, in which the coroutine API can be used.

  • Safety Tips

Asynchronous and coroutine APIs can be used in the onStart callback, but it should be noted that this may conflict dispatch_func and package_length_func at the same time.

Coroutine::getStackUsage()

Get the memory usage of the current PHP stack.

Swoole\Coroutine::getStackUsage([$cid]): int

Table::stats

Used to get the Swoole\Table status.

use Swoole\Table;

$table = new Table(1024);
$table->column('string', Table::TYPE_STRING, 256);
$table->create();

$table->set('swoole', ['string' => 'www.swoole.com']);
var_dump($table->stats());

//array(8) {
//  ["num"]=>
//  int(1)
//  ["conflict_count"]=>
//  int(0)
//  ["conflict_max_level"]=>
//  int(0)
//  ["insert_count"]=>
//  int(1)
//  ["update_count"]=>
//  int(0)
//  ["delete_count"]=>
//  int(0)
//  ["available_slice_num"]=>
//  int(204)
//  ["total_slice_num"]=>
//  int(204)
//}

Update log

The following is the complete update log:

Incompatible changes downwards

  • In base mode, the onStart callback will always be called when the first worker process (worker id is 0) starts, before onWorkerStart (#4389) (@matyhtf)

New API

  • Add Coroutine::getStackUsage() method (#4398) (@matyhtf) (@twose)
  • Add Coroutine\Redis (#4390) (@chrysanthemum)
  • Add Table::stats() method (#4405) (@matyhtf)
  • Add Coroutine::join() method (#4406) (@matyhtf)

new features

  • Support server command (#4389) (@matyhtf)
  • Support Server::onBeforeShutdown event callback (#4415) (@matyhtf)

Enhance

  • Set error code when Websocket pack fails (swoole/swoole-src@d27c5a5) (@matyhtf)
  • Added Timer::exec_count field (#4402) (@matyhtf)
  • hook mkdir supports the use of open_basedir ini configuration (#4407) (@NathanFreeman)
  • library adds vendor_init.php script (swoole/library@6c40b02) (@matyhtf)
  • SWOOLE_HOOK_CURL supports CURLOPT_UNIX_SOCKET_PATH (swoole/library#121) (@sy-records)
  • Client supports setting ssl_ciphers configuration item (#4432) (@amuluowin)
  • Added some new information for Server::stats() (#4410) (#4412) (@matyhtf)

repair

  • Fix the unnecessary URL decode (swoole/swoole-src@a73780e) (@matyhtf) of the file name when uploading files
  • Fix HTTP2 max_frame_size issue (#4394) (@twose)
  • Fix curl_multi_select bug #4393 (#4418) (@matyhtf)
  • Fix missing coroutine options (#4425) (@sy-records)
  • Fix the problem that the connection cannot be closed when the sending buffer is full (swoole/swoole-src@2198378) (@matyhtf)


沈唁
1.9k 声望1.2k 粉丝