|
24 | 24 | use GuzzleHttp\Exception\RequestException; |
25 | 25 | use GuzzleHttp\Exception\TransferException; |
26 | 26 | use GuzzleHttp\HandlerStack; |
| 27 | +use GuzzleHttp\Middleware; |
27 | 28 | use GuzzleHttp\Promise\EachPromise; |
28 | 29 | use GuzzleHttp\Psr7\Request; |
29 | 30 | use GuzzleHttp\Psr7\Response; |
@@ -74,13 +75,40 @@ private function getClient() |
74 | 75 | if (!$this->client) { |
75 | 76 | // Initialize Guzzle and the retry middleware, include the default options |
76 | 77 | $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 | + })); |
78 | 105 | $guzzle = new Client(array_merge( |
79 | 106 | $this->defaultOptions, |
80 | 107 | [ |
81 | 108 | 'timeout' => $this->timeout, |
82 | 109 | 'connect_timeout' => $this->connectTimeout, |
83 | 110 | 'http_errors' => false, |
| 111 | + 'handler' => $stack, |
84 | 112 | ] |
85 | 113 | )); |
86 | 114 |
|
@@ -377,58 +405,4 @@ public function doRequests($requests = []) |
377 | 405 |
|
378 | 406 | return $responses; |
379 | 407 | } |
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 | | - } |
434 | 408 | } |
0 commit comments