35 public function __construct(
37 callable $nextHandler,
38 callable $delay =
null
40 $this->decider = $decider;
41 $this->nextHandler = $nextHandler;
42 $this->delay = $delay ?: __CLASS__ .
'::exponentialDelay';
52 public static function exponentialDelay($retries)
54 return (
int) pow(2, $retries - 1) * 1000;
65 if (!isset($options[
'retries'])) {
66 $options[
'retries'] = 0;
69 $fn = $this->nextHandler;
70 return $fn($request, $options)
72 $this->onFulfilled($request, $options),
73 $this->onRejected($request, $options)
84 return function ($value) use ($req, $options) {
94 return $this->doRetry($req, $options, $value);
103 private function onRejected(RequestInterface $req, array $options)
105 return function ($reason) use ($req, $options) {
113 return \GuzzleHttp\Promise\rejection_for($reason);
115 return $this->doRetry($req, $options);
122 private function doRetry(RequestInterface $request, array $options, ResponseInterface $response =
null)
124 $options[
'delay'] = call_user_func($this->delay, ++$options[
'retries'], $response);
126 return $this($request, $options);