TsgcGRPCClient

TsgcGRPCClient implements a gRPC client over HTTP/2 (using TsgcHTTP2Client as transport) and supports the four gRPC RPC patterns.

Introduction

TsgcGRPCClient implements a gRPC client that runs over HTTP/2. Follow the steps below to configure this component:

1. Assign a TsgcHTTP2Client instance to the Client property; this component is used as the HTTP/2 transport. Set the Host, Port and TLS of the transport to point to the gRPC server.

2. Handle the responses using the OnGRPCResponse event, then connect and call a unary method. example:


    oHTTP2 := TsgcHTTP2Client.Create(nil);
    oHTTP2.Host := 'grpc.example.com';
    oHTTP2.Port := 443;
    oHTTP2.TLS := True;

    oGRPC := TsgcGRPCClient.Create(nil);
    oGRPC.Client := oHTTP2;
    oGRPC.OnGRPCResponse := OnGRPCResponseEvent;
    oGRPC.CallAsync('helloworld.Greeter', 'SayHello', vRequestBytes);

    procedure OnGRPCResponseEvent(Sender: TObject; const aResponse: TsgcGRPCResponse);
    begin
      if aResponse.StatusCode = grpcOK then
        ShowMessage(aResponse.DataString);
    end;

RPC Patterns

gRPC defines four RPC patterns. TsgcGRPCClient exposes a set of members for each one:

The following example performs a server-streaming call and reads every message delivered by the server:


    oGRPC.OnGRPCStreamMessage := OnGRPCStreamMessageEvent;
    oGRPC.ServerStreamingCall('routeguide.RouteGuide', 'ListFeatures', vRequestBytes);

    procedure OnGRPCStreamMessageEvent(Sender: TObject; const aMessage: TsgcGRPCStreamMessage);
    begin
      ShowMessage(aMessage.DataString);
    end;

Features

Channel Options

The ChannelOptions property configures channel-level behaviour such as message Compression, the ContentType, the maximum send and receive message sizes and HTTP/2 keep-alive pings.

Metadata

Use DefaultMetadata to attach custom headers to every call, or pass per-call metadata to any of the call methods. This is how authorization headers such as a Bearer token are sent to the server.

Deadlines and Retry

Each call accepts a deadline so it fails fast when the server is slow. The RetryPolicy controls automatic retries: MaxAttempts, exponential backoff and the set of retryable status codes.

Load Balancing

TsgcGRPCLoadBalancer distributes calls across several backends using a Pick First or Round Robin policy, and resolves backend addresses through a DNS name resolver.

Health Checking

TsgcGRPCHealthClient implements the standard grpc.health.v1.Health service, with Check for a one-shot status query and Watch to receive health-status changes as a stream.

Server Reflection

TsgcGRPCReflectionClient uses the gRPC Server Reflection service to list the services exposed by a server and to fetch their file descriptors at runtime.

Interceptors

Interceptors can be chained through the Interceptors property. The library includes TsgcGRPCLoggingInterceptor, TsgcGRPCTimeoutInterceptor and TsgcGRPCMetadataInterceptor.

Telemetry

The MetricsCollector exposes per-method telemetry, including call counts, success rate and latency, useful for monitoring and diagnostics.

Protocol Buffers

gRPC requests and responses are Protocol Buffers (protobuf) encoded. The library includes TsgcProtobufWriter and TsgcProtobufMessage to build and parse messages by field number. The Google Cloud interfaces provide typed request and response classes, so you do not need to encode protobuf by hand when calling those services.

Reference

Properties

NameDescription
ClientThe TsgcHTTP2Client instance used as the HTTP/2 transport for all gRPC calls.
ChannelOptionsChannel-level options: compression, content-type, maximum message sizes and HTTP/2 keep-alive.
DefaultMetadataMetadata (custom headers) added to every call, such as an authorization Bearer token.
InterceptorsChain of interceptors applied to every call (logging, timeout, metadata).
RetryPolicyAutomatic retry configuration: maximum attempts, exponential backoff and retryable status codes.
ServiceConfigPer-service configuration, including method defaults, retry settings and load-balancing policy.
MetricsCollectorCollects per-method telemetry: call counts, success rate and latency.

Methods

NameDescription
CallPerforms a synchronous unary call and returns the TsgcGRPCResponse.
CallAsyncPerforms an asynchronous unary call; the response is delivered through the OnGRPCResponse event.
ServerStreamingCallStarts a server-streaming call; each server message arrives in OnGRPCStreamMessage.
OpenClientStreamOpens a client-streaming call so the client can send a sequence of messages.
SendStreamMessageSends one message on an open client stream.
CloseClientStreamCloses a client stream synchronously and returns the server response.
CloseClientStreamAsyncCloses a client stream asynchronously; the response arrives through OnGRPCResponse.
OpenBidiStreamOpens a bidirectional streaming call where both sides exchange messages.
SendBidiMessageSends one message on an open bidirectional stream.
CloseBidiStreamCloses a bidirectional stream and signals end-of-stream to the server.
CancelCallCancels an active call or stream before it completes.
CallStringUnary helper that sends a request and returns the response as a string.

Events

NameDescription
OnGRPCConnectRaised when the underlying HTTP/2 connection to the gRPC server is established.
OnGRPCDisconnectRaised when the connection to the gRPC server is closed.
OnGRPCResponseRaised when a unary call completes, delivering the TsgcGRPCResponse.
OnGRPCStreamMessageRaised for every message received on a server-streaming or bidirectional stream.
OnGRPCStreamEndRaised when a stream completes, carrying the final gRPC status.
OnGRPCErrorRaised when a call finishes with a non-OK gRPC status code.
OnGRPCExceptionRaised when an exception occurs while processing a call.
OnGRPCBeforeRequestRaised before a request is sent, allowing the headers and metadata to be inspected or modified.

See Also