晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。 林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。 见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝) 既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。 南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。
|
Server : Apache System : Linux srv.rainic.com 4.18.0-553.47.1.el8_10.x86_64 #1 SMP Wed Apr 2 05:45:37 EDT 2025 x86_64 User : rainic ( 1014) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/stando/www/wp-content/plugins/w3-total-cache/lib/Aws/Aws/ |
Upload File : |
<?php
namespace Aws;
use Aws\Exception\AwsException;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\RequestInterface;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Promise;
/**
* @internal Middleware that retries failures.
*/
class RetryMiddleware
{
private static $retryStatusCodes = [
500 => true,
502 => true,
503 => true,
504 => true
];
private static $retryCodes = [
// Throttling error
'RequestLimitExceeded' => true,
'Throttling' => true,
'ThrottlingException' => true,
'ThrottledException' => true,
'ProvisionedThroughputExceededException' => true,
'RequestThrottled' => true,
'BandwidthLimitExceeded' => true,
'RequestThrottledException' => true,
];
private $decider;
private $delay;
private $nextHandler;
private $collectStats;
public function __construct(
callable $decider,
callable $delay,
callable $nextHandler,
$collectStats = false
) {
$this->decider = $decider;
$this->delay = $delay;
$this->nextHandler = $nextHandler;
$this->collectStats = (bool) $collectStats;
}
/**
* Creates a default AWS retry decider function.
*
* The optional $additionalRetryConfig parameter is an associative array
* that specifies additional retry conditions on top of the ones specified
* by default by the Aws\RetryMiddleware class, with the following keys:
*
* - errorCodes: (string[]) An indexed array of AWS exception codes to retry.
* Optional.
* - statusCodes: (int[]) An indexed array of HTTP status codes to retry.
* Optional.
* - curlErrors: (int[]) An indexed array of Curl error codes to retry. Note
* these should be valid Curl constants. Optional.
*
* @param int $maxRetries
* @param array $additionalRetryConfig
* @return callable
*/
public static function createDefaultDecider(
$maxRetries = 3,
$additionalRetryConfig = []
) {
$retryCurlErrors = [];
if (extension_loaded('curl')) {
$retryCurlErrors[CURLE_RECV_ERROR] = true;
}
return function (
$retries,
CommandInterface $command,
RequestInterface $request,
ResultInterface $result = null,
$error = null
) use ($maxRetries, $retryCurlErrors, $additionalRetryConfig) {
// Allow command-level options to override this value
$maxRetries = null !== $command['@retries'] ?
$command['@retries']
: $maxRetries;
$isRetryable = self::isRetryable(
$result,
$error,
$retryCurlErrors,
$additionalRetryConfig
);
if ($retries >= $maxRetries) {
if (!empty($error)
&& $error instanceof AwsException
&& $isRetryable
) {
$error->setMaxRetriesExceeded();
}
return false;
}
return $isRetryable;
};
}
private static function isRetryable(
$result,
$error,
$retryCurlErrors,
$additionalRetryConfig = []
) {
$errorCodes = self::$retryCodes;
if (!empty($additionalRetryConfig['errorCodes'])
&& is_array($additionalRetryConfig['errorCodes'])
) {
foreach($additionalRetryConfig['errorCodes'] as $code) {
$errorCodes[$code] = true;
}
}
$statusCodes = self::$retryStatusCodes;
if (!empty($additionalRetryConfig['statusCodes'])
&& is_array($additionalRetryConfig['statusCodes'])
) {
foreach($additionalRetryConfig['statusCodes'] as $code) {
$statusCodes[$code] = true;
}
}
if (!empty($additionalRetryConfig['curlErrors'])
&& is_array($additionalRetryConfig['curlErrors'])
) {
foreach($additionalRetryConfig['curlErrors'] as $code) {
$retryCurlErrors[$code] = true;
}
}
if (!$error) {
return isset($statusCodes[$result['@metadata']['statusCode']]);
}
if (!($error instanceof AwsException)) {
return false;
}
if ($error->isConnectionError()) {
return true;
}
if (isset($errorCodes[$error->getAwsErrorCode()])) {
return true;
}
if (isset($statusCodes[$error->getStatusCode()])) {
return true;
}
if (count($retryCurlErrors)
&& ($previous = $error->getPrevious())
&& $previous instanceof RequestException
) {
if (method_exists($previous, 'getHandlerContext')) {
$context = $previous->getHandlerContext();
return !empty($context['errno'])
&& isset($retryCurlErrors[$context['errno']]);
}
$message = $previous->getMessage();
foreach (array_keys($retryCurlErrors) as $curlError) {
if (strpos($message, 'cURL error ' . $curlError . ':') === 0) {
return true;
}
}
}
return false;
}
/**
* Delay function that calculates an exponential delay.
*
* Exponential backoff with jitter, 100ms base, 20 sec ceiling
*
* @param $retries - The number of retries that have already been attempted
*
* @return int
*
* @link https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
*/
public static function exponentialDelay($retries)
{
return mt_rand(0, (int) min(20000, (int) pow(2, $retries) * 100));
}
/**
* @param CommandInterface $command
* @param RequestInterface $request
*
* @return PromiseInterface
*/
public function __invoke(
CommandInterface $command,
RequestInterface $request = null
) {
$retries = 0;
$requestStats = [];
$monitoringEvents = [];
$handler = $this->nextHandler;
$decider = $this->decider;
$delay = $this->delay;
$request = $this->addRetryHeader($request, 0, 0);
$g = function ($value) use (
$handler,
$decider,
$delay,
$command,
$request,
&$retries,
&$requestStats,
&$monitoringEvents,
&$g
) {
$this->updateHttpStats($value, $requestStats);
if ($value instanceof MonitoringEventsInterface) {
$reversedEvents = array_reverse($monitoringEvents);
$monitoringEvents = array_merge($monitoringEvents, $value->getMonitoringEvents());
foreach ($reversedEvents as $event) {
$value->prependMonitoringEvent($event);
}
}
if ($value instanceof \Exception || $value instanceof \Throwable) {
if (!$decider($retries, $command, $request, null, $value)) {
return Promise\rejection_for(
$this->bindStatsToReturn($value, $requestStats)
);
}
} elseif ($value instanceof ResultInterface
&& !$decider($retries, $command, $request, $value, null)
) {
return $this->bindStatsToReturn($value, $requestStats);
}
// Delay fn is called with 0, 1, ... so increment after the call.
$delayBy = $delay($retries++);
$command['@http']['delay'] = $delayBy;
if ($this->collectStats) {
$this->updateStats($retries, $delayBy, $requestStats);
}
// Update retry header with retry count and delayBy
$request = $this->addRetryHeader($request, $retries, $delayBy);
return $handler($command, $request)->then($g, $g);
};
return $handler($command, $request)->then($g, $g);
}
private function addRetryHeader($request, $retries, $delayBy)
{
return $request->withHeader('aws-sdk-retry', "{$retries}/{$delayBy}");
}
private function updateStats($retries, $delay, array &$stats)
{
if (!isset($stats['total_retry_delay'])) {
$stats['total_retry_delay'] = 0;
}
$stats['total_retry_delay'] += $delay;
$stats['retries_attempted'] = $retries;
}
private function updateHttpStats($value, array &$stats)
{
if (empty($stats['http'])) {
$stats['http'] = [];
}
if ($value instanceof AwsException) {
$resultStats = isset($value->getTransferInfo('http')[0])
? $value->getTransferInfo('http')[0]
: [];
$stats['http'] []= $resultStats;
} elseif ($value instanceof ResultInterface) {
$resultStats = isset($value['@metadata']['transferStats']['http'][0])
? $value['@metadata']['transferStats']['http'][0]
: [];
$stats['http'] []= $resultStats;
}
}
private function bindStatsToReturn($return, array $stats)
{
if ($return instanceof ResultInterface) {
if (!isset($return['@metadata'])) {
$return['@metadata'] = [];
}
$return['@metadata']['transferStats'] = $stats;
} elseif ($return instanceof AwsException) {
$return->setTransferInfo($stats);
}
return $return;
}
}