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

92 lines 3.7 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 ?: __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(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options) : \YoastSEO_Vendor\GuzzleHttp\Promise\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(\YoastSEO_Vendor\Psr\Http\Message\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(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $req, array $options) : callable
78 {
79 return function ($reason) use($req, $options) {
80 if (!($this->decider)($options['retries'], $req, null, $reason)) {
81 return \YoastSEO_Vendor\GuzzleHttp\Promise\Create::rejectionFor($reason);
82 }
83 return $this->doRetry($req, $options);
84 };
85 }
86 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
87 {
88 $options['delay'] = ($this->delay)(++$options['retries'], $response, $request);
89 return $this($request, $options);
90 }
91 }
92