[使用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;
};
}
try {
// Your code
} catch (\Exception $e) {
// Your code
} finally {
// Here add your variable count, for example: $count++
}
阅读有关official documentation的更多信息。