80

Swoole 1.10.0 发布,增加多项新特性 - Swoole开源项目 - SegmentFault

 6 years ago
source link: https://segmentfault.com/a/1190000012753549?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

PHP的异步、并行、高性能网络通信引擎 Swoole 已发布 1.10.0 版本。此版本增加了多项新特性。

自动 DNS 解析

新版本的异步客户端不再需要使用 swoole_async_dns_lookup 解析域名了,底层实现了自动域名解析。Client 在执行 connect 方法时可直接传入域名。

$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function(swoole_client $cli) {
    $cli->send("GET / HTTP/1.1\r\n\r\n");
});
$client->on("receive", function(swoole_client $cli, $data){
    echo "Receive: $data";
    $cli->send(str_repeat('A', 100)."\n");
    sleep(1);
});
$client->on("error", function(swoole_client $cli){
    echo "error\n";
});
$client->on("close", function(swoole_client $cli){
    echo "Connection close\n";
});
//底层会自动进行异步域名解析
$client->connect('www.baidu.com', 9501);

慢请求日志

新版本增加了追踪慢请求功能,可记录慢请求的 PHP 函数调用栈。

function test()
{
    test_sleep();
}

function test_sleep()
{
    echo "sleep 5\n";
    sleep(5);
}

$server = new swoole_server('127.0.0.1', 9501);

$server->set([
    'worker_num' => 1,
    'task_worker_num' => 1,
    'trace_event_worker' => true,
    'request_slowlog_timeout' => 1,
    'request_slowlog_file' => '/tmp/trace.log',
]);

$server->on('Receive', function($serv, $fd, $reactor_id, $data) {
    test();
    $serv->send($fd, "Swoole: $data");
});

$server->start();

处理慢请求后,/tmp/trace.log日志中将打印一行错误信息:

[08-Jan-2018 15:21:57]  [worker#0] pid 26905
[0x00007f60cda22340] sleep() /home/htf/workspace/swoole/examples/server/trace.php:10
[0x00007f60cda222e0] test_sleep() /home/htf/workspace/swoole/examples/server/trace.php:4
[0x00007f60cda22280] test() /home/htf/workspace/swoole/examples/server/trace.php:28
[0x00007f60cda22190] {closure}() /home/htf/workspace/swoole/examples/server/trace.php:42
[0x00007f60cda22140] start() /home/htf/workspace/swoole/examples/server/trace.php:42

新增 STREAM 模块

新增的 stream 模块使得 ReactorWorkerTask 进程之间的通信方式更灵活,最大程度地解耦。复杂的线上项目使用 stream 模式,请求分配调度的效率更高。

$serv = new swoole_server("127.0.0.1", 9501);

$serv->set(array(
    'dispatch_mode' => 7,
    'worker_num' => 2,
));

$serv->on('receive', function (swoole_server $serv, $fd, $threadId, $data)
{
    var_dump($data);
    echo "#{$serv->worker_id}>> received length=" . strlen($data) . "\n";
});

$serv->start();
  • ReactorWorker 之间通信,使用 dispatch_mode = 7 来开启
  • WorkerTask 之间通信,使用 task_ipc_mode = 4 来开启

增加 Event::cycle 函数

用户代码可自定义一个 EventLoop 的钩子函数,此函数会在每一轮事件循环结束时调用。方便使用 Generator + YieldPromiseSwoole 框架实现自己的调度器。

Swoole\Timer::tick(2000, function ($id) {
    var_dump($id);
});

Swoole\Event::cycle(function () {
    echo "hello [1]\n";
    Swoole\Event::cycle(function () {
        echo "hello [2]\n";
        Swoole\Event::cycle(null);
    });
});

其他更新内容

  • 更新Table::incrTable::decr支持有符号整型
  • 兼容PHP-7.2版本
  • 修复Event::del函数无法移除标准输入句柄的问题
  • 修复Task进程内定时器间隔小于Client接收超时时间,引起Client::recv死锁的问题
  • 增加ssl_host_name配置项,用于验证SSL/TLS主机合法性
  • 使用dispatch_mode = 3时,当所有Worker为忙的状态时打印一条错误日志
  • 增加端口迭代器,可遍历某个监听端口的所有连接
  • 修复Table在非x86平台存在的内存对齐问题
  • 修复BASE模式下max_request配置无效的问题
  • 修复WebSocket服务器在某些客户端ping帧带有mask数据时回包错误的问题
  • 修复HttpClient使用HEAD方法响应内容携带Content-Length导致卡死的问题
  • 增加MySQL异步客户端对JSON格式的支持

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK