A new version of Swoole 1.10. 0 was released with several new features
- 2021-09-04 23:39:59
- OfStack
Preface
Swoole can be widely used in Internet, mobile communication, enterprise software, cloud computing, online games, Internet of Things (IOT), Internet of Vehicles, smart home and other fields. Using PHP + Swoole as the network communication framework can greatly improve the efficiency of IT R&D team and focus more on developing innovative products. Recently, PHP's asynchronous, parallel, high-performance network communication engine, Swoole, has released version 1.10. 0. A number of new features have been added to this release. The following words are not much to say, let's take a look at the detailed introduction.
Automatic DNS parsing
The new version of asynchronous client no longer needs to use swoole_async_dns_lookup to resolve domain names, and the bottom layer realizes automatic domain name resolution. Client can pass in the domain name directly when executing the connect method.
$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";
});
// The underlying layer will automatically resolve asynchronous domain names
$client->connect('www.baidu.com', 9501);
Slow request log
The new version adds the function of tracking slow requests, which can record the PHP function call stack of slow requests.
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();
After processing the slow request, one line of error message will be printed in the/tmp/trace. log 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
Added STREAM module
The new stream module makes the communication mode among Reactor, Worker and Task processes more flexible and decouples to the greatest extent. Complex online projects use stream mode, which makes request allocation scheduling more efficient.
$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();
Communication between Reactor and Worker is enabled using dispatch_mode = 7
Communication between Worker and Task is enabled using task_ipc_mode = 4
Add Event:: cycle function
User code can customize 1 EventLoop hook function, this function will be called at the end of every 1 event cycle. Easy to implement your own scheduler using the Generator + Yield or Promise class Swoole framework.
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);
});
});
Other updates
Download address
GITHUB: https://github.com/swoole/swoole-src/releases/tag/v1.10. 0
Open source China: https://gitee.com/swoole/swoole/tree/v1.10. 0/
PECL: https://pecl.php.net/package/swoole/1. 10.0
Local download: http://xiazai.ofstack.com/201801/yuanma/swoole-src-1. 10.0 (ofstack.com). rar
Summarize