Cookie
3 years ago
Exception
3 years ago
Handler
3 years ago
Client.php
3 years ago
ClientInterface.php
3 years ago
HandlerStack.php
3 years ago
MessageFormatter.php
3 years ago
Middleware.php
3 years ago
Pool.php
3 years ago
PrepareBodyMiddleware.php
3 years ago
RedirectMiddleware.php
3 years ago
RequestOptions.php
3 years ago
RetryMiddleware.php
3 years ago
TransferStats.php
3 years ago
UriTemplate.php
3 years ago
Utils.php
3 years ago
functions.php
3 years ago
functions_include.php
3 years ago
RetryMiddleware.php
100 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP\Vendor\GuzzleHttp; |
| 4 | |
| 5 | use WPMailSMTP\Vendor\GuzzleHttp\Promise\PromiseInterface; |
| 6 | use WPMailSMTP\Vendor\GuzzleHttp\Promise\RejectedPromise; |
| 7 | use WPMailSMTP\Vendor\GuzzleHttp\Psr7; |
| 8 | use WPMailSMTP\Vendor\Psr\Http\Message\RequestInterface; |
| 9 | use WPMailSMTP\Vendor\Psr\Http\Message\ResponseInterface; |
| 10 | /** |
| 11 | * Middleware that retries requests based on the boolean result of |
| 12 | * invoking the provided "decider" function. |
| 13 | */ |
| 14 | class RetryMiddleware |
| 15 | { |
| 16 | /** @var callable */ |
| 17 | private $nextHandler; |
| 18 | /** @var callable */ |
| 19 | private $decider; |
| 20 | /** @var callable */ |
| 21 | private $delay; |
| 22 | /** |
| 23 | * @param callable $decider Function that accepts the number of retries, |
| 24 | * a request, [response], and [exception] and |
| 25 | * returns true if the request is to be |
| 26 | * retried. |
| 27 | * @param callable $nextHandler Next handler to invoke. |
| 28 | * @param callable $delay Function that accepts the number of retries |
| 29 | * and [response] and returns the number of |
| 30 | * milliseconds to delay. |
| 31 | */ |
| 32 | public function __construct(callable $decider, callable $nextHandler, callable $delay = null) |
| 33 | { |
| 34 | $this->decider = $decider; |
| 35 | $this->nextHandler = $nextHandler; |
| 36 | $this->delay = $delay ?: __CLASS__ . '::exponentialDelay'; |
| 37 | } |
| 38 | /** |
| 39 | * Default exponential backoff delay function. |
| 40 | * |
| 41 | * @param int $retries |
| 42 | * |
| 43 | * @return int milliseconds. |
| 44 | */ |
| 45 | public static function exponentialDelay($retries) |
| 46 | { |
| 47 | return (int) \pow(2, $retries - 1) * 1000; |
| 48 | } |
| 49 | /** |
| 50 | * @param RequestInterface $request |
| 51 | * @param array $options |
| 52 | * |
| 53 | * @return PromiseInterface |
| 54 | */ |
| 55 | public function __invoke(\WPMailSMTP\Vendor\Psr\Http\Message\RequestInterface $request, array $options) |
| 56 | { |
| 57 | if (!isset($options['retries'])) { |
| 58 | $options['retries'] = 0; |
| 59 | } |
| 60 | $fn = $this->nextHandler; |
| 61 | return $fn($request, $options)->then($this->onFulfilled($request, $options), $this->onRejected($request, $options)); |
| 62 | } |
| 63 | /** |
| 64 | * Execute fulfilled closure |
| 65 | * |
| 66 | * @return mixed |
| 67 | */ |
| 68 | private function onFulfilled(\WPMailSMTP\Vendor\Psr\Http\Message\RequestInterface $req, array $options) |
| 69 | { |
| 70 | return function ($value) use($req, $options) { |
| 71 | if (!\call_user_func($this->decider, $options['retries'], $req, $value, null)) { |
| 72 | return $value; |
| 73 | } |
| 74 | return $this->doRetry($req, $options, $value); |
| 75 | }; |
| 76 | } |
| 77 | /** |
| 78 | * Execute rejected closure |
| 79 | * |
| 80 | * @return callable |
| 81 | */ |
| 82 | private function onRejected(\WPMailSMTP\Vendor\Psr\Http\Message\RequestInterface $req, array $options) |
| 83 | { |
| 84 | return function ($reason) use($req, $options) { |
| 85 | if (!\call_user_func($this->decider, $options['retries'], $req, null, $reason)) { |
| 86 | return \WPMailSMTP\Vendor\GuzzleHttp\Promise\rejection_for($reason); |
| 87 | } |
| 88 | return $this->doRetry($req, $options); |
| 89 | }; |
| 90 | } |
| 91 | /** |
| 92 | * @return self |
| 93 | */ |
| 94 | private function doRetry(\WPMailSMTP\Vendor\Psr\Http\Message\RequestInterface $request, array $options, \WPMailSMTP\Vendor\Psr\Http\Message\ResponseInterface $response = null) |
| 95 | { |
| 96 | $options['delay'] = \call_user_func($this->delay, ++$options['retries'], $response); |
| 97 | return $this($request, $options); |
| 98 | } |
| 99 | } |
| 100 |