GRPCClient
A gRPC client.
final class GRPCClient<Transport> where Transport : ClientTransport
A GRPCClient
communicates to a server via a ClientTransport
.
You can start RPCs to the server by calling the corresponding method:
unary(request:descriptor:serializer:deserializer:options:onResponse:)
clientStreaming(request:descriptor:serializer:deserializer:options:onResponse:)
serverStreaming(request:descriptor:serializer:deserializer:options:onResponse:)
bidirectionalStreaming(request:descriptor:serializer:deserializer:options:onResponse:)
However, in most cases you should prefer wrapping the GRPCClient
with a generated stub.
Creating a client
You can create and run a client using withGRPCClient(transport:interceptors:isolation:handleClient:)
or withGRPCClient(transport:interceptorPipeline:isolation:handleClient:)
which create, configure and run the client providing scoped access to it via the handleClient
closure. The client will begin gracefully shutting down when the closure returns.
let transport: any ClientTransport = ...
try await withGRPCClient(transport: transport) { client in
// ...
}
Creating a client manually
If the with
-style methods for creating clients isn’t suitable for your application then you can create and run a client manually. This requires you to call the runConnections
method in a task which instructs the client to start connecting to the server.
The runConnections
method won’t return until the client has finished handling all requests. You can signal to the client that it should stop creating new request streams by calling beginGracefulShutdown
. This gives the client enough time to drain any requests already in flight. To stop the client more abruptly you can cancel the task running your client. If your application requires additional resources that need their lifecycles managed you should consider using Swift Service Lifecycle.