【问题标题】:Guzzle Pool doesn't respect timeoutGuzzle Pool 不尊重超时
【发布时间】:2017-11-28 16:44:25
【问题描述】:

我的 guzzle 客户端有问题。我为例如 1.0 设置了超时,在某些路线上我做了 sleep(5)。无论如何,Guzzle 应该等待响应,什么时候应该抛出异常。 客户:

$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);
        }
    ]);

还有我的延迟路线:

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

我总是得到 504 和我的超时异常,而我不应该得到它,因为设置了超时。

我是通过设置客户端完成的,但这对我来说不是解决方案,因为我需要为某些请求而不是客户端自定义超时。

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

【问题讨论】:

    标签: php asynchronous request timeout guzzle


    【解决方案1】:

    我认为您记错了new Request() 的签名。来自文档:

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

    第三个参数用于 HTTP 标头,而不是选项。

    【讨论】:

      【解决方案2】:

      您应该在构造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);
          }
      ]);
      

      Pool 代码 here 的 cmets 中找到这个。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-02
        • 2012-07-04
        • 1970-01-01
        • 2023-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多