ulo
ulo
Documentation

Start here

Coming from NestJSNestJS topics, mapped

The model

Protocols

HTTP Serversulo-http-hyperTLSRuntimesEmbeddingaxumactix-websalvopoemrocketWriting a server crate

Integrations

Operating

Reference

HTTP Servers

Writing a server crate

The Backend, Listener and Embed traits a crate implements to serve Ulo over another HTTP stack, another runtime, or inside another framework.

Three extension points exist. Which one applies depends on what is new.

New thingImplementExamples
A runtime (sockets and TLS for it)ulo_server_hyper::Listenerulo-listener-tokio, ulo-listener-smol
A different HTTP engine that owns the socketsulo_http::Backendulo-http-hyper
A host framework that owns the socketsulo_http::embed::Embedulo-http-axum, -actix, -poem, -rocket, -salvo

ulo-http and a backend exchange the http crate's types. A backend crate is conversion plus the drain mapping.

A listener for a runtime

ulo-server-hyper implements the accept loop that the HTTP server, the standalone WebSocket server and the gRPC server share. A runtime crate supplies the sockets and TLS stack through Listener; everything else (one task per connection spawned through the app's Runtime, the handshake timeout on the app's Timer, the drain, the close) is the loop's and identical on every runtime.

pub trait Listener: Sized + Send + Sync + 'static {
    /// One connection's I/O as hyper reads and writes it, after the TLS handshake.
    type Io: Read + Write + Send + Unpin + 'static;

    /// Adopts a socket `ulo-net` bound or inherited into the runtime, accepting TLS under `tls`.
    fn adopt(listener: BoundListener, tls: Option<Arc<ServerConfig>>) -> io::Result<Self>;

    /// The next connection. The TLS handshake is not done here but in `Incoming`'s handshake,
    /// on the connection's own task, so a slow one never holds up the accept loop.
    fn accept(&self) -> impl Future<Output = io::Result<Incoming<Self::Io>>> + Send;
}

BoundListener and ServerConfig are ulo_net::BoundListener and ulo_net::rustls::ServerConfig, Read and Write are hyper's hyper::rt traits, and Incoming is ulo_server_hyper::Incoming. adopt is called from the server's bind, inside the app's runtime. Incoming::plain wraps a connection with no handshake, and Incoming::handshaking wraps one whose handshake yields the I/O and a TlsInfo (build it with ulo_server_hyper::tls_info(&session)). A new listener is used by naming it: ulo_http_hyper::ServerOn::<MyListener>::new(..).

A backend

A Backend owns the listeners and the connection loop for one HTTP engine. ulo_http::Server<B> prepares everything first (the route table, endpoint parsing, TLS loading, the inherited sockets, the backend's declared limits) and then hands the backend bound listeners and an AppService to call for each request.

pub trait Backend: Send + Sync + 'static {
    const NAME: &'static str;

    /// What it cannot do, checked in `prepare`.
    fn limits(&self) -> BackendLimits;

    /// Takes the bound listeners and starts accepting, without serving yet. All-or-nothing.
    fn bind(&mut self, listeners: Vec<BoundListener>, tls: Option<Arc<ServerConfig>>,
            svc: AppService, cfg: &HttpConfig) -> impl Future<Output = Result<(), BoxError>> + Send;

    /// Serves until drained, calling `svc` for every request.
    fn serve(&self) -> impl Future<Output = Result<(), BoxError>> + Send;

    /// Stops accepting: HTTP/2 GOAWAY, idle keep-alive connections closed, busy ones marked
    /// `Connection: close`.
    fn drain(&self) -> impl Future<Output = ()> + Send;

    /// Closes every listener and connection left.
    fn close(&self) -> impl Future<Output = Result<(), BoxError>> + Send;
}

Backend, BackendLimits, HttpConfig and AppService are exported from ulo_http, and BoundListener is the same ulo_net type as above. Rules the trait states:

  • A response body dropped before its end is how a peer's reset or closed connection reaches the execution, as CancelReason::Disconnected. The backend drops it as soon as it observes either. An unread request body that is dropped is not a disconnect. The future AppService::call returns cancels the execution with Disconnected when the backend drops it before the response's head is produced, because the peer closed the connection or reset the stream.
  • drain may return only once its connections have ended, so close does not cut a response still being written. The core drops a drain still running at its deadline.
  • BackendLimits declares upgrades, h2c, inherited_sockets, port_zero and tls. A server asking for what the backend declares unsupported fails prepare as Configure, naming the limit. BackendLimits::NONE is a backend that can do everything.

The backend's HttpConfig carries the settings listed on the hyper page, with Count and Bound unresolved; header_timeout_after() and handshake_timeout_after() give the durations.

An embedding adapter

An Embed implementation names the host's request type, declares what the host cannot do (EmbedLimits), and, through strips_prefix, whether the host strips the mount prefix. See Embedding for what each field means. EmbedLimits::NONE has every capability, and a host declares one absent through its without_<capability>(reason) setter: without_strips_prefix(reason) for a host that leaves the prefix on the path, where Service::respond strips it. The associated items are:

pub trait Embed: Send + Sync + 'static {
    const NAME: &'static str;
    type HostRequest<'r>;
    fn limits() -> EmbedLimits;
    fn builtin_forwards(embedded: Embedded<Self>) -> Embedded<Self> where Self: Sized { embedded }
    fn take_upgrade(extensions: &mut http::Extensions) -> Option<OnUpgrade> { None }
}

A host that mounts tower services names http::request::Parts as HostRequest<'r>, and only then is embed::Service a tower::Service. Other hosts implement their own handler trait, build the app's Request themselves and call Service::respond(&host_request, request). A host that keeps a call running after its peer closes the connection or resets the stream, until the call answers, declares without_pending_close for an HTTP/1.1 close and without_pending_reset for an HTTP/2 reset, with its reason. Nothing is refused, and the execution then observes the disconnect when the host drops the response body. A host that keeps upgrades (it does not declare without_upgrades) implements take_upgrade, unless it builds the app's request itself and sets upgrade there.

Conformance

One test suite checks both kinds of crate. ulo-http-conformance runs the same scenarios against the hyper backend and every adapter. A server or adapter crate implements its Host trait in tests/ and stamps the scenarios with http_conformance_suite!(HostType), listing any scenario that cannot apply in not_applicable { .. } with a reason. A scenario about an absence first waits for a positive signal that the moment passed, and a host that passes a scenario it declares unsupported fails it.

The suites are ulo-http-conformance for HTTP, ulo-ws-conformance for WebSocket servers and ulo-runtime-conformance for runtime crates. ulo-runtime-conformance is published; the HTTP and WebSocket suites are dev-dependencies of their crates and are not.

rocket

Running an Ulo application inside a rocket server with ulo-http-rocket.

WebSocket

Gateways, connection hooks, message handlers, rooms and cross-process broadcast on ulo-ws.

On this page

A listener for a runtimeA backendAn embedding adapterConformance