间隔上个版本v4.7.1公布近两个月了,v4.8.0 版本终于公布了。

此版本蕴含了新性能、BUG 修复以及向下不兼容的改变。

不兼容改变

在 base 模式下,onStart 回调将始终在第一个工作过程 (worker id 为 0) 启动时回调,先于 onWorkerStart 执行。在 onStart 函数中始终能够应用协程 API,Worker-0 呈现致命谬误重启时,会再次回调 onStart

在之前的版本中,onStart 在只有一个工作过程时,会在 Worker-0 中回调。有多个工作过程时,在 Manager 过程中执行。

admin_server

在此版本中重要的性能就是减少了admin_server的选项,用于提供 API 服务,能够用于在 Swoole Dashboard 面板中查看以后服务的信息,例如 PHP 加载的扩大、文件、类、函数、常量,以及 Swoole 相干的过程、协程、连贯信息等。

//创立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();

能够在更新 Swoole v4.8.0 版本后,返回 https://dashboard.swoole.com/ 进行体验。

在登录时配置本地的admin_server地址或者云端的地址,形如:http://127.0.0.1:9502/ ,登录后也能够在右上角配置其余地址。

注:多数性能受限,须要装置ext-swoole_plus

另外还减少了一些新的 API:Table::statsCoroutine::join等,上面来具体看一下:

Coroutine::join

并发执行多个协程。

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

$timeout为总的超时工夫,超时后会立刻返回。但正在运行的协程会继续执行结束,而不会停止

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 的 API 就是基于addCommand提供的,代码位于 library 中,除了 library 中提供的command,swoole 扩大中也有一些。

当然也能够自定义:

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办法用于在 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

新增onBeforeShutdown事件回调,在此回调中能够应用协程 API。

  • 平安提醒

onStart回调中能够应用异步和协程的 API,但须要留神这可能会与dispatch_funcpackage_length_func存在抵触,请勿同时应用

Coroutine::getStackUsage()

获取以后 PHP 栈的内存使用量。

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

Table::stats

用来获取 Swoole\Table 状态。

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)//}

更新日志

上面是残缺的更新日志:

向下不兼容改变

  • 在 base 模式下,onStart 回调将始终在第一个工作过程 (worker id 为 0) 启动时回调,先于 onWorkerStart 执行 (#4389) (@matyhtf)

新增 API

  • 新增 Coroutine::getStackUsage() 办法 (#4398) (@matyhtf) (@twose)
  • 新增 Coroutine\Redis 的一些 API (#4390) (@chrysanthemum)
  • 新增 Table::stats() 办法 (#4405) (@matyhtf)
  • 新增 Coroutine::join() 办法 (#4406) (@matyhtf)

新增性能

  • 反对 server command (#4389) (@matyhtf)
  • 反对 Server::onBeforeShutdown 事件回调 (#4415) (@matyhtf)

加强

  • 当 Websocket pack 失败时设置错误码 (swoole/swoole-src@d27c5a5) (@matyhtf)
  • 新增 Timer::exec_count 字段 (#4402) (@matyhtf)
  • hook mkdir 反对应用 open_basedir ini 配置 (#4407) (@NathanFreeman)
  • library 新增 vendor_init.php 脚本 (swoole/library@6c40b02) (@matyhtf)
  • SWOOLE_HOOK_CURL 反对 CURLOPT_UNIX_SOCKET_PATH (swoole/library#121) (@sy-records)
  • Client 反对设置 ssl_ciphers 配置项 (#4432) (@amuluowin)
  • Server::stats() 增加了一些新的信息 (#4410) (#4412) (@matyhtf)

修复

  • 修复文件上传时,对文件名字进行不必要的 URL decode (swoole/swoole-src@a73780e) (@matyhtf)
  • 修复 HTTP2 max_frame_size 问题 (#4394) (@twose)
  • 修复 curl_multi_select bug #4393 (#4418) (@matyhtf)
  • 修复失落的 coroutine options (#4425) (@sy-records)
  • 修复当发送缓冲区满的时候,连贯无奈被 close 的问题 (swoole/swoole-src@2198378) (@matyhtf)