简体   繁体   English

Guzzle Pool不尊重超时

[英]Guzzle Pool doesn't respect timeout

I have some problem with my guzzle client. 我的guzzle客户端有一些问题。 I set timeout for example 1.0 and in some route I do sleep(5). 我设置超时为例1.0,在某些路线我睡觉(5)。 Guzzle anyway wait on response when should just throw exception. Guzzle无论如何等待响应时应该抛出异常。 client: 客户:

$requests[] = new Request('GET', $path, [
        'timeout' => 1,
        'connect_timeout' => 1
    ]);

$pool = new Pool($this->client, $requests, [
        'concurrency' => 5,
        'fulfilled' => function ($response, $index) use ($response_merger) {
            $response_merger->fulfilled($response);
        },
        'rejected' => function ($reason, $index) use ($response_merger) {
            $response_merger->error($reason);
        }
    ]);

and my route with delay: 和我的延误路线:

$app->get('/timeout', function() use ($app) {
    sleep(5);
    return (new JsonResponse())->setData([ 'error' => 'My timeout exception.' ])->setStatusCode(504);
});

I always get 504 with My timeout exception, when I should not get it because timeout is set. 我总是得到504我的超时异常,当我不应该得到它因为超时设置。

I did it with set client, but it is not a solution for me because I need custom timeout for certain request, not client. 我用set client做了它,但它对我来说不是一个解决方案,因为我需要为某些请求自定义超时,而不是客户端。

$this->client = new Client([
        'timeout'  => 3.0,
        'connect_timeout'  => 1.0
    ]);

I think you've got the wrong signature in mind for new Request() . 对于new Request()我认为你的签名错了。 From the docs: 来自文档:

// Create a PSR-7 request object to send
$headers = ['X-Foo' => 'Bar'];
$body = 'Hello!';
$request = new Request('HEAD', 'http://httpbin.org/head', $headers, $body);

The third parameter is for HTTP headers, not options. 第三个参数用于HTTP标头,而不是选项。

You should pass timeout as an option when constructing the Pool : 在构造Pool时,您应该将timeout作为选项传递:

$pool = new Pool($this->client, $requests, [
    'concurrency' => 5,
    'options' => ['timeout' => 10],
    'fulfilled' => function ($response, $index) use ($response_merger) {
        $response_merger->fulfilled($response);
    },
    'rejected' => function ($reason, $index) use ($response_merger) {
        $response_merger->error($reason);
    }
]);

Found this in comments of the Pool code here . 这里Pool代码评论中找到了这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM