PluginProbe
Gmail SMTP / trunk
Gmail SMTP vtrunk
1.2.3.21 1.2.3.20 trunk 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.9 1.2.0 1.2.3.14 1.2.3.15 1.2.3.16 1.2.3.18 1.2.3.5
gmail-smtp / google-api-php-client / vendor / guzzlehttp / guzzle / src / RetryMiddleware.php

RetryMiddleware.php in Gmail SMTP trunk, at google-api-php-client/vendor/guzzlehttp/guzzle/src/RetryMiddleware.php

126 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace GuzzleHttp;
4
5 use GuzzleHttp\Promise as P;
6 use GuzzleHttp\Promise\PromiseInterface;
7 use Psr\Http\Message\RequestInterface;
8 use Psr\Http\Message\ResponseInterface;
9
10 /**
11 * Middleware that retries requests based on the boolean result of
12 * invoking the provided "decider" function.
13 *
14 * @final
15 */
16 class RetryMiddleware
17 {
18 /**
19 * @var callable(RequestInterface, array): PromiseInterface
20 */
21 private $nextHandler;
22
23 /**
24 * @var callable
25 */
26 private $decider;
27
28 /**
29 * @var callable(int)
30 */
31 private $delay;
32
33 /**
34 * @param callable $decider Function that accepts the number of retries,
35 * a request, [response], and [exception] and
36 * returns true if the request is to be
37 * retried.
38 * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke.
39 * @param (callable(int): int)|null $delay Function that accepts the number of retries
40 * and returns the number of
41 * milliseconds to delay.
42 */
43 public function __construct(callable $decider, callable $nextHandler, ?callable $delay = null)
44 {
45 $this->decider = $decider;
46 $this->nextHandler = $nextHandler;
47 $this->delay = $delay ?: static function (int $retries): int {
48 return (int) 2 ** ($retries - 1) * 1000;
49 };
50 }
51
52 /**
53 * Default exponential backoff delay function.
54 *
55 * @return int milliseconds.
56 *
57 * @deprecated since 7.11, will be removed in 8.0.
58 */
59 public static function exponentialDelay(int $retries): int
60 {
61 \trigger_deprecation('guzzlehttp/guzzle', '7.11', '%s::%s() is deprecated and will be removed in 8.0.', __CLASS__, __FUNCTION__);
62
63 return (int) 2 ** ($retries - 1) * 1000;
64 }
65
66 public function __invoke(RequestInterface $request, array $options): PromiseInterface
67 {
68 if (!isset($options['retries'])) {
69 $options['retries'] = 0;
70 }
71
72 $fn = $this->nextHandler;
73
74 return $fn($request, $options)
75 ->then(
76 $this->onFulfilled($request, $options),
77 $this->onRejected($request, $options)
78 );
79 }
80
81 /**
82 * Execute fulfilled closure
83 */
84 private function onFulfilled(RequestInterface $request, array $options): callable
85 {
86 return function ($value) use ($request, $options) {
87 if (!($this->decider)(
88 $options['retries'],
89 $request,
90 $value,
91 null
92 )) {
93 return $value;
94 }
95
96 return $this->doRetry($request, $options, $value);
97 };
98 }
99
100 /**
101 * Execute rejected closure
102 */
103 private function onRejected(RequestInterface $req, array $options): callable
104 {
105 return function ($reason) use ($req, $options) {
106 if (!($this->decider)(
107 $options['retries'],
108 $req,
109 null,
110 $reason
111 )) {
112 return P\Create::rejectionFor($reason);
113 }
114
115 return $this->doRetry($req, $options);
116 };
117 }
118
119 private function doRetry(RequestInterface $request, array $options, ?ResponseInterface $response = null): PromiseInterface
120 {
121 $options['delay'] = ($this->delay)(++$options['retries'], $response, $request);
122
123 return $this($request, $options);
124 }
125 }
126