Skip to content

Commit

Permalink
Retry the test case for maximum attempts.
Browse files Browse the repository at this point in the history
Ensure proper handling of maxRetries to prevent infinite loop errors.
  • Loading branch information
thiyaguk09 committed Dec 11, 2024
1 parent a0fb38d commit 9b67a34
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
20 changes: 16 additions & 4 deletions Storage/src/Connection/RetryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ private function getRestRetryFunction($resource, $method, array $args)
StorageClient::RETRY_IDEMPOTENT;

return function (
\Exception $exception
\Exception $exception,
$currentAttempt = 0,
$maxRetries = null
) use (
$isOpIdempotent,
$preconditionNeeded,
Expand All @@ -147,7 +149,9 @@ private function getRestRetryFunction($resource, $method, array $args)
$isOpIdempotent,
$preconditionNeeded,
$preconditionSupplied,
$retryStrategy
$retryStrategy,
$currentAttempt,
$maxRetries
);
};
}
Expand Down Expand Up @@ -188,16 +192,24 @@ private function isPreConditionSupplied($methodName, array $args)
* @param bool $isIdempotent
* @param bool $preconditionNeeded
* @param bool $preconditionSupplied
* @param int $maxRetries
* @param int|null $maxRetries The maximum number of retries allowd.
* Null for no limit.
* @return bool
*/
private function retryDeciderFunction(
\Exception $exception,
$isIdempotent,
$preconditionNeeded,
$preconditionSupplied,
$retryStrategy
$retryStrategy,
$currentAttempt = 0,
$maxRetries = null
) {
// If maxRetries is specified, ensure we don't exceed it
if ($maxRetries !== null && $currentAttempt >= $maxRetries) {
return false;
}

if ($retryStrategy == StorageClient::RETRY_NEVER) {
return false;
}
Expand Down
5 changes: 4 additions & 1 deletion Storage/src/SigningHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class SigningHelper
const V4_ALGO_NAME = 'GOOG4-RSA-SHA256';
const V4_TIMESTAMP_FORMAT = 'Ymd\THis\Z';
const V4_DATESTAMP_FORMAT = 'Ymd';
const MAX_RETRIES = 5;

/**
* Create or fetch a SigningHelper instance.
Expand Down Expand Up @@ -901,14 +902,16 @@ private function buildQueryString(array $input)
*/
private function retrySignBlob(callable $signBlobFn, string $resourceName = 'signBlob', array $args = [])
{
$attempt = 0;
// Generate a retry decider function using the RetryTrait logic.
$retryDecider = $this->getRestRetryFunction($resourceName, 'execute', $args);
while (true) {
++$attempt;
try {
// Attempt the operation
return $signBlobFn();
} catch (\Exception $exception) {
if (!$retryDecider($exception)) {
if (!$retryDecider($exception, $attempt, self::MAX_RETRIES)) {
// Non-retryable error
throw $exception;
}
Expand Down
16 changes: 16 additions & 0 deletions Storage/tests/Unit/SigningHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,22 @@ public function testRetrySignBlobNonRetryableError()
$signBlobFn
]);
}

public function testRetrySignBlobThrowsExceptionAfterThreeAttempts()
{
$this->expectException(ServiceException::class);
$this->expectExceptionMessage('Transient error (5 attempts)');

$attempt = 0;
$signBlobFn = function () use (&$attempt) {
throw new ServiceException(
sprintf('Transient error (%s attempts)', ++$attempt),
503
);
};

$this->helper->proxyPrivateMethodCall('retrySignBlob', [$signBlobFn]);
}
}

//@codingStandardsIgnoreStart
Expand Down

0 comments on commit 9b67a34

Please sign in to comment.