Swoole v4.6.5 版本發佈,增長原生curl multi支持

v4.6.5 版本沒有向下不兼容改動,主要對原生 curl hook 進行了一些加強,支持了 curl multiphp

  • 支持原生 curl multi

使用原生 curl hook 的前提是在編譯 Swoole 擴展時開啓--enable-swoole-curl選項html

能夠使用如下代碼進行測試:git

use Swoole\Runtime;
use function Swoole\Coroutine\run;

Runtime::enableCoroutine(SWOOLE_HOOK_NATIVE_CURL);
run(function () {
    $ch1 = curl_init();
    $ch2 = curl_init();

    // 設置URL和相應的選項
    curl_setopt($ch1, CURLOPT_URL, "http://www.baidu.com/");
    curl_setopt($ch1, CURLOPT_HEADER, 0);
    curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch2, CURLOPT_URL, "http://www.gov.cn/");
    curl_setopt($ch2, CURLOPT_HEADER, 0);
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);

    $mh = curl_multi_init();

    curl_multi_add_handle($mh, $ch1);
    curl_multi_add_handle($mh, $ch2);

    $active = null;
    // 執行批處理句柄
    do {
        $mrc = curl_multi_exec($mh, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);

    while ($active && $mrc == CURLM_OK) {
        $n = curl_multi_select($mh);
        if ($n != -1) {
            do {
                $mrc = curl_multi_exec($mh, $active);
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        }
    }

    $info1 = curl_multi_info_read($mh);
    $info2 = curl_multi_info_read($mh);
    $info3 = curl_multi_info_read($mh);

    assert($info1['msg'] === CURLMSG_DONE);
    assert($info2['msg'] === CURLMSG_DONE);
    assert($info3 === false);

    assert(strpos(curl_multi_getcontent($ch1),'baidu.com') !== false);
    assert(strpos(curl_multi_getcontent($ch2),'中央人民政府門戶網站') !== false);

    curl_multi_remove_handle($mh, $ch1);
    curl_multi_remove_handle($mh, $ch2);

    curl_multi_close($mh);
});

支持 curl multi 以後,也就間接的支持了 Guzzle,無需更改任何代碼,便可支持。github

include __DIR__ . '/vendor/autoload.php';

use Swoole\Coroutine\Barrier;
use Swoole\Runtime;
use GuzzleHttp\Client;
use GuzzleHttp\Promise;

use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;

Runtime::enableCoroutine(SWOOLE_HOOK_NATIVE_CURL);

const N = 4;

run(function () {
    $barrier = Barrier::make();
    $result = [];
    go(function () use ($barrier, &$result) {
        $client = new Client();
        $promises = [
            'baidu' => $client->getAsync('http://www.baidu.com/'),
            'qq' => $client->getAsync('https://www.qq.com/'),
            'gov' => $client->getAsync('http://www.gov.cn/')
        ];
        $responses = Promise\Utils::unwrap($promises);
        assert(strpos($responses['baidu']->getBody(),'百度') !== false);
        assert(strpos(iconv('gbk', 'utf-8', $responses['qq']->getBody()),'騰訊') !== false);
        assert(strpos($responses['gov']->getBody(),'中華人民共和國') !== false);
        $result['task_1'] = 'OK';
    });

    go(function () use ($barrier, &$result) {
        $client = new Client(['base_uri' => 'http://httpbin.org/']);
        $n = N;
        $data = $promises = [];
        while ($n--) {
            $key = 'req_' . $n;
            $data[$key] = uniqid('swoole_test');
            $promises[$key] = $client->getAsync('/base64/' . base64_encode($data[$key]));
        }
        $responses = Promise\Utils::unwrap($promises);

        $n = N;
        while ($n--) {
            $key = 'req_' . $n;
            assert($responses[$key]->getBody() === $data[$key]);
        }
        $result['task_2'] = 'OK';
    });

    Barrier::wait($barrier);
    assert($result['task_1'] === 'OK');
    assert($result['task_2'] === 'OK');
    echo 'Done' . PHP_EOL;
});

同時也還添加了一些 Guzzle 的單元測試。數組

  • 容許在使用 HTTP/2 的 Response 中使用數組設置 headers

v4.6.0 版本開始 Swoole\Http\Response 支持重複設置相同 $keyHTTP 頭,而且 $value 支持多種類型,如 arrayobjectintfloat,底層會進行 toString 轉換,而且會移除末尾的空格以及換行。promise

可是未支持 HTTP/2 的,詳情見 issue #4133bash

在此版本中也進行了支持:swoole

$http = new Swoole\Http\Server('127.0.0.1', 9501);
$http->set(['open_http2_protocol' => true]);

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

    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});
$http->start();

能夠使用以上代碼進行測試,並使用 curl 命令進行測試結果cookie

$ curl --http2-prior-knowledge -v http://localhost:9501
*   Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 9501 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9501 (#0)
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x7fe9e9009200)
> GET / HTTP/2
> Host: localhost:9501
> User-Agent: curl/7.64.1
> Accept: */*
>
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
< HTTP/2 200
< test-value: a
< test-value: d5678
< test-value: e
< test-value: 5678
< test-value: 3.1415926
< server: swoole-http-server
< date: Fri, 09 Apr 2021 11:04:39 GMT
< content-type: text/html
< content-length: 28
<
* Connection #0 to host localhost left intact
<h1>Hello Swoole. #6944</h1>* Closing connection 0

更新日誌

下面是完整的更新日誌:dom

新增 API

  • 在 WaitGroup 中增長 count 方法(swoole/library#100) (@sy-records) (@deminy)

加強

  • 支持原生 curl multi (#4093) (#4099) (#4101) (#4105) (#4113) (#4121) (#4147) (swoole/swoole-src@cd7f51c) (@matyhtf) (@sy-records) (@huanghantao)
  • 容許在使用 HTTP/2 的 Response 中使用數組設置 headers

修復

  • 修復 NetBSD 構建 (#4080) (@devnexen)
  • 修復 OpenBSD 構建 (#4108) (@devnexen)
  • 修復 illumos/solaris 構建,只有成員別名 (#4109) (@devnexen)
  • 修復握手未完成時,SSL 鏈接的心跳檢測不生效 (#4114) (@matyhtf)
  • 修復 Http\Client 使用代理時host中存在host:port產生的錯誤 (#4124) (@Yurunsoft)
  • 修復 Swoole\Coroutine\Http::request 中 header 和 cookie 的設置 (swoole/library#103) (@leocavalcante) (@deminy)

內核

  • 支持 BSD 上的 asm context (#4082) (@devnexen)
  • 在 FreeBSD 下使用 arc4random\_buf 來實現 getrandom (#4096) (@devnexen)
  • 優化 darwin arm64 context:刪除 workaround 使用 label (#4127) (@devnexen)

測試

  • 添加 alpine 的構建腳本 (#4104) (@limingxinleo)

相關文章
相關標籤/搜索