Runtimes
The runtime and listener crates a server is built from, with tokio and smol side by side.
A server crate does not pick a runtime. The app has one, set with AppBuilder::runtime(..), and every task the server starts (each connection, HTTP/2 stream tasks, the header-read clock) goes through it. The sockets come from a listener crate that matches it.
| tokio | smol | |
|---|---|---|
| Runtime crate | ulo-runtime-tokio | ulo-runtime-smol |
| Runtime value | Tokio::current() | Smol::new(Arc::new(Executor::new())) |
| Listener crate | ulo-listener-tokio | ulo-listener-smol |
| Listener type | TokioListener | SmolListener |
| Sockets and TLS | tokio sockets, tokio-rustls | async-net sockets, futures-rustls |
| hyper server type | ulo_http_hyper::Server | ulo_http_hyper::ServerOn::<SmolListener> |
| Shutdown signal | shutdown_signal() | written by the application |
ulo-http-hyper's default tokio feature builds the tokio listener and the Server alias over it. A smol application sets default-features = false so no tokio runtime crate is built.
tokio
Every example except examples/22-http-on-smol runs here. The runtime is Tokio::current(), taken inside #[tokio::main], and shutdown_signal() resolves on SIGINT or SIGTERM.
A tokio listener adopts its sockets into tokio's reactor, so a tokio runtime must be running when the server binds. An app whose runtime is not ulo_runtime_tokio::Tokio and that binds a tokio-listener server is refused at bind, with an error naming the listener.
smol
examples/22-http-on-smol is the same controller as example 01 on smol. ulo-runtime-smol takes an async-executor Executor that the caller owns and runs, and Smol::block_on drives the app on it. No tokio runtime is in the dependency tree.
pub fn server(endpoint: &str) -> ulo_http_hyper::ServerOn<ulo_listener_smol::SmolListener> {
ulo_http_hyper::ServerOn::new(endpoint)
}use std::sync::Arc;
use ulo_runtime_smol::{Executor, Smol};
fn main() -> Result<(), ulo::BoxError> {
let port = std::env::var("PORT").unwrap_or_else(|_| "3000".into());
let smol = Smol::new(Arc::new(Executor::new()));
smol.block_on(async {
let app = ex_22_http_on_smol::builder()
.runtime(smol.clone())
.wire()?
.connect()
.await?
.bind(ex_22_http_on_smol::server(&format!("127.0.0.1:{port}")))
.listen()
.await?;
for bound in app.addresses() {
println!("listening on http://{}", bound.addr);
}
app.serve(shutdown_signal()).await?;
Ok(())
})
}shutdown_signal here is the function the example defines, described below.
The example's Cargo manifest turns the tokio feature off:
ulo-http-hyper = { path = "../../crates/ulo-http-hyper", default-features = false }
ulo-listener-smol = { path = "../../crates/ulo-listener-smol" }
ulo-runtime-smol = { path = "../../crates/ulo-runtime-smol" }Only the tokio runtime crate ships a shutdown future. The smol example writes its own over async-signal, resolving on the first SIGINT or SIGTERM and returning a ulo::Signal.
The smol runtime never refuses to spawn or sleep. Its executor is the caller's: a spawned task makes progress only while some thread runs the executor.
The hyper dependency
hyper depends on tokio with its sync feature, for a channel inside its upgrade future. Tokio's synchronisation primitives are therefore in every hyper server's tree, the smol one included. Its executor, reactor and timers are not.
Other runtimes
ulo-server-hyper implements the accept loop that ulo-http-hyper, ulo-ws-hyper and the gRPC server share. A new runtime supplies a Runtime and a Listener; see Writing a server crate.