PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
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 28.5, at vendor_prefixed/guzzlehttp/guzzle/src/RetryMiddleware.php

97 lines 3.9 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 as P;
6 use YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface;
7 use YoastSEO_Vendor\Psr\Http\Message\RequestInterface;
8 use YoastSEO_Vendor\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 ?: static function (int $retries) : int {
44 return (int) 2 ** ($retries - 1) * 1000;
45 };
46 }
47 /**
48 * Default exponential backoff delay function.
49 *
50 * @return int milliseconds.
51 *
52 * @deprecated since 7.11, will be removed in 8.0.
53 */
54 public static function exponentialDelay(int $retries) : int
55 {
56 \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/guzzle', '7.11', '%s::%s() is deprecated and will be removed in 8.0.', __CLASS__, __FUNCTION__);
57 return (int) 2 ** ($retries - 1) * 1000;
58 }
59 public function __invoke(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface
60 {
61 if (!isset($options['retries'])) {
62 $options['retries'] = 0;
63 }
64 $fn = $this->nextHandler;
65 return $fn($request, $options)->then($this->onFulfilled($request, $options), $this->onRejected($request, $options));
66 }
67 /**
68 * Execute fulfilled closure
69 */
70 private function onFulfilled(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options) : callable
71 {
72 return function ($value) use($request, $options) {
73 if (!($this->decider)($options['retries'], $request, $value, null)) {
74 return $value;
75 }
76 return $this->doRetry($request, $options, $value);
77 };
78 }
79 /**
80 * Execute rejected closure
81 */
82 private function onRejected(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $req, array $options) : callable
83 {
84 return function ($reason) use($req, $options) {
85 if (!($this->decider)($options['retries'], $req, null, $reason)) {
86 return \YoastSEO_Vendor\GuzzleHttp\Promise\Create::rejectionFor($reason);
87 }
88 return $this->doRetry($req, $options);
89 };
90 }
91 private function doRetry(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options, ?\YoastSEO_Vendor\Psr\Http\Message\ResponseInterface $response = null) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface
92 {
93 $options['delay'] = ($this->delay)(++$options['retries'], $response, $request);
94 return $this($request, $options);
95 }
96 }
97