Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

guzzlehttp在stream=true时,设置超时无效 #5326

Open
huelsgp27 opened this issue May 16, 2024 · 5 comments
Open

guzzlehttp在stream=true时,设置超时无效 #5326

huelsgp27 opened this issue May 16, 2024 · 5 comments

Comments

@huelsgp27
Copy link

Please answer these questions before submitting your issue.

  1. What did you do? If possible, provide a simple script for reproducing the error.
    使用guzzleHttp请求一个被防火墙拦截的端口:
<?php
use GuzzleHttp\Client;

require 'vendor/autoload.php';

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

            $http->on('request', function ($request, $response) {
                $result = [];
                $client = new Client([
                    'timeout' => 5,
                    'connect_timeout' => 5,
                    'read_timeout' => 3,
                    'stream' => true
                ]);
                print_r(date("Y-m-d H:i:s")." 准备发起请求\n");
                $client->post("https://i.cmd.center:38000");
                print_r(date("Y-m-d H:i:s")." 发起请求之后\n");
                $response->end(json_encode($result));
            });
echo "http start";
$http->start();
  1. What did you expect to see?
    发起一个请求:
time curl 127.0.0.1:9501

期望:在5秒内返回,代码抛出超时异常。

  1. What did you see instead?
    超时设置并没有生效,超过1分钟才抛出异常。
 time curl 127.0.0.1:9501

real    1m5.592s
user    0m0.009s
sys     0m0.003s
  1. What version of Swoole are you using (show your php --ri swoole)?
    swoole

Swoole => enabled
Author => Swoole Team [email protected]
Version => 5.1.2
Built => Apr 29 2024 21:13:47
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
sockets => enabled
openssl => OpenSSL 3.0.11 19 Sep 2023
dtls => enabled
http2 => enabled
json => enabled
curl-native => enabled
zlib => 1.2.13
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) ?
    PHP 8.2.15 (cli) (built: Feb 13 2024 06:34:40) (NTS)
    Linux 462719e020fd 5.15.0-71-generic examples/client/long_tcp.php 长连接有问题 #78-Ubuntu SMP Tue Apr 18 09:00:29 UTC 2023 x86_64 GNU/Linux
    gcc version 12.2.0 (Debian 12.2.0-14)
@huelsgp27
Copy link
Author

在同步模式下,或者guzzle设置stream=false时,超时配置都正常生效。

@NathanFreeman
Copy link
Member

尝试在Swoole\Server::set设置default_socket_timeout = 5试试看

@huelsgp27
Copy link
Author

尝试在Swoole\Server::set设置default_socket_timeout = 5试试看

这个应该是ini_set的选项吧,设置后有点奇怪,超时时间为12秒,刚好等于socket_timeout + timeout.

<?php
use GuzzleHttp\Client;

require 'vendor/autoload.php';
Co::set([
    'socket_connect_timeout' => 2,
    'socket_timeout' => 4
]);
ini_set('default_socket_timeout', 1);
$http = new Swoole\Http\Server('127.0.0.1', 9501);
            $http->set(['hook_flags' => SWOOLE_HOOK_ALL]);

            $http->on('request', function ($request, $response) {
                $result = [];
                $client = new Client([
                    'timeout' => 8,
                    'connect_timeout' => 3,
                   // 'read_timeout' => 3,
                    'stream' => true
                ]);
                print_r(date("Y-m-d H:i:s")." 准备发起请求\n");
                $client->post("https://i.cmd.center:38000");
                print_r(date("Y-m-d H:i:s")." 发起请求之后\n");
                $response->end(json_encode($result));
            });
echo "http start";
$http->start();

@NathanFreeman
Copy link
Member

我晚上看看

@NathanFreeman
Copy link
Member

这个最好看一下防火墙规则,我这边没复现出来

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants