关于php:Swoole-v465-版本发布增加原生curl-multi支持

38次阅读

共计 4698 个字符,预计需要花费 12 分钟才能阅读完成。

v4.6.5 版本没有向下不兼容改变,次要对原生 curl hook 进行了一些加强,反对了 curl multi

  • 反对原生 curl multi

应用原生 curl hook 的前提是在编译 Swoole 扩大时开启 --enable-swoole-curl 选项

能够应用以下代码进行测试:

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,无需更改任何代码,即可反对。

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 转换,并且会移除开端的空格以及换行。

然而未反对 HTTP/2 的,详情见 issue #4133

在此版本中也进行了反对:

$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 命令进行测试后果

$ 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

更新日志

上面是残缺的更新日志:

新增 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)

正文完
 0