Skip to content

Commit

Permalink
add retry using idempontentOps list
Browse files Browse the repository at this point in the history
add retry using idempontentOps list
  • Loading branch information
thiyaguk09 committed Dec 9, 2024
1 parent 69eaeb1 commit cf9fd55
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 19 deletions.
1 change: 1 addition & 0 deletions Storage/src/Connection/RetryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ trait RetryTrait
'objects.get',
'objects.list',
'serviceaccount.get',
'signBlob.execute'
];

/**
Expand Down
15 changes: 1 addition & 14 deletions Storage/src/SigningHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
use Google\Cloud\Storage\Connection\ConnectionInterface;
use Google\Cloud\Core\Exception\ServiceException;
use Google\Cloud\Storage\Connection\RetryTrait;
use Google\Cloud\Storage\StorageClient;
use Ramsey\Uuid\Uuid;

/**
* Provides common methods for signing storage URLs.
Expand Down Expand Up @@ -909,21 +907,10 @@ private function buildQueryString(array $input)
*/
private function retrySignBlob(callable $signBlobFn, string $resourceName = 'signBlob', array $args = [])
{
$attempts = 0;
$maxRetries = 5;
$invocationId = Uuid::uuid4()->toString();
$args['retryStrategy'] = StorageClient::RETRY_ALWAYS;

// Generate a retry decider function using the RetryTrait logic.
$retryDecider = $this->getRestRetryFunction($resourceName, 'execute', $args);

while ($attempts < $maxRetries) {
$attempts++;
while (true) {
try {
// Attach retry headers
$headers = self::getRetryHeaders($invocationId, $attempts);
$args['headers'] = array_merge($args['headers'] ?? [], $headers);

// Attempt the operation
return $signBlobFn();
} catch (\Exception $exception) {
Expand Down
8 changes: 3 additions & 5 deletions Storage/tests/Unit/SigningHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -806,10 +806,9 @@ public function testRetrySignBlobSuccessFirstAttempt()

public function testRetrySignBlobSuccessAfterRetries()
{
$attempts = 0;
$signBlobFn = function () use (&$attempts) {
$attempts++;
if ($attempts < 5) {
$attempt = 0;
$signBlobFn = function () use (&$attempt) {
if (++$attempt < 3) {
throw new ServiceException('Transient error', 503);
}
return 'signature';
Expand All @@ -820,7 +819,6 @@ public function testRetrySignBlobSuccessAfterRetries()
]);

$this->assertEquals('signature', $res);
$this->assertEquals(5, $attempts);
}

public function testRetrySignBlobNonRetryableError()
Expand Down

0 comments on commit cf9fd55

Please sign in to comment.