PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.1
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / vendor_prefixed / guzzlehttp / guzzle / src / RetryMiddleware.php

RetryMiddleware.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.1, at vendor_prefixed/guzzlehttp/guzzle/src/RetryMiddleware.php

100 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 YoastSEO_Vendor\GuzzleHttp;
4
5 use YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface;
6 use YoastSEO_Vendor\GuzzleHttp\Promise\RejectedPromise;
7 use YoastSEO_Vendor\GuzzleHttp\Psr7;
8 use YoastSEO_Vendor\Psr\Http\Message\RequestInterface;
9 use YoastSEO_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(\YoastSEO_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(\YoastSEO_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(\YoastSEO_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 \YoastSEO_Vendor\GuzzleHttp\Promise\rejection_for($reason);
87 }
88 return $this->doRetry($req, $options);
89 };
90 }
91 /**
92 * @return self
93 */
94 private function doRetry(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options, \YoastSEO_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