← All changes
|
vendor_prefixed/guzzlehttp/guzzle/src/RetryMiddleware.php
+0
-96
28.2
→
trunk
View file →
| @@ -1,96 +1,0 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace YoastSEO_Vendor\GuzzleHttp; | |
| 4 | - | |
| 5 | -use YoastSEO_Vendor\GuzzleHttp\Promise as P; | |
| 6 | -use YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface; | |
| 7 | -use YoastSEO_Vendor\Psr\Http\Message\RequestInterface; | |
| 8 | -use YoastSEO_Vendor\Psr\Http\Message\ResponseInterface; | |
| 9 | -/** | |
| 10 | - * Middleware that retries requests based on the boolean result of | |
| 11 | - * invoking the provided "decider" function. | |
| 12 | - * | |
| 13 | - * @final | |
| 14 | - */ | |
| 15 | -class RetryMiddleware | |
| 16 | -{ | |
| 17 | - /** | |
| 18 | - * @var callable(RequestInterface, array): PromiseInterface | |
| 19 | - */ | |
| 20 | - private $nextHandler; | |
| 21 | - /** | |
| 22 | - * @var callable | |
| 23 | - */ | |
| 24 | - private $decider; | |
| 25 | - /** | |
| 26 | - * @var callable(int) | |
| 27 | - */ | |
| 28 | - private $delay; | |
| 29 | - /** | |
| 30 | - * @param callable $decider Function that accepts the number of retries, | |
| 31 | - * a request, [response], and [exception] and | |
| 32 | - * returns true if the request is to be | |
| 33 | - * retried. | |
| 34 | - * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke. | |
| 35 | - * @param (callable(int): int)|null $delay Function that accepts the number of retries | |
| 36 | - * and returns the number of | |
| 37 | - * milliseconds to delay. | |
| 38 | - */ | |
| 39 | - public function __construct(callable $decider, callable $nextHandler, ?callable $delay = null) | |
| 40 | - { | |
| 41 | - $this->decider = $decider; | |
| 42 | - $this->nextHandler = $nextHandler; | |
| 43 | - $this->delay = $delay ?: static function (int $retries) : int { | |
| 44 | - return (int) 2 ** ($retries - 1) * 1000; | |
| 45 | - }; | |
| 46 | - } | |
| 47 | - /** | |
| 48 | - * Default exponential backoff delay function. | |
| 49 | - * | |
| 50 | - * @return int milliseconds. | |
| 51 | - * | |
| 52 | - * @deprecated since 7.11, will be removed in 8.0. | |
| 53 | - */ | |
| 54 | - public static function exponentialDelay(int $retries) : int | |
| 55 | - { | |
| 56 | - \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/guzzle', '7.11', '%s::%s() is deprecated and will be removed in 8.0.', __CLASS__, __FUNCTION__); | |
| 57 | - return (int) 2 ** ($retries - 1) * 1000; | |
| 58 | - } | |
| 59 | - public function __invoke(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface | |
| 60 | - { | |
| 61 | - if (!isset($options['retries'])) { | |
| 62 | - $options['retries'] = 0; | |
| 63 | - } | |
| 64 | - $fn = $this->nextHandler; | |
| 65 | - return $fn($request, $options)->then($this->onFulfilled($request, $options), $this->onRejected($request, $options)); | |
| 66 | - } | |
| 67 | - /** | |
| 68 | - * Execute fulfilled closure | |
| 69 | - */ | |
| 70 | - private function onFulfilled(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options) : callable | |
| 71 | - { | |
| 72 | - return function ($value) use($request, $options) { | |
| 73 | - if (!($this->decider)($options['retries'], $request, $value, null)) { | |
| 74 | - return $value; | |
| 75 | - } | |
| 76 | - return $this->doRetry($request, $options, $value); | |
| 77 | - }; | |
| 78 | - } | |
| 79 | - /** | |
| 80 | - * Execute rejected closure | |
| 81 | - */ | |
| 82 | - private function onRejected(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $req, array $options) : callable | |
| 83 | - { | |
| 84 | - return function ($reason) use($req, $options) { | |
| 85 | - if (!($this->decider)($options['retries'], $req, null, $reason)) { | |
| 86 | - return \YoastSEO_Vendor\GuzzleHttp\Promise\Create::rejectionFor($reason); | |
| 87 | - } | |
| 88 | - return $this->doRetry($req, $options); | |
| 89 | - }; | |
| 90 | - } | |
| 91 | - private function doRetry(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options, ?\YoastSEO_Vendor\Psr\Http\Message\ResponseInterface $response = null) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface | |
| 92 | - { | |
| 93 | - $options['delay'] = ($this->delay)(++$options['retries'], $response, $request); | |
| 94 | - return $this($request, $options); | |
| 95 | - } | |
| 96 | -} | |