簡體   English   中英

Guzzle Pool不尊重超時

[英]Guzzle Pool doesn't respect timeout

我的guzzle客戶端有一些問題。 我設置超時為例1.0,在某些路線我睡覺(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我的超時異常,當我不應該得到它因為超時設置。

我用set client做了它,但它對我來說不是一個解決方案,因為我需要為某些請求自定義超時,而不是客戶端。

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

對於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標頭,而不是選項。

在構造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代碼評論中找到了這個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM