Skip to content

Commit

Permalink
fix gRPC timeout (#1498) (#1499)
Browse files Browse the repository at this point in the history
  • Loading branch information
spadger authored Feb 3, 2025
1 parent 96ce422 commit 31d902b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/API/Common/Time/ClockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface ClockInterface
public const NANOS_PER_SECOND = 1_000_000_000;
public const NANOS_PER_MILLISECOND = 1_000_000;
public const NANOS_PER_MICROSECOND = 1_000;
public const MICROS_PER_MILLISECOND = 1_000;
public const MILLIS_PER_SECOND = 1_000;

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Contrib/Grpc/GrpcTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use const Grpc\OP_SEND_MESSAGE;
use const Grpc\STATUS_OK;
use Grpc\Timeval;
use OpenTelemetry\API\Common\Time\ClockInterface;
use OpenTelemetry\Contrib\Otlp\ContentTypes;
use OpenTelemetry\SDK\Common\Export\TransportInterface;
use OpenTelemetry\SDK\Common\Future\CancellationInterface;
Expand All @@ -38,15 +39,18 @@ final class GrpcTransport implements TransportInterface
private readonly array $metadata;
private readonly Channel $channel;
private bool $closed = false;
private Timeval $exportTimeout;

public function __construct(
string $endpoint,
array $opts,
private readonly string $method,
array $headers = [],
int $timeoutMillis = 500,
) {
$this->channel = new Channel($endpoint, $opts);
$this->metadata = $this->formatMetadata(array_change_key_case($headers));
$this->exportTimeout = new Timeval($timeoutMillis * ClockInterface::MICROS_PER_MILLISECOND);
}

public function contentType(): string
Expand All @@ -60,7 +64,7 @@ public function send(string $payload, ?CancellationInterface $cancellation = nul
return new ErrorFuture(new BadMethodCallException('Transport closed'));
}

$call = new Call($this->channel, $this->method, Timeval::infFuture());
$call = new Call($this->channel, $this->method, $this->exportTimeout);

$cancellation ??= new NullCancellation();
$cancellationId = $cancellation->subscribe(static fn (Throwable $e) => $call->cancel());
Expand Down
2 changes: 1 addition & 1 deletion src/Contrib/Grpc/GrpcTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function create(
$opts,
$method,
$headers,
(int) ($timeout * 1000),
);
}

Expand Down Expand Up @@ -116,7 +117,6 @@ private static function createOpts(
'method' => null,
],
],
'timeout' => sprintf('%0.6fs', $timeout),
'retryPolicy' => [
'maxAttempts' => $maxRetries,
'initialBackoff' => sprintf('%0.3fs', $retryDelay / 1000),
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Contrib/Grpc/GrpcTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class GrpcTransportTest extends TestCase

public function setUp(): void
{
$this->transport = new GrpcTransport('http://localhost:4317', [], '/method', []);
$this->transport = new GrpcTransport('http://localhost:4317', [], '/method', [], 123);
}

public function test_grpc_transport_supports_only_protobuf(): void
Expand Down

0 comments on commit 31d902b

Please sign in to comment.