swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.27k stars 3.16k forks source link

curl协程化无法通过curl_getinfo获取tcp连接时长等数据 #5232

Closed netcccyun closed 4 months ago

netcccyun commented 4 months ago

Swoole版本:4.8.13 PHP版本:7.4.33 操作系统版本:CentOS 7.9

相关代码:

Co::set(['hook_flags'=> SWOOLE_HOOK_ALL]);
Co\run(function() {
    go(function (){
        $ch=curl_init('https://www.swoole.com/');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_exec($ch);
        print_r(curl_getinfo($ch));
        curl_close($ch);
    });
});
Swoole\Event::wait();

在使用上述curl协程化的代码,输出的namelookup_time、connect_time、pretransfer_time都是0,也就是获取不到这些耗时数据。只能拿到一个总耗时total_time。

15837_658d11154d69d

但是不用协程的curl是可以拿到这些耗时数据的。有没有办法在协程里面拿到这些耗时数据?

NathanFreeman commented 4 months ago

我晚上回去看一下

netcccyun commented 4 months ago

你好,请问看了吗

NathanFreeman commented 4 months ago

@netcccyun 编译swoole的时候加上--enable-swoole-curl就可以了,你现在用的是以前协程化curl的方法

matyhtf commented 4 months ago
Array
(
    [url] => https://www.swoole.com/
    [content_type] => text/html; charset=UTF-8
    [http_code] => 200
    [header_size] => 1151
    [request_size] => 53
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.624566
    [namelookup_time] => 0.058743
    [connect_time] => 0.090391
    [pretransfer_time] => 0.153779
    [size_upload] => 0
    [size_download] => 71309
    [speed_download] => 114277
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 0.306561
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 180.97.70.235
    [certinfo] => Array
        (
        )

    [primary_port] => 443
    [local_ip] => 192.168.1.2
    [local_port] => 42470
    [http_version] => 2
    [protocol] => 2
    [ssl_verifyresult] => 0
    [scheme] => HTTPS
    [appconnect_time_us] => 153726
    [connect_time_us] => 90391
    [namelookup_time_us] => 58743
    [pretransfer_time_us] => 153779
    [redirect_time_us] => 0
    [starttransfer_time_us] => 306561
    [total_time_us] => 624566
)

需要使用 native curl