Swoole v4.6.0 release, support native curl coroutine client

Time:2021-2-26

Swoole v4.6.0Version is released, which is also the first version update in 2021.

Released as a Y version, this update also contains incompatible modifications and many new features. Let’s take a look at the changes?

Downward incompatible changes

  • fromv4.6.0Version will no longer be supportedPHP7.1

PHP official forPHP7.1The support of the government has already ended by the end of 2019.

  • takeEvent::rshutdown()Marked as obsolete, use insteadCoroutine\run

In previous versions, ifindex.phpDirect use ingoCreate a collaborative process

go(function () {
    var_dump(Co\System::gethostbyname('www.baidu.com'));
});

This is normal, but in this version, you will receive an discard warning

PHP Deprecated:  Swoole\Event::rshutdown(): Event::wait() in shutdown function is deprecated in Unknown on line 0

RecommendedCoroutine\runInstead of this way:

Swoole\Coroutine\run(function () {
    var_dump(Co\System::gethostbyname('www.baidu.com'));
});

Swoole\Coroutine\run(function () {
    go(function () {
        var_dump(Co\System::gethostbyname('www.baidu.com'));
    });
    go(function () {
        var_dump(Co\System::gethostbyname('www.zhihu.com'));
    });
});
  • Enabled by defaultCoroutine hook

Using the aboveCoroutine\runAfter that, there will be a new change: enabled by defaultCoroutineHook, that is, automatic settingSWOOLE_HOOK_ALL

use Swoole\Runtime;

Swoole\Coroutine\run(function () {
    $flags = Runtime::getHookFlags();
    assert($flags === SWOOLE_HOOK_ALL);
    var_dump($flags);
});

Of course, you can also set the required parameters by yourselfflag

use Swoole\Runtime;

Runtime::setHookFlags(SWOOLE_HOOK_TCP);
Swoole\Coroutine\run(function () {
    $flags = Runtime::getHookFlags();
    assert($flags === SWOOLE_HOOK_TCP);
    var_dump($flags);
});
  • Disable unsafe functions when using coroutines, includingpcntl_fork/pcntl_wait/pcntl_waitpid/pcntl_sigtimedwait
Swoole\Coroutine\run(function () {
    $pid = pcntl_fork();
    var_dump($pid);
});

Using the sample code above in this release, you’ll get aWarningerror

PHP Warning:  pcntl_fork() has been disabled for security reasons
  • Removedsession_idMaximum limit of, no more repetition

Thesession_idfromint24Change toint64In this way, it can continue to increase and never repeat.

previousint24When,session_idAbout 16 million, there may be repeated problems.

New API & enhancement

Native curl coroutine client (swoole)_ HOOK_ NATIVE_ CURL)

The biggest change in this version is the support for nativecurlWhat’s the use of coroutine client?

Used itSWOOLE_HOOK_CURLYou should know that there are some options that are not supported. At the same time, some SDK incompatibility may lead to some errors, such as:

PHP Notice:  Object of class Swoole\Curl\Handler could not be converted to int

PHP Warning: curl_multi_add_handle() expects parameter 2 to be resource, object given

The reason is after hookcurlIt’s no longer a problemresourceType, butobjectType.

In case of this problem, it is also recommended to contact the SDK to modify the codePHP8incurlNo longerresourceType, butobjecttype

And the frequently used Alibaba cloud OSS SDK is also not supportedSWOOLE_HOOK_CURLYes, there will be some strange things

So fromv4.6.0Version can be used from the beginningSWOOLE_HOOK_NATIVE_CURLreplaceSWOOLE_HOOK_CURLTo solve the problems mentioned above

useSWOOLE_HOOK_NATIVE_CURLIt needs to be added when compiling the spool extension–enable-swoole-curlCompile parameter, which will be set automatically after the option is turned onSWOOLE_HOOK_NATIVE_CURL, offSWOOLE_HOOK_CURLAt the same timeSWOOLE_HOOK_ALLIt will also includeSWOOLE_HOOK_NATIVE_CURL

PECL v4.6.0 does not add this option for the time being. Please use manual compilation to enable it. It will be added in the next version.

Use after successful compilation--riLook at the information and you’ll seecurl-native

$ php --ri swoole | grep curl
curl-native => enabled

The difference can be seen from the following examples

Swoole\Runtime::setHookFlags(SWOOLE_HOOK_CURL);
Swoole\Coroutine\run(function () {
    $curl = curl_init();
    var_dump(get_class($curl), (int) $curl);
    //PHP Notice:  Object of class Swoole\Curl\Handler could not be converted to int
    //string(19) "Swoole\Curl\Handler"
    //int(1)
});
Swoole\Coroutine\run(function () {
    $curl = curl_init();
    var_dump($curl, (int) $curl);
    //resource(4) of type (Swoole-Coroutine-cURL Handle)
    //int(4)
});

SWOOLE_HOOK_SOCKETS

Add the support for ext sockets

const N = 8;
$GLOBALS['time'] = [];
$s = microtime(true);
Swoole\Runtime::setHookFlags(SWOOLE_HOOK_SOCKETS);
Swoole\Coroutine\run(function () {
    $n = N;
    while($n--) {
        go(function() {
            $s = microtime(true);
            $domain = 'www.baidu.com';
            $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
            socket_connect($sock, $domain, 80);
            socket_write($sock, "GET / HTTP/1.0\r\nHost: $domain\r\nConnection: close\r\nKeep-Alive: off\r\n\r\n");

            $html = '';
            while(true) {
                $data = socket_read($sock, 8192);
                if ($data == '') {
                    break;
                }
                $html .= $data;
            }

            socket_close($sock);

            $GLOBALS['time'][] = microtime(true) - $s;
        });
    }
});
echo "Done\n";
var_dump(microtime(true) - $s, array_sum($GLOBALS['time']) / 3);

