From a44cc1d2e3c0f147e91a5c052ac7fd879e34e706 Mon Sep 17 00:00:00 2001 From: Eole Date: Thu, 21 Jan 2016 10:29:26 +0100 Subject: Init Server Composer Components --- .../guzzlehttp/guzzle/src/RetryMiddleware.php | 111 +++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 server/vendor/guzzlehttp/guzzle/src/RetryMiddleware.php (limited to 'server/vendor/guzzlehttp/guzzle/src/RetryMiddleware.php') diff --git a/server/vendor/guzzlehttp/guzzle/src/RetryMiddleware.php b/server/vendor/guzzlehttp/guzzle/src/RetryMiddleware.php new file mode 100644 index 0000000..4b95a14 --- /dev/null +++ b/server/vendor/guzzlehttp/guzzle/src/RetryMiddleware.php @@ -0,0 +1,111 @@ +decider = $decider; + $this->nextHandler = $nextHandler; + $this->delay = $delay ?: __CLASS__ . '::exponentialDelay'; + } + + /** + * Default exponential backoff delay function. + * + * @param $retries + * + * @return int + */ + public static function exponentialDelay($retries) + { + return (int) pow(2, $retries - 1); + } + + /** + * @param RequestInterface $request + * @param array $options + * + * @return PromiseInterface + */ + public function __invoke(RequestInterface $request, array $options) + { + if (!isset($options['retries'])) { + $options['retries'] = 0; + } + + $fn = $this->nextHandler; + return $fn($request, $options) + ->then( + $this->onFulfilled($request, $options), + $this->onRejected($request, $options) + ); + } + + private function onFulfilled(RequestInterface $req, array $options) + { + return function ($value) use ($req, $options) { + if (!call_user_func( + $this->decider, + $options['retries'], + $req, + $value, + null + )) { + return $value; + } + return $this->doRetry($req, $options); + }; + } + + private function onRejected(RequestInterface $req, array $options) + { + return function ($reason) use ($req, $options) { + if (!call_user_func( + $this->decider, + $options['retries'], + $req, + null, + $reason + )) { + return new RejectedPromise($reason); + } + return $this->doRetry($req, $options); + }; + } + + private function doRetry(RequestInterface $request, array $options) + { + $options['delay'] = call_user_func($this->delay, ++$options['retries']); + + return $this($request, $options); + } +} -- cgit v1.2.3