swoole / swoole-src

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

Re-use curl connection #5101

Open piunu opened 11 months ago

piunu commented 11 months ago

Hi, I need some help, please.

Please answer these questions before submitting your issue.

  1. What did you do? If possible, provide a simple script for reproducing the error.
<?php

Swoole\Runtime::enableCoroutine(true, SWOOLE_HOOK_NATIVE_CURL);

Swoole\Coroutine\run(function () {
    checkReuse();
});

function checkReuse()
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, 'https://example.com');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    curl_exec($curl);
    curl_exec($curl);

    $connectionTime = curl_getinfo($curl, CURLINFO_PRETRANSFER_TIME);

    if ($connectionTime < 0.01) {
        echo 'Curl re-use connection: ' . $connectionTime . ' sec';
    } else {
        echo 'Curl create new connection: ' . $connectionTime . ' sec';
    }

    echo "\r\n";
}
  1. What did you expect to see?

I expect curl to reuse the connection in the second request.

  1. What did you see instead?

I see that curl creates a new connection for each request. The problem is only with SWOOLE_HOOK_NATIVE_CURL. When using SWOOLE_HOOK_CURL or curl without swoole, there is no problem. I checked on Ubuntu 20.04, 22.04 and Debian 11, and on older versions of swoole.

  1. What version of Swoole are you using (show your php --ri swoole)?
swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 5.1.0-dev
Built => Jun 22 2023 01:44:22
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
sockets => enabled
openssl => OpenSSL 1.1.1n  15 Mar 2022
dtls => enabled
http2 => enabled
json => enabled
curl-native => enabled
zlib => 1.2.11
brotli => E16777225/D16777225
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
mysqlnd => enabled
async_redis => enabled
coroutine_pgsql => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_fiber_mock => Off => Off
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608
  1. What is your machine environment used (show your uname -a & php -v & gcc -v) ?
Linux ip-172-31-22-1 5.10.0-23-cloud-amd64 #1 SMP Debian 5.10.179-1 (2023-05-12) x86_64 GNU/Linux
PHP 8.2.7 (cli) (built: Jun  9 2023 07:37:35) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.7, Copyright (c), by Zend Technologies`
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/10/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 10.2.1-6' --with-bugurl=file:///usr/share/doc/gcc-10/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-10 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-10-Km9U7s/gcc-10-10.2.1/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-10-Km9U7s/gcc-10-10.2.1/debian/tmp-gcn/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-mutex
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.1 20210110 (Debian 10.2.1-6) 
NathanFreeman commented 11 months ago

Can not share the same curl connection between coroutines.

NathanFreeman commented 11 months ago

Each request needs to create different connections and maintain IO state of different connections.