Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/RetryMiddleware.php

raw symbols SCIP
1 <?php
2 namespace Aws;
3
4 use Aws\Exception\AwsException;
5 use Aws\Retry\RetryHelperTrait;
6 use GuzzleHttp\Exception\RequestException;
7 use Psr\Http\Message\RequestInterface;
8 use GuzzleHttp\Promise\PromiseInterface;
9 use GuzzleHttp\Promise;
10
11 /**
12 * Middleware that retries failures. V1 implemention that supports 'legacy' mode.
13 *
14 * @internal
15 */
16 class RetryMiddleware
17 {
18 use RetryHelperTrait;
19
20 private static $retryStatusCodes = [
21 500 => true,
22 502 => true,
23 503 => true,
24 504 => true
25 ];
26
27 private static $retryCodes = [
28 // Throttling error
29 'RequestLimitExceeded' => true,
30 'Throttling' => true,
31 'ThrottlingException' => true,
32 'ThrottledException' => true,
33 'ProvisionedThroughputExceededException' => true,
34 'RequestThrottled' => true,
35 'BandwidthLimitExceeded' => true,
36 'RequestThrottledException' => true,
37 'TooManyRequestsException' => true,
38 'IDPCommunicationError' => true,
39 'EC2ThrottledException' => true,
40 ];
41
42 private $decider;
43 private $delay;
44 private $nextHandler;
45 private $collectStats;
46
47 public function __construct(
48 callable $decider,
49 callable $delay,
50 callable $nextHandler,
51 $collectStats = false
52 ) {
53 $this->decider = $decider;
54 $this->delay = $delay;
55 $this->nextHandler = $nextHandler;
56 $this->collectStats = (bool) $collectStats;
57 }
58
59 /**
60 * Creates a default AWS retry decider function.
61 *
62 * The optional $extraConfig parameter is an associative array
63 * that specifies additional retry conditions on top of the ones specified
64 * by default by the Aws\RetryMiddleware class, with the following keys:
65 *
66 * - errorCodes: (string[]) An indexed array of AWS exception codes to retry.
67 * Optional.
68 * - statusCodes: (int[]) An indexed array of HTTP status codes to retry.
69 * Optional.
70 * - curlErrors: (int[]) An indexed array of Curl error codes to retry. Note
71 * these should be valid Curl constants. Optional.
72 *
73 * @param int $maxRetries
74 * @param array $extraConfig
75 * @return callable
76 */
77 public static function createDefaultDecider(
78 $maxRetries = 3,
79 $extraConfig = []
80 ) {
81 $retryCurlErrors = [];
82 if (extension_loaded('curl')) {
83 $retryCurlErrors[CURLE_RECV_ERROR] = true;
84 }
85
86 return function (
87 $retries,
88 CommandInterface $command,
89 RequestInterface $request,
90 ?ResultInterface $result = null,
91 $error = null
92 ) use ($maxRetries, $retryCurlErrors, $extraConfig) {
93 // Allow command-level options to override this value
94 $maxRetries = null !== $command['@retries'] ?
95 $command['@retries']
96 : $maxRetries;
97
98 $isRetryable = self::isRetryable(
99 $result,
100 $error,
101 $retryCurlErrors,
102 $extraConfig
103 );
104
105 if ($retries >= $maxRetries) {
106 if (!empty($error)
107 && $error instanceof AwsException
108 && $isRetryable
109 ) {
110 $error->setMaxRetriesExceeded();
111 }
112 return false;
113 }
114
115 return $isRetryable;
116 };
117 }
118
119 private static function isRetryable(
120 $result,
121 $error,
122 $retryCurlErrors,
123 $extraConfig = []
124 ) {
125 $errorCodes = self::$retryCodes;
126 if (!empty($extraConfig['error_codes'])
127 && is_array($extraConfig['error_codes'])
128 ) {
129 foreach($extraConfig['error_codes'] as $code) {
130 $errorCodes[$code] = true;
131 }
132 }
133
134 $statusCodes = self::$retryStatusCodes;
135 if (!empty($extraConfig['status_codes'])
136 && is_array($extraConfig['status_codes'])
137 ) {
138 foreach($extraConfig['status_codes'] as $code) {
139 $statusCodes[$code] = true;
140 }
141 }
142
143 if (!empty($extraConfig['curl_errors'])
144 && is_array($extraConfig['curl_errors'])
145 ) {
146 foreach($extraConfig['curl_errors'] as $code) {
147 $retryCurlErrors[$code] = true;
148 }
149 }
150
151 if (!$error) {
152 if (!isset($result['@metadata']['statusCode'])) {
153 return false;
154 }
155 return isset($statusCodes[$result['@metadata']['statusCode']]);
156 }
157
158 if (!($error instanceof AwsException)) {
159 return false;
160 }
161
162 if ($error->isConnectionError()) {
163 return true;
164 }
165
166 $awsCode = $error->getAwsErrorCode();
167 if (!is_null($awsCode) && isset($errorCodes[$awsCode])) {
168 return true;
169 }
170
171 $status = $error->getStatusCode();
172 if (!is_null($status) && isset($statusCodes[$status])) {
173 return true;
174 }
175
176 if (count($retryCurlErrors)
177 && ($previous = $error->getPrevious())
178 && $previous instanceof RequestException
179 ) {
180 if (method_exists($previous, 'getHandlerContext')) {
181 $context = $previous->getHandlerContext();
182 return !empty($context['errno'])
183 && isset($retryCurlErrors[$context['errno']]);
184 }
185
186 $message = $previous->getMessage();
187 foreach (array_keys($retryCurlErrors) as $curlError) {
188 if (strpos($message, 'cURL error ' . $curlError . ':') === 0) {
189 return true;
190 }
191 }
192 }
193
194 return false;
195 }
196
197 /**
198 * Delay function that calculates an exponential delay.
199 *
200 * Exponential backoff with jitter, 100ms base, 20 sec ceiling
201 *
202 * @param $retries - The number of retries that have already been attempted
203 *
204 * @return int
205 *
206 * @link https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
207 */
208 public static function exponentialDelay($retries)
209 {
210 return mt_rand(0, (int) min(20000, (int) pow(2, $retries) * 100));
211 }
212
213 /**
214 * @param CommandInterface $command
215 * @param RequestInterface $request
216 *
217 * @return PromiseInterface
218 */
219 public function __invoke(
220 CommandInterface $command,
221 ?RequestInterface $request = null
222 ) {
223 $retries = 0;
224 $requestStats = [];
225 $monitoringEvents = [];
226 $handler = $this->nextHandler;
227 $decider = $this->decider;
228 $delay = $this->delay;
229
230 $request = $this->addRetryHeader($request, 0, 0);
231
232 $g = function ($value) use (
233 $handler,
234 $decider,
235 $delay,
236 $command,
237 $request,
238 &$retries,
239 &$requestStats,
240 &$monitoringEvents,
241 &$g
242 ) {
243 $this->updateHttpStats($value, $requestStats);
244
245 if ($value instanceof MonitoringEventsInterface) {
246 $reversedEvents = array_reverse($monitoringEvents);
247 $monitoringEvents = array_merge($monitoringEvents, $value->getMonitoringEvents());
248 foreach ($reversedEvents as $event) {
249 $value->prependMonitoringEvent($event);
250 }
251 }
252 if ($value instanceof \Exception || $value instanceof \Throwable) {
253 if (!$decider($retries, $command, $request, null, $value)) {
254 $g = null;
255 return Promise\Create::rejectionFor(
256 $this->bindStatsToReturn($value, $requestStats)
257 );
258 }
259 } elseif ($value instanceof ResultInterface
260 && !$decider($retries, $command, $request, $value, null)
261 ) {
262 $g = null;
263 return $this->bindStatsToReturn($value, $requestStats);
264 }
265
266 // Delay fn is called with 0, 1, ... so increment after the call.
267 $delayBy = $delay($retries++);
268 $command['@http']['delay'] = $delayBy;
269 if ($this->collectStats) {
270 $this->updateStats($retries, $delayBy, $requestStats);
271 }
272
273 // Update retry header with retry count and delayBy
274 $request = $this->addRetryHeader($request, $retries, $delayBy);
275
276 return $handler($command, $request)->then($g, $g);
277 };
278
279 return $handler($command, $request)->then($g, $g);
280 }
281 }