Skip to main content

Crate trillium_tokio

Crate trillium_tokio 

Source
Expand description

§Trillium server adapter for tokio

fn main() {
    trillium_tokio::run(|conn: trillium::Conn| async move { conn.ok("hello tokio") });
}
#[tokio::main]
async fn main() {
    trillium_tokio::run_async(|conn: trillium::Conn| async move { conn.ok("hello tokio") })
        .await;
}

For advanced binding — several listeners on one server, fallible binds you can recover from, or adopting an already-bound socket — call .listeners() on a config() to get a ListenerConfig.

§Thread-per-core with SO_REUSEPORT on Linux

SO_REUSEPORT is a socket option that lets several sockets bind the same address and port at once, with the kernel distributing incoming connections across them. Enable the reuseport cargo feature on Linux to use it for thread-per-core fan-out:

use trillium::Conn;

fn main() -> std::io::Result<()> {
    trillium_tokio::config()
        .listeners()
        .bind_reuseport_tcp(8080)?
        .run(|conn: Conn| async move { conn.ok("hello") });
    Ok(())
}

Each worker thread runs its own single-threaded runtime, pinned to a core, driving that worker’s accept loop — one SO_REUSEPORT listener per worker. The standard multi-threaded work-stealing runtime is still present alongside them, hosting HTTP/3, signal handling, and the application tasks you spawn, so QUIC is never fanned out this way. Set the worker count with .with_reuseport_workers(n); it defaults to the WORKERS environment variable, or if that’s not set, to available parallelism.

This trades the work-stealing runtime’s load balancing for per-core locality, which can improve throughput for short, CPU-cheap requests served over many connections. It is gated off on platforms where plain SO_REUSEPORT does not distribute connections (including macOS), where it would offer no benefit.

Re-exports§

pub use async_compat;
pub use tokio;
pub use tokio_stream;

Structs§

ClientConfig
configuration for the tcp Connector
ListenerConfig
An advanced listener builder.
Swansong
🦢 Shutdown manager
TokioRuntime
tokio runtime
TokioTransport
A transport newtype for tokio
TokioUdpSocket
Tokio-backed async UDP socket for use with QUIC transports.

Enums§

Binding
A wrapper enum that has blanket implementations for common traits like TryFrom, Stream, AsyncRead, and AsyncWrite. This can contain listeners (like TcpListener), Streams (like Incoming), or bytestreams (like TcpStream).

Traits§

IntoListenAddr
Conversion into a single TCP bind address.

Functions§

config
Configures a server before running it
run
Runs a trillium handler in a sync context with default config
run_async
Runs a trillium handler in an async context with default config