swoole-src
swoole-src copied to clipboard
guzzlehttp在stream=true时,设置超时无效
Please answer these questions before submitting your issue.
- 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();
- What did you expect to see? 发起一个请求:
time curl 127.0.0.1:9501
期望:在5秒内返回,代码抛出超时异常。
- What did you see instead? 超时设置并没有生效,超过1分钟才抛出异常。
time curl 127.0.0.1:9501
real 1m5.592s
user 0m0.009s
sys 0m0.003s
- 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
- 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 #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)
在同步模式下,或者guzzle设置stream=false时,超时配置都正常生效。
尝试在Swoole\Server::set设置default_socket_timeout = 5试试看
尝试在
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();
我晚上看看
这个最好看一下防火墙规则,我这边没复现出来
参考:
https://docs.guzzlephp.org/en/stable/request-options.html#connect-timeout
Note
This setting must be supported by the HTTP handler used to send a request. connect_timeout is currently only supported by the built-in cURL handler.
stream => true 时,guzzle 使用了 php stream,而不是 curl,所以不支持超时设置。