Request Lifecycle
The one pipeline every transport runs a call through, in the order it runs.
Every call, on every transport, goes through the same pipeline, so authentication, timing, error shaping and the like are written once as these roles and apply to any handler. A transport opens an execution for the call (one request, message or call; see Scopes), builds its context, and hands the handler to dispatch, which runs guards, interceptors, the controller and the handler, and offers any error to the error handlers. The roles are the same traits everywhere: Guard<T>, Interceptor<T> and ErrorHandler<T>, plus a pre-dispatch Middleware on HTTP and gRPC. T is the transport marker (Http, Rpc, Ws, Grpc).
The order of one call
For an HTTP request:
- Admission. Over the server's in-flight limit, the request gets a 503 before an execution opens. See Limits and timeouts.
- Pre-dispatch, unscoped entries. CORS, tower layers and middleware declared without a path scope run first, in the order written. They see requests no route matches and can rewrite the path.
- Routing. The router picks the handler, or none.
- Pre-dispatch, scoped entries. Entries whose path scope matches the request run next, still before any guard.
- Guards. Global, then controller, then method, in the order written within each tier. Each guard is built only once the one before it admits.
- Interceptors. Built after every guard has admitted, then nested around the handler in the same order: the global interceptor is outermost.
- The handler. The controller is built, extraction runs for each parameter in order, and the method runs.
- Error handlers. An error from any of steps 2 to 7, a guard's refusal and a caught panic included, is offered to the error handlers: method tier, then controller tier, then global tier, the last declared first within a tier. The first handler that answers
Okclaims the error.
Both pre-dispatch sub-steps run inside the error chain, so an entry's Err reaches the error handlers. On a request no route matches, only the global tier applies. The order of steps 5 to 8 is pinned by tests/enhancers.rs in examples/16-http-enhancers:
guard global, guard role: admitted, guard controller, guard method,
global in, controller in, method in, handler, method out, controller out, global outAn error no handler claims is rendered by the transport. Error Handling lists the renderings.
RPC and WebSocket have no pre-dispatch stage: a pattern or an event is matched inside the framework, and guards and interceptors cover what middleware would. gRPC has its own, PreDispatch<Grpc>, scoped by method path; see gRPC.
One role, one trait per transport
Each role is generic over the transport:
| Role | Trait | Runs |
|---|---|---|
| Pre-dispatch | Middleware, declared on PreDispatch | before routing, or right after it, before any guard |
| Guard | Guard<T> | before interceptors; answers Result<bool, BoxError> |
| Interceptor | Interceptor<T> | around the handler; sees the reply on the way out |
| Error handler | ErrorHandler<T> | on any error, method tier first |
Guard, Interceptor and ErrorHandler are imported from ulo, and Middleware from ulo_http. A type implements the role for each transport it serves. A handler's enhancers are checked against the handler's own transport at compile time, so a guard that does not implement Guard<Rpc> cannot be named on an RPC handler. Guards shows one type guarding an HTTP route and an RPC message (examples/41-one-guard-two-transports).
Tiers and how they are declared
An enhancer is declared at one of three tiers, and the tiers stack global, then controller, then method:
- Global: an
intolist in#[module(providers = [..])], contributing under a role key such asAnyGuard<Http>. - Controller:
#[guards(..)],#[interceptors(..)]and#[error_handlers(..)]on the#[handlers]impl. Each applies to every handler in the impl, and an entry that lacks the role for one handler's transport is a compile error.#[guards(http = AuthGuard)]scopes the entry to the impl's HTTP handlers. - Method: the same attributes on one handler.
Each entry is written one of three ways, and the written order is the run order whichever spelling an entry uses:
| Spelling | Example | Built |
|---|---|---|
| a type | #[guards(RoleGuard)] | through the container, in the type's scope |
| a value | #[guards(value = RateLimit::per_second(100))] | once, shared by every call |
| a closure | #[guards(with = |trace: Dep<Trace>| Stamp::new("method", trace))] | by the closure, its parameters resolved from the container; per call when a parameter reads execution data such as Ext<T>, and with(execution) = || .. forces it |
Dep<T> injects a provider the container builds (Providers). examples/16-http-enhancers/src/lib.rs uses all three tiers, and crates/ulo-codegen-tests/tests/enhancers.rs contains the value = form.
In this chapter
Middleware
The pre-dispatch stage: scoped middleware, tower layers, and values handed to handlers.
Guards
Admit or refuse a call before anything is built beyond the guard itself.
Interceptors
Wrap the handler and see the reply on the way out.
Error Handling
Domain errors, problem details, the handler chain, and panics.