Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
},
"require": {
"php": ">=8.1",
"guzzlehttp/guzzle": "^7.4.5",
"guzzlehttp/psr7": "^2.4.5",
"guzzlehttp/promises": "^2.0",
"guzzlehttp/guzzle": "^7.8.2 || ^8.0",
"guzzlehttp/psr7": "^2.6.3 || ^3.0",
"guzzlehttp/promises": "^2.0.3 || ^3.0",
"mtdowling/jmespath.php": "^2.8.0",
"ext-pcre": "*",
"ext-json": "*",
Expand Down
9 changes: 5 additions & 4 deletions src/Credentials/EcsCredentialProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use Aws\Arn\Arn;
use Aws\Exception\CredentialsException;
use GuzzleHttp\Exception\ConnectException;
use Aws\Handler\HttpHandlerError;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Promise;
Expand Down Expand Up @@ -105,13 +105,14 @@ public function __invoke()
CredentialSources::ECS
);
})->otherwise(function ($reason) {
$reason = is_array($reason) ? $reason['exception'] : $reason;
$connectionError = is_array($reason) && !empty($reason['connection_error']);
$exception = is_array($reason) ? ($reason['exception'] ?? null) : $reason;
$isRetryable = $connectionError || ($exception instanceof \Throwable && HttpHandlerError::isConnectionError($exception));

$isRetryable = $reason instanceof ConnectException;
if ($isRetryable && ($this->attempts < $this->retries)) {
sleep((int)pow(1.2, $this->attempts));
} else {
$msg = $reason->getMessage();
$msg = $exception instanceof \Throwable ? $exception->getMessage() : \Aws\describe_type($reason);
throw new CredentialsException(
sprintf('Error retrieving credentials from container metadata after attempt %d/%d (%s)', $this->attempts, $this->retries, $msg)
);
Expand Down
25 changes: 7 additions & 18 deletions src/Handler/Guzzle/GuzzleHandler.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php
namespace Aws\Handler\Guzzle;

use Exception;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use Aws\Handler\HttpHandlerError;
use GuzzleHttp\Utils;
use GuzzleHttp\Promise;
use GuzzleHttp\Client;
Expand Down Expand Up @@ -31,7 +29,7 @@ public function __construct(?ClientInterface $client = null)
* @param Psr7Request $request
* @param array $options
*
* @return Promise\Promise
* @return Promise\PromiseInterface
*/
public function __invoke(Psr7Request $request, array $options = [])
{
Expand All @@ -43,21 +41,12 @@ public function __invoke(Psr7Request $request, array $options = [])

return $this->client->sendAsync($request, $this->parseOptions($options))
->otherwise(
static function ($e) {
$error = [
static function (\Throwable $e) {
return new Promise\RejectedPromise([
'exception' => $e,
'connection_error' => $e instanceof ConnectException,
'response' => null,
];

if (
($e instanceof RequestException)
&& $e->getResponse()
) {
$error['response'] = $e->getResponse();
}

return new Promise\RejectedPromise($error);
'connection_error' => HttpHandlerError::isConnectionError($e),
'response' => HttpHandlerError::getResponse($e),
]);
}
);
}
Expand Down
56 changes: 56 additions & 0 deletions src/Handler/HttpHandlerError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
namespace Aws\Handler;

use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\NetworkException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ResponseException;
use GuzzleHttp\Exception\ResponseTransferException;
use Psr\Http\Message\ResponseInterface;

/**
* @internal
*/
final class HttpHandlerError
{
private const CURLE_RECV_ERROR = 56;

public static function isConnectionError(\Throwable $exception): bool
{
// Guzzle 8: transfer failures have dedicated exception classes.
if ($exception instanceof NetworkException || $exception instanceof ResponseTransferException) {
return true;
}

// Guzzle 7: connection establishment failures use ConnectException.
if ($exception instanceof ConnectException) {
return true;
}

// Guzzle 7: mid-response receive failures identifiable by cURL handler context.
if ($exception instanceof RequestException && is_callable([$exception, 'getHandlerContext'])
) {
$context = $exception->getHandlerContext();

return !empty($context['errno']) && $context['errno'] === self::CURLE_RECV_ERROR;
}

return false;
}

public static function getResponse(\Throwable $exception): ?ResponseInterface
{
// Guzzle 8: response-aware failures expose the response through ResponseException.
if ($exception instanceof ResponseException) {
return $exception->getResponse();
}

// Guzzle 7: RequestException directly carried an optional response.
if ($exception instanceof RequestException && is_callable([$exception, 'getResponse'])
) {
return $exception->getResponse();
}

return null;
}
}
54 changes: 4 additions & 50 deletions src/Retry/V3/RetryMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Aws\Retry\ConfigurationInterface;
use Aws\Retry\RateLimiter;
use Aws\Retry\RetryHelperTrait;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\RequestInterface;
Expand Down Expand Up @@ -72,7 +71,6 @@ class RetryMiddleware
private array $options;
private QuotaManager $quotaManager;
private ?RateLimiter $rateLimiter = null;
private array $retryCurlErrors;
private ?string $service;

public static function wrap(ConfigurationInterface $config, array $options): \Closure
Expand All @@ -84,23 +82,17 @@ public static function wrap(ConfigurationInterface $config, array $options): \Cl

/**
* Returns a closure that decides retryability for a given result based
* on the standard error codes, status codes, and curl errors. Quota and
* max-attempts decisions are handled by the middleware itself, not by
* this closure.
* on the standard error codes and status codes. Quota and max-attempts
* decisions are handled by the middleware itself, not by this closure.
*/
public static function createDefaultDecider(array $options = []): \Closure
{
$retryCurlErrors = [];
if (extension_loaded('curl')) {
$retryCurlErrors[CURLE_RECV_ERROR] = true;
}

return function (
int $attempts,
CommandInterface $command,
mixed $result
) use ($options, $retryCurlErrors): bool {
return self::isRetryable($result, $retryCurlErrors, $options);
) use ($options): bool {
return self::isRetryable($result, $options);
};
}

Expand Down Expand Up @@ -128,16 +120,6 @@ public function __construct(
? ($options['delayer'])(...)
: null;

$this->retryCurlErrors = [];
if (extension_loaded('curl')) {
$this->retryCurlErrors[CURLE_RECV_ERROR] = true;
}
if (!empty($options['curl_errors']) && is_array($options['curl_errors'])) {
foreach ($options['curl_errors'] as $code) {
$this->retryCurlErrors[$code] = true;
}
}

if ($this->mode === 'adaptive') {
$this->rateLimiter = $options['rate_limiter'] ?? new RateLimiter();
}
Expand Down Expand Up @@ -198,7 +180,6 @@ public function __invoke(CommandInterface $cmd, RequestInterface $req): PromiseI

$isRetryable = self::isRetryable(
$value,
$this->retryCurlErrors,
$this->options
);

Expand Down Expand Up @@ -342,7 +323,6 @@ private function computeRetryDelay(int $attemptIndex, bool $isThrottling, mixed

private static function isRetryable(
mixed $result,
array $retryCurlErrors,
array $options = []
): bool
{
Expand Down Expand Up @@ -371,14 +351,6 @@ private static function isRetryable(
}
}

if (!empty($options['curl_errors'])
&& is_array($options['curl_errors'])
) {
foreach ($options['curl_errors'] as $code) {
$retryCurlErrors[$code] = true;
}
}

$isError = $result instanceof \Throwable;

if (!$isError) {
Expand Down Expand Up @@ -406,24 +378,6 @@ private static function isRetryable(
return true;
}

if (count($retryCurlErrors)
&& ($previous = $result->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 (str_starts_with($message, 'cURL error ' . $curlError . ':')) {
return true;
}
}
}

if (!empty($errorShape = $result->getAwsErrorShape())) {
$definition = $errorShape->toArray();
if (!empty($definition['retryable'])) {
Expand Down
39 changes: 1 addition & 38 deletions src/RetryMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Aws\Exception\AwsException;
use Aws\Retry\RetryHelperTrait;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\RequestInterface;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Promise;
Expand Down Expand Up @@ -67,9 +66,6 @@ public function __construct(
* 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 $extraConfig
* @return callable
Expand All @@ -78,18 +74,13 @@ public static function createDefaultDecider(
$maxRetries = 3,
$extraConfig = []
) {
$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, $extraConfig) {
) use ($maxRetries, $extraConfig) {
// Allow command-level options to override this value
$maxRetries = null !== $command['@retries'] ?
$command['@retries']
Expand All @@ -98,7 +89,6 @@ public static function createDefaultDecider(
$isRetryable = self::isRetryable(
$result,
$error,
$retryCurlErrors,
$extraConfig
);

Expand All @@ -119,7 +109,6 @@ public static function createDefaultDecider(
private static function isRetryable(
$result,
$error,
$retryCurlErrors,
$extraConfig = []
) {
$errorCodes = self::$retryCodes;
Expand All @@ -140,14 +129,6 @@ private static function isRetryable(
}
}

if (!empty($extraConfig['curl_errors'])
&& is_array($extraConfig['curl_errors'])
) {
foreach($extraConfig['curl_errors'] as $code) {
$retryCurlErrors[$code] = true;
}
}

if (!$error) {
if (!isset($result['@metadata']['statusCode'])) {
return false;
Expand All @@ -173,24 +154,6 @@ private static function isRetryable(
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;
}

Expand Down
Loading