Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Core): add retryFunction in GrpcRequestWrapper::send #5770

Merged
merged 4 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions Core/src/GrpcRequestWrapper.php
Expand Up @@ -111,20 +111,23 @@ public function __construct(array $config = [])
* request. **Defaults to** `60`.
* @type int $retries Number of retries for a failed request.
* **Defaults to** `3`.
* @type callable $retryFunction Returns bool for whether or not to retry.
* @type array $grpcOptions gRPC specific configuration options.
* }
* @return array
*/
public function send(callable $request, array $args, array $options = [])
{
$retries = isset($options['retries']) ? $options['retries'] : $this->retries;
$retryFunction = isset($options['retryFunction'])
? $options['retryFunction']
: function (\Exception $ex) {
$statusCode = $ex->getCode();
return in_array($statusCode, $this->grpcRetryCodes);
};
$grpcOptions = isset($options['grpcOptions']) ? $options['grpcOptions'] : $this->grpcOptions;
$timeout = isset($options['requestTimeout']) ? $options['requestTimeout'] : $this->requestTimeout;
$backoff = new ExponentialBackoff($retries, function (\Exception $ex) {
$statusCode = $ex->getCode();

return in_array($statusCode, $this->grpcRetryCodes);
});
$backoff = new ExponentialBackoff($retries, $retryFunction);

if (!isset($grpcOptions['retrySettings'])) {
$retrySettings = [
Expand Down
28 changes: 28 additions & 0 deletions Core/tests/Unit/GrpcRequestWrapperTest.php
Expand Up @@ -109,6 +109,34 @@ public function responseProvider()
];
}

public function testRetryFunction()
{
$requestWrapper = new GrpcRequestWrapper();
$timesCalled = 0;
$requestWrapper->send(
function () use (&$timesCalled) {
if (1 === $timesCalled++) {
vishwarajanand marked this conversation as resolved.
Show resolved Hide resolved
// succeed on second try
return;
}
throw new ApiException(
'Retryable exception',
\Google\Rpc\Code::NOT_FOUND,
\Google\ApiCore\ApiStatus::NOT_FOUND
);
},
[[]],
[
'retries' => 1,
'retryFunction' => function (\Exception $ex) {
return $ex->getMessage() === 'Retryable exception';
}
]
);

$this->assertEquals(2, $timesCalled);
}

public function testThrowsExceptionWhenRequestFails()
{
$this->expectException('Google\Cloud\Core\Exception\GoogleException');
Expand Down