PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / RetryMiddleware.php

RetryMiddleware.php in Media Cloud Sync 1.3.11, at includes/sdk/s3/GuzzleHttp/RetryMiddleware.php

92 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\GuzzleHttp;
4
5 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise as P;
6 use Dudlewebs\WPMCS\s3\GuzzleHttp\Promise\PromiseInterface;
7 use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface;
8 use Dudlewebs\WPMCS\s3\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 ?: __CLASS__ . '::exponentialDelay';
44 }
45 /**
46 * Default exponential backoff delay function.
47 *
48 * @return int milliseconds.
49 */
50 public static function exponentialDelay(int $retries) : int
51 {
52 return (int) 2 ** ($retries - 1) * 1000;
53 }
54 public function __invoke(RequestInterface $request, array $options) : PromiseInterface
55 {
56 if (!isset($options['retries'])) {
57 $options['retries'] = 0;
58 }
59 $fn = $this->nextHandler;
60 return $fn($request, $options)->then($this->onFulfilled($request, $options), $this->onRejected($request, $options));
61 }
62 /**
63 * Execute fulfilled closure
64 */
65 private function onFulfilled(RequestInterface $request, array $options) : callable
66 {
67 return function ($value) use($request, $options) {
68 if (!($this->decider)($options['retries'], $request, $value, null)) {
69 return $value;
70 }
71 return $this->doRetry($request, $options, $value);
72 };
73 }
74 /**
75 * Execute rejected closure
76 */
77 private function onRejected(RequestInterface $req, array $options) : callable
78 {
79 return function ($reason) use($req, $options) {
80 if (!($this->decider)($options['retries'], $req, null, $reason)) {
81 return P\Create::rejectionFor($reason);
82 }
83 return $this->doRetry($req, $options);
84 };
85 }
86 private function doRetry(RequestInterface $request, array $options, ?ResponseInterface $response = null) : PromiseInterface
87 {
88 $options['delay'] = ($this->delay)(++$options['retries'], $response, $request);
89 return $this($request, $options);
90 }
91 }
92