Deadlines
How a caller's remaining time travels with an RPC call, and what the callee does when it runs out.
A deadline is the instant by which a caller stops waiting. A call made .within an execution sends what is left of that execution's deadline to the callee as the reserved header deadline-ms. The callee opens its own execution with that much time and stops when its caller gives up. This page follows examples/27-rpc-deadlines: a gateway calls a pricing service.
Forwarding a deadline
A handler reads its execution through an ExecutionRef parameter. The gateway passes it to the outgoing call with .within:
use ulo::{ExecutionRef, Timer, handlers};
use ulo_rpc::{Payload, RpcError};
impl Gateway {
/// The call carries what is left of `exec`'s deadline to the callee, and does not leave while
/// `exec` has none left.
pub async fn quote(&self, exec: &ExecutionRef, sku: &str) -> Result<Quote, RpcError> {
self.pricing.request::<_, Quote>("price.quote", sku).within(exec).await
}
}
#[handlers]
impl Gateway {
#[ulo_rpc::message("gateway.quote")]
async fn relay(&self, sku: Payload<String>, exec: ExecutionRef) -> Result<Relayed, RpcError> {
Ok(Relayed { gateway_left_ms: left_ms(&exec, &*self.timer), quote: self.quote(&exec, &sku.0).await? })
}
}The callee reads the deadline it was given from its own ExecutionRef:
fn left_ms(exec: &ExecutionRef, timer: &dyn Timer) -> u64 {
exec.deadline().map_or(u64::MAX, |at| at.saturating_duration_since(timer.now()).as_millis() as u64)
}No call is forwarded on its own. There is no ambient execution, so a call made without .within carries only its own timeout.
How the callee's time is chosen
The deadline sent is the shortest of:
- the execution's remaining time, when the call is
.withinone, - the call's
timeout(..)ordeadline(..), - the client's
timeout, five seconds atBound::Default, forrequestandsend_stream.
A caller with thirty seconds left reaches the callee with about five on a default client, because the client stops waiting then and the callee is not to outlive its caller. A longer bound on the call lifts the cap: with thirty seconds left and .timeout(Duration::from_secs(20)), the callee gets about twenty. The tests in examples/27-rpc-deadlines/tests/deadlines.rs pin both cases.
pricing.request::<_, Quote>("price.quote", "tea").timeout(Duration::from_secs(20)).within(&exec.handle()).awaitA stream or duplex sends the shorter of its own .deadline(d), where it sets one, and the execution's remaining time. Its idle_timeout is not sent. An emit sends no deadline-ms: nobody waits on it and its handler commonly runs after the emitter has ended.
A deadline that has already passed
A call made .within an execution whose deadline has passed fails when it is awaited, with Timeout, before the client connects. Nothing reaches the callee and its handler does not run. A cancelled execution fails the call with Unavailable the same way.
let spent = ExecOptions::new().deadline(Instant::now());
let late = gateway.handle.execute(spent, async |exec| service.quote(&exec.handle(), "tea").await).await?;
assert_eq!(late.expect_err("refused").kind(), ErrorKind::Timeout);
assert_eq!(runs.count.load(Ordering::SeqCst), 0);When the callee's deadline passes
The server drops the handler at its current await, cancels the execution with the reason Deadline, and offers a Timeout error to the error handlers for up to Server::timeout_grace (one second by default). An error handler may reshape the answer. If none claims it, or the grace passes, the caller receives err with the kind timeout. A streamed answer from an error handler is ended at the deadline instead of delivered.
Running the example
cargo test -p ex-27-rpc-deadlines
PORT=7000 cargo run -p ex-27-rpc-deadlinesThe binary runs both services in one process: the pricing service on a port of its own, the gateway on PORT.
gRPC carries deadlines the same way in the grpc-timeout header; see gRPC client.