获取Guzzle的RetryMiddleware已完成的重试次数

问题描述 投票:1回答:2

[使用Middleware::retry()时,有什么方法可以获取已完成的重试次数?我正在使用Guzzle来对外部站点执行ping操作,以作为正常运行时间检测的一种方式,对于我们有一个失败或成功的请求之后,我尝试了多少次重试是我很感兴趣的。

客户端设置和请求

$stack = HandlerStack::create();
$stack->push(Middleware::retry(retryDecide(), $retryDelay()));
try {
  $client->request('GET', 'https://mydomain.ext', [
    'exceptions' => false,
    'on_stats' => function (TransferStats $stats) {
      if ($stats->hasResponse()) {

        // We have a response, I would like to get the retries here

      }
    }
  ]);
} catch (\Exception $e) {

    // We failed probably because of networking problems, also want retries here. 

}

retryDecide()

  function retryDecide()
  {
    return function (
      $retries,
      Request $request,
      Response $response = null,
      RequestException $exception = null
    ) {
      if ($retries >= 5) {
        return false;
      }

      if ($exception instanceof ConnectException) {
        return true;
      }

      return false;
    };
  }

retryDelay()

function retryDelay()
  {
    return function ($numberOfRetries) {
      return 1000 * $numberOfRetries;
    };
  }
php guzzle
2个回答
0
投票
如果可以,您可以这样做:

try { // Your code } catch (\Exception $e) { // Your code } finally { // Here add your variable count, for example: $count++ }

阅读有关official documentation的更多信息。


0
投票
© www.soinside.com 2019 - 2024. All rights reserved.