| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\GuzzleHttp; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromiseInterface; |
| 6 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\RejectedPromise; |
| 7 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7; |
| 8 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface; |
| 9 |
use Dudlewebs\WPMCS\s3\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(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(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(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 \Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\rejection_for($reason); |
| 87 |
} |
| 88 |
return $this->doRetry($req, $options); |
| 89 |
}; |
| 90 |
} |
| 91 |
/** |
| 92 |
* @return self |
| 93 |
*/ |
| 94 |
private function doRetry(RequestInterface $request, array $options, ResponseInterface $response = null) |
| 95 |
{ |
| 96 |
$options['delay'] = \call_user_func($this->delay, ++$options['retries'], $response); |
| 97 |
return $this($request, $options); |
| 98 |
} |
| 99 |
} |
| 100 |
|