使用异步承诺处理 Guzzle 超时 [英] Handle Guzzle timeouts with async promises

查看:150
本文介绍了使用异步承诺处理 Guzzle 超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在发送多个异步承诺,并将 guzzle 超时设置为 30,我想知道如何捕获是否有任何承诺超时,以便我可以报告此错误.请看下面的代码.本质上,我想在超时之前使用我可以使用的任何响应并捕获那些超时的响应.

I am sending multiple async promises with a guzzle timeout set to 30, I am wondering how to capture if any promises timed out so that I can report on this error. Please see the code below. Essentially I want to use any responses I can before timeout and capture those that do timeout.

foreach ($apiRequests as $guzzleParameters) {
    $request = new Request($guzzleParameters->getType(), $guzzleParameters->getApiEndpoint(), $guzzleParameters->getHeaders(), $guzzleParameters->getBody());
    $promises[$guzzleParameters->createKey()] = $this->client->sendAsync($request)->then(
                function (ResponseInterface $res) {
                    return $res;
                },
                function (RequestException $e) {
                    switch ($e->getCode()) {
                        case 400:
                        // log error 
                            break;
                        case 401:
                        // log error 
                            break;
                        case 403:
                        // log error 
                            break;
                        case 404:
                        // log error 
                            break;
                    }
                    return $e->getResponse();
                }
            );
        }
$responses = Promise\Utils::settle($promises)->wait(true);

推荐答案

据我所知,您希望在异步中记录每个请求的时间,然后您可以将其累加并在总和达到 30 以上时放置一个标志.

As I have understood you want to record time of each request in async and then you can add it up and put a flag that when summation reaches more than 30.

注意:未测试的代码,但测试了 on_stats 部分的代码

NOTE: Non tested code but the on_stats portion code is tested

foreach ($apiRequests as $key => $guzzleParameters) {
    $LogStats = [];
    $request = new Request($guzzleParameters->getType(), $guzzleParameters->getApiEndpoint(), $guzzleParameters->getHeaders(), $guzzleParameters->getBody());
    $promises[$guzzleParameters->createKey()] = $this->client->sendAsync($request, [
                    'on_stats' => function (\GuzzleHttp\TransferStats $stats) {
                        // $LogStats["uri"] = $stats->getEffectiveUri();
                        $LogStats[$key]["time"] = $stats->getTransferTime();
                        // $LogStats["stats"] = json_encode($stats->getHandlerStats());
                        // Log::debug("\n--------------------------------\nDebug Solr Request Stats :: ", $LogStats);
                    }
                ])->then(
                function (ResponseInterface $res) {
                    return $res;
                },
                function (RequestException $e) {
                     // ....
                    }
                    return $e->getResponse();
                }
            );
        }
$responses = Promise\Utils::settle($promises)->wait(true);

这里我使用了 on_stats,你可以一直计数,然后总结,一旦达到 30 设置标志为真.然后你就可以知道请求失败了.

Here I have used on_stats, you can put a count on all the time and then sum it up and once it reaches 30 set flag as true. then you can know after which key the request failed.

这篇关于使用异步承诺处理 Guzzle 超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