Skip to content

Commit

Permalink
Skip retry policy when maxRetries is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
barell committed Feb 25, 2025
1 parent e5c416a commit b88d33e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 35 deletions.
47 changes: 24 additions & 23 deletions src/Contrib/Grpc/GrpcTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ public function create(
if (substr_count($parts['path'], '/') !== 2) {
throw new InvalidArgumentException(sprintf('Endpoint path is not a valid gRPC method "%s"', $method));
}
if ($maxRetries < 2) {
throw new InvalidArgumentException(sprintf('Max retries must be at least 2 when using gRPC; "%d" given', $maxRetries));
}

$opts = self::createOpts($compression, $maxRetries, $retryDelay);
/** @psalm-suppress PossiblyNullArgument */
Expand Down Expand Up @@ -110,9 +107,7 @@ private static function createOpts(
break;
}
}

// https://github.com/grpc/grpc-proto/blob/master/grpc/service_config/service_config.proto
$opts['grpc.service_config'] = json_encode([
$serviceConfig = [
'methodConfig' => [
[
'name' => [
Expand All @@ -121,25 +116,31 @@ private static function createOpts(
'method' => null,
],
],
'retryPolicy' => [
'maxAttempts' => $maxRetries,
'initialBackoff' => sprintf('%0.3fs', $retryDelay / self::MILLIS_PER_SECOND),
'maxBackoff' => sprintf('%0.3fs', ($retryDelay << $maxRetries - 1) / self::MILLIS_PER_SECOND),
'backoffMultiplier' => 2,
'retryableStatusCodes' => [
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md#otlpgrpc-response
'CANCELLED',
'DEADLINE_EXCEEDED',
'RESOURCE_EXHAUSTED',
'ABORTED',
'OUT_OF_RANGE',
'UNAVAILABLE',
'DATA_LOSS',
],
],
],
],
]);
];

if ($maxRetries > 0) {
$serviceConfig['methodConfig'][0]['retryPolicy'] = [
'maxAttempts' => $maxRetries + 1, // maxAttempts includes first attempt
'initialBackoff' => sprintf('%0.3fs', $retryDelay / self::MILLIS_PER_SECOND),
'maxBackoff' => sprintf('%0.3fs', ($retryDelay << $maxRetries - 1) / self::MILLIS_PER_SECOND),
'backoffMultiplier' => 2,
'retryableStatusCodes' => [
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md#otlpgrpc-response
'CANCELLED',
'DEADLINE_EXCEEDED',
'RESOURCE_EXHAUSTED',
'ABORTED',
'OUT_OF_RANGE',
'UNAVAILABLE',
'DATA_LOSS',
],
];
}

// https://github.com/grpc/grpc-proto/blob/master/grpc/service_config/service_config.proto
$opts['grpc.service_config'] = json_encode($serviceConfig);

return $opts;
}
Expand Down
14 changes: 2 additions & 12 deletions tests/Unit/Contrib/Grpc/GrpcTransportFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,14 @@ public function test_grpc_transport_create(): void
$this->assertInstanceOf(TransportInterface::class, $transport);
}

public function test_when_max_retries_is_two_then_transport_is_created(): void
public function test_when_max_retries_is_zero_transport_is_created(): void
{
$factory = new GrpcTransportFactory();
$transport = $factory->create(
endpoint: 'http://localhost/service/method',
maxRetries: 2,
maxRetries: 0,
);

$this->assertInstanceOf(TransportInterface::class, $transport);
}

public function test_when_max_retries_is_less_than_two_then_exception_is_thrown(): void
{
$this->expectException(\InvalidArgumentException::class);
$factory = new GrpcTransportFactory();
$factory->create(
endpoint: 'http://localhost/service/method',
maxRetries: 1,
);
}
}

0 comments on commit b88d33e

Please sign in to comment.