Swoole 1.10.0 发布,增加多项新特性

栏目: 软件资讯 · 发布时间: 7年前

内容简介:PHP的异步、并行、高性能网络通信引擎 Swoole 已发布 1.10.0 版本。此版本增加了多项新特性。 自动 DNS 解析 新版本的异步客户端不再需要使用 swoole_async_dns_lookup 解析域名了,底层实现了自动域名解析。Cli...

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 模块使得 Reactor、Worker、Task 进程之间的通信方式更灵活,最大程度地解耦。复杂的线上项目使用 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();
  • Reactor 和 Worker 之间通信,使用 dispatch_mode = 7 来开启

  • Worker 和 Task 之间通信,使用 task_ipc_mode = 7 来开启

增加 Event::cycle 函数

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

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::incr 和 Table::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 格式的支持

下载地址:


【声明】文章转载自:开源中国社区 [http://www.oschina.net]


以上所述就是小编给大家介绍的《Swoole 1.10.0 发布,增加多项新特性》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Modeling the Internet and the Web

Modeling the Internet and the Web

Pierre Baldi、Paolo Frasconi、Padhraic Smyth / Wiley / 2003-7-7 / USD 115.00

Modeling the Internet and the Web covers the most important aspects of modeling the Web using a modern mathematical and probabilistic treatment. It focuses on the information and application layers, a......一起来看看 《Modeling the Internet and the Web》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具