不兼容改动
在 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::stats、Coroutine::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_func和package_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