Skip to content

Commit da7cee0

Browse files
committed
Fix Guzzle retry stack
1 parent f2b43b9 commit da7cee0

File tree

1 file changed

+29
-55
lines changed

1 file changed

+29
-55
lines changed

src/HttpClient/GuzzleClient.php

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use GuzzleHttp\Exception\RequestException;
2525
use GuzzleHttp\Exception\TransferException;
2626
use GuzzleHttp\HandlerStack;
27+
use GuzzleHttp\Middleware;
2728
use GuzzleHttp\Promise\EachPromise;
2829
use GuzzleHttp\Psr7\Request;
2930
use GuzzleHttp\Psr7\Response;
@@ -74,13 +75,40 @@ private function getClient()
7475
if (!$this->client) {
7576
// Initialize Guzzle and the retry middleware, include the default options
7677
$stack = HandlerStack::create(\GuzzleHttp\choose_handler());
77-
$stack->push(\GuzzleHttp\Middleware::retry(static::createRetryHandler()));
78+
$stack->push(Middleware::retry(function (
79+
$retries,
80+
Request $request,
81+
Response $response = null,
82+
RequestException $exception = null
83+
) {
84+
// Limit the number of retries to 5
85+
if ($retries >= 5) {
86+
return false;
87+
}
88+
89+
// Retry connection exceptions
90+
if ($exception instanceof ConnectException) {
91+
return true;
92+
}
93+
94+
if ($response) {
95+
// Retry on server errors
96+
if ($response->getStatusCode() >= 500) {
97+
return true;
98+
}
99+
}
100+
101+
return false;
102+
}, function ($retries) {
103+
return $retries * 1000;
104+
}));
78105
$guzzle = new Client(array_merge(
79106
$this->defaultOptions,
80107
[
81108
'timeout' => $this->timeout,
82109
'connect_timeout' => $this->connectTimeout,
83110
'http_errors' => false,
111+
'handler' => $stack,
84112
]
85113
));
86114

@@ -377,58 +405,4 @@ public function doRequests($requests = [])
377405

378406
return $responses;
379407
}
380-
381-
/**
382-
* Create the retry handler
383-
*
384-
* @return \Closure
385-
*/
386-
protected function createRetryHandler()
387-
{
388-
return function (
389-
$retries,
390-
Request $request,
391-
Response $response = null,
392-
RequestException $exception = null
393-
) {
394-
if ($retries >= $this->maxRetries) {
395-
return false;
396-
}
397-
if (!(static::isServerError($response) || static::isConnectError($exception))) {
398-
return false;
399-
}
400-
401-
return true;
402-
};
403-
}
404-
405-
/**
406-
* @param Response $response
407-
*
408-
* @return bool
409-
*/
410-
protected function isServerError(Response $response = null)
411-
{
412-
if ($response instanceof Response) {
413-
$content = (string) $response->getBody();
414-
if (strpos($content, 'The Service is temporarily unavailable') !== false) {
415-
sleep(2); // Been sending too many requests, go for a short break
416-
}
417-
} else {
418-
return false;
419-
}
420-
421-
return $response->getStatusCode() >= 500
422-
|| strpos($content, 'The Service is temporarily unavailable') !== false;
423-
}
424-
425-
/**
426-
* @param RequestException $exception
427-
*
428-
* @return bool
429-
*/
430-
protected function isConnectError(RequestException $exception = null)
431-
{
432-
return $exception instanceof ConnectException;
433-
}
434408
}

0 commit comments

Comments
 (0)