Client
A tonic-generated client registered as an ordinary provider, a lazily connected channel, and deadlines forwarded with each call.
There is no Ulo client type for gRPC. ulo-build generates a tonic client per service and implements ulo_grpc::GrpcClient for it. GrpcClientModule binds that client under its own type, and a service injects it like any other dependency. This page follows examples/30-grpc-client: Front is a gRPC service that calls Clock and forwards its own deadline.
Registering a client
use tonic::transport::Channel;
use ulo::{Dep, Module, ModuleDef, ModuleIdentity, controller};
use ulo_grpc::{GrpcClientModule, GrpcEndpoint};
use pb::clock_client::ClockClient;
#[controller]
pub struct Front {
clock: Dep<ClockClient<Channel>>,
}
impl Module for FrontModule {
fn identity(&self) -> ModuleIdentity {
ModuleIdentity::of_type::<Self>()
}
fn register(&self, m: &mut ModuleDef<'_>) {
m.import(GrpcClientModule::<ClockClient<Channel>>::for_root(GrpcEndpoint::new(self.callee.clone())));
m.controller::<Front>();
}
}The URI is parsed when the app wires, and a bad one is reported with the other wiring errors. The channel is built when the app connects and opens its connection on the first call, so the app starts whether or not the callee is up. A callee that is down fails the first call with UNAVAILABLE. examples/30-grpc-client/tests/client.rs pins this.
Two endpoints of one service coexist by keying the module with a marker type, as with RPC clients:
m.import(GrpcClientModule::<KnownGrpc>::for_root(GrpcEndpoint::new(primary)).keyed::<Primary>());
m.import(GrpcClientModule::<KnownGrpc>::for_root(GrpcEndpoint::new(secondary)).keyed::<Secondary>());The injected client is then Dep<KnownGrpc, Primary>. This form is from crates/ulo-codegen-tests/tests/clients.rs.
TLS
An https URI trusts the platform's root certificates. GrpcEndpoint::tls(ClientTls) chooses the roots:
use ulo_grpc::{ClientTls, GrpcEndpoint};
GrpcEndpoint::new("https://users:443").tls(ClientTls::system_roots())ClientTls::ca_pem(pem) trusts the CA certificates in pem, for a service whose certificate a private CA signed.
Forwarding the deadline
Nothing forwards a deadline on its own: there is no ambient execution. ulo_grpc::outgoing(&exec, request) sets grpc-timeout from the execution's remaining time and writes its trace context as traceparent and tracestate.
use tonic::{Request, Status};
use ulo::ExecutionRef;
pub async fn remaining(&self, exec: &ExecutionRef) -> Result<pb::Left, Status> {
let request = ulo_grpc::outgoing(exec, Request::new(pb::Probe {}))?;
let mut clock = (*self.clock).clone();
Ok(clock.remaining(request).await?.into_inner())
}The callee opens its execution with that much time left, so it stops when its caller gives up. examples/30-grpc-client/tests/client.rs calls Front with deadlines of 5 s and 2 s and checks the callee sees the matching time left. A caller with no deadline gives the callee none.
A deadline that has already passed is refused in outgoing with DEADLINE_EXCEEDED and the message "the calling execution's deadline has passed". No request is built and nothing reaches the callee. outgoing returns a tonic::Status. The call itself fails with the same type, so outgoing(..)? and the call's own ? are one error type in a handler.
A deadline further off than 99999999H, the longest the grpc-timeout grammar allows, is sent as that. The value is truncated to the finest unit whose amount fits eight digits, so the callee's deadline is up to one unit before the caller's. The remaining time is read on tokio's clock.
A request that sets traceparent itself on an execution that carries a trace context fails in outgoing as INVALID_ARGUMENT with an ErrorInfo detail, domain ulo.grpc, reason reserved_header.
From a test or a script
A generated client also works on its own, with any tonic channel:
use tonic::transport::Endpoint;
let channel = Endpoint::from_shared(format!("http://{}", served.addr))?.connect().await?;
let mut client = CatalogClient::new(channel);as in examples/29-grpc-service/tests/service.rs.
The RPC counterpart is Deadlines.