/usr/share/php/Aws/RetryMiddleware.php is in php-aws-sdk 3.15.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | <?php
namespace Aws;
use Aws\Exception\AwsException;
use Psr\Http\Message\RequestInterface;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Psr7;
/**
* @internal Middleware that retries failures.
*/
class RetryMiddleware
{
private static $retryStatusCodes = [
500 => true,
503 => true
];
private static $retryCodes = [
'RequestLimitExceeded' => true,
'Throttling' => true,
'ThrottlingException' => true,
'ProvisionedThroughputExceededException' => true,
'RequestThrottled' => true,
];
private $decider;
private $delay;
private $nextHandler;
public function __construct(
callable $decider,
callable $delay,
callable $nextHandler
) {
$this->decider = $decider;
$this->delay = $delay;
$this->nextHandler = $nextHandler;
}
/**
* Creates a default AWS retry decider function.
*
* @param int $maxRetries
*
* @return callable
*/
public static function createDefaultDecider($maxRetries = 3)
{
return function (
$retries,
CommandInterface $command,
RequestInterface $request,
ResultInterface $result = null,
$error = null
) use ($maxRetries) {
// Allow command-level options to override this value
$maxRetries = null !== $command['@retries'] ?
$command['@retries']
: $maxRetries;
if ($retries >= $maxRetries) {
return false;
} elseif (!$error) {
return isset(self::$retryStatusCodes[$result['@metadata']['statusCode']]);
} elseif (!($error instanceof AwsException)) {
return false;
} elseif ($error->isConnectionError()) {
return true;
} elseif (isset(self::$retryCodes[$error->getAwsErrorCode()])) {
return true;
} elseif (isset(self::$retryStatusCodes[$error->getStatusCode()])) {
return true;
} else {
return false;
}
};
}
/**
* Delay function that calculates an exponential delay.
*
* @param $retries
*
* @return int
*/
public static function exponentialDelay($retries)
{
return mt_rand(0, (int) pow(2, $retries - 1) * 100);
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
*
* @return PromiseInterface
*/
public function __invoke(
CommandInterface $command,
RequestInterface $request = null
) {
$retries = 0;
$handler = $this->nextHandler;
$decider = $this->decider;
$delay = $this->delay;
$g = function ($value) use ($handler, $decider, $delay, $command, $request, &$retries, &$g) {
if ($value instanceof \Exception) {
if (!$decider($retries, $command, $request, null, $value)) {
return \GuzzleHttp\Promise\rejection_for($value);
}
} elseif ($value instanceof ResultInterface
&& !$decider($retries, $command, $request, $value, null)
) {
return $value;
}
// Delay fn is called with 0, 1, ... so increment after the call.
$command['@http']['delay'] = $delay($retries++);
return $handler($command, $request)->then($g, $g);
};
return $handler($command, $request)->then($g, $g);
}
}
|