ulo
ulo
Documentation

Start here

Coming from NestJSNestJS topics, mapped

The model

Request LifecycleMiddlewareGuardsInterceptorsError Handling

Protocols

Integrations

Operating

Reference

Request Lifecycle

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:

  1. Admission. Over the server's in-flight limit, the request gets a 503 before an execution opens. See Limits and timeouts.
  2. 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.
  3. Routing. The router picks the handler, or none.
  4. Pre-dispatch, scoped entries. Entries whose path scope matches the request run next, still before any guard.
  5. Guards. Global, then controller, then method, in the order written within each tier. Each guard is built only once the one before it admits.
  6. Interceptors. Built after every guard has admitted, then nested around the handler in the same order: the global interceptor is outermost.
  7. The handler. The controller is built, extraction runs for each parameter in order, and the method runs.
  8. 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 Ok claims the error.
REQUESTRESPONSEPre-dispatch, unscoped entriesCORS, tower layers and unscoped middleware. Runs before routing and sees requests no route matchesRoutingthe router picks the handler, or none. A path rewritten above changes what matchesPre-dispatch, scoped entriesentries whose path scope matches the request, still before any guardGuardsglobal, controller, method. Ok(false) is a GuardRejected, rendered 403Interceptorsglobal outermost. next.run() calls the next layer and returns its replyHandlerthe controller is built and each extractor runs in orderAn error from any layer goes to the error handlers: method, then controller, then global.

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 out

An 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:

RoleTraitRuns
Pre-dispatchMiddleware, declared on PreDispatchbefore routing, or right after it, before any guard
GuardGuard<T>before interceptors; answers Result<bool, BoxError>
InterceptorInterceptor<T>around the handler; sees the reply on the way out
Error handlerErrorHandler<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 into list in #[module(providers = [..])], contributing under a role key such as AnyGuard<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:

SpellingExampleBuilt
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.

Without macros

The value API the macros expand to: Construct, Module and ModuleDef, written by hand.

Middleware

The pre-dispatch stage on HTTP, where CORS, tower layers and path-scoped middleware run before a guard does.

On this page

The order of one callOne role, one trait per transportTiers and how they are declaredIn this chapter