Support server event callback function to pass object style parameters

Not enabled by default. By settingevent_objectThe following event callbacks will use the object style

withonConnectFor example, please refer to the document for detailscallback object

$server->on('Connect', function (Swoole\Server $server, int $fd, int $reactorId) {
    var_dump($fd);
});

$server->set([
    'event_object' => true,
]);
$server->on('Connect', function (Swoole\Server $serv, Swoole\Server\Event $object) {
    var_dump($object);
});

Duplicate headers are supported

Support duplicate settings$keyAnd$valueSupport multiple types, such asarrayobjectintfloatAnd the bottom layer will do ittoStringConversion, and will remove the end of the space and newline

$http = new Swoole\Http\Server('0.0.0.0', 9501);

$http->on('request', function ($request, $response) {
    $response->header('Test-Value', [
        "a\r\n",
        'd5678',
        "e  \n ",
        null,
        5678,
        3.1415926,
    ]);
    $response->header('Foo', new SplFileInfo('bar'));
});

$http->start();
$ curl -I http://127.0.0.1:9501
HTTP/1.1 200 OK
Test-Value: a
Test-Value: d5678
Test-Value: e
Test-Value: 5678
Test-Value: 3.1415926
Foo: bar
Server: swoole-http-server
Connection: keep-alive
Content-Type: text/html
Date: Wed, 06 Jan 2021 05:16:17 GMT
Content-Length: 39

Coroutine deadlock detection

It is on by default. You can use theCoroutine::setSet inenable_deadlock_checkClose it

stayEventLoopAfter termination, if there is a coroutine deadlock, the underlying layer will output the relevant stack information

===================================================================
 [FATAL ERROR]: all coroutines (count: 1) are asleep - deadlock!
===================================================================

 [Coroutine-2]
--------------------------------------------------------------------
#0  Swoole\Coroutine::printBackTrace() called at [@swoole-src/library/core/Coroutine/functions.php:74]
#1  Swoole\Coroutine\deadlock_check()
#2  curl_getinfo() called at [/mnt/c/code/php/hyperf-skeleton/vendor/aliyuncs/oss-sdk-php/src/OSS/Http/RequestCore.php:492]

Update log

Here is the complete update log

Downward incompatible changes

  • Removedsession idThe maximum limit of (# 3879) (@ matyhtf)
  • Disable unsafe functions when using coroutines, includingpcntl_fork/pcntl_wait/pcntl_waitpid/pcntl_sigtimedwait (#3880) (@matyhtf)
  • Coroutine hook (# 3903) (@ matyhtf) is enabled by default

remove

  • Php7.1 (4a963df) (9de8d9e) (@ matyhtf) is no longer supported

Abandoned

  • takeEvent::rshutdown()Mark as obsolete, use coroutine / run (# 3881) (@ matyhtf)

New API

  • Support setpriority / getpriority (# 3876) (@ matyhtf)
  • Support native curl hook (# 3863) (@ matyhtf) (@ huanghantao)
  • Support server event callback function to pass object style parameters. By default, it does not pass object style parameters (# 3888) (@ matyhtf)
  • Support hook sockets extension (#3898) (@ matyhtf)
  • Support duplicate header (# 3905) (@ matyhtf)
  • Support SSL SNI (# 3908) (@ matyhtf)
  • Support hook stdio (#) (@ matyhtf)
  • Support stream_ Capture of socket_ peer_ CERT options (#) (@ matyhtf)
  • Add http / request:: create / parse / iscompleted (# 3938) (@ matyhtf)
  • Add http / response:: iswritable (db56827) (@ matyhtf)

enhance

  • All time precision of server is changed from int to double (# 3882) (@ matyhtf)
  • In swoole_ client_ Check the Eintr of the poll function in the select function (# 3909) (@ shiguangqi)
  • Add coroutine deadlock detection (# 3911) (@ matyhtf)
  • Support the use of swoole_ Base mode closes the connection in another process (# 3916) (@ matyhtf)
  • Optimize the performance of communication between server master process and worker process, reduce memory copy (# 3910) (@ huanghantao) (@ matyhtf)

repair

  • When coroutine / channel is closed, pop will display all the data in it (960431d) (@ matyhtf)
  • Fix memory errors when using JIT (# 3907) (@ twist)
  • repairport->set()Dtls compilation error (#3947) (@ yurunsoft)
  • Repair connection_ List error (#3948) (@ sy records)
  • Repair SSL verify (ා 3954) (@ matyhtf)
  • Fix the problem of not clearing all columns when table is incrementing and decrementing (# 3956) (@ matyhtf) (@ sy records)
  • Fix compilation failure using libresl2.7.5 (#3962) (@ matyhtf)
  • Fix undefined constant curlopt_ Headeropt and curlopt_ PROXYHEADER (swoole/library#77) (@sy-records)

kernel

  • Ignore SIGPIPE signal (9647678) (@ matyhtf) by default
  • Support running PHP coroutine and C coroutine (c94bfd8) (@ matyhtf)
  • Add get_ Elapsed test (#) (@ luolaifa000)
  • Add get_ init_ Msec test (# 3964) (@ luffloo)

Swoole v4.6.0 release, support native curl coroutine client

Recommended Today

Third party calls wechat payment interface

Step one: preparation 1. Wechat payment interface can only be called if the developer qualification has been authenticated on wechat open platform, so the first thing is to authenticate. It’s very simple, but wechat will charge 300 yuan for audit 2. Set payment directory Login wechat payment merchant platform( pay.weixin.qq . com) — > Product […]