Socket activation
Serving on a socket a supervisor opened, so a restart or a rebuild refuses no connection.
A supervisor (systemd, ulo dev --listen, a test) binds and listens, then starts the process with the socket as file descriptor 3, LISTEN_FDS=1 and LISTEN_PID=<the process>. The application names the socket instead of an address, and listen() adopts it. Connections made before the application exists wait in the kernel's backlog and are answered once it is up, so a restart drops none.
ulo-net reads the variables itself. No listenfd crate is needed.
In the application
examples/40-socket-activation chooses between an address and an inherited socket in one function:
use ulo_http_hyper::Server;
use ulo_net::Endpoint;
/// Where the server listens: the first socket the supervisor handed over (descriptor 3, named by
/// `LISTEN_FDS`) when `activated`, else `127.0.0.1:port`, which the app binds itself.
pub fn server(activated: bool, port: &str) -> Server {
if activated {
Server::new(Endpoint::inherited_index(0))
} else {
Server::new(format!("127.0.0.1:{port}"))
}
}let port = std::env::var("PORT").unwrap_or_else(|_| "3000".into());
let activated = std::env::var_os("LISTEN_FDS").is_some();
let app = ex_40_socket_activation::builder()
.runtime(Tokio::current())
.wire()?
.connect()
.await?
.bind(ex_40_socket_activation::server(activated, &port))
.listen()
.await?;Keeping the address branch lets the binary run on its own, from cargo run or a test. The dependencies are ulo, ulo-http, ulo-http-hyper, ulo-net and ulo-runtime-tokio.
Endpoint::inherited_index(0) takes the first inherited socket. Endpoint::inherited("name") takes the one whose LISTEN_FDNAMES entry matches, which is how a service with several sockets tells them apart.
Under systemd
A socket unit owns the port and a service unit runs the binary:
[Socket]
ListenStream=8080
[Install]
WantedBy=sockets.target[Service]
ExecStart=/opt/myapp/my-app
User=apisudo systemctl enable --now myapp.socketsystemd starts myapp.service on the first connection and sets LISTEN_PID to the service's process ID. The socket stays open while the service restarts.
Under ulo dev
ulo dev --listen 127.0.0.1:3000 is a supervisor that follows the same protocol and holds the socket across rebuilds:
ulo dev --listen 127.0.0.1:3000 -p ex-40-socket-activationUnder ulo dev --listen ADDR, an application that never names an inherited socket adopts the held one too: every child runs with ULO_DEV=1, and a plain Server::new("ADDR") resolves to the held socket. Example 01 therefore restarts under ulo dev --listen 127.0.0.1:3000 -p ex-01-hello without refusing a connection. See Development Workflow.
What is refused
Adoption is checked before any socket is used, and a failure stops startup with an error that names the cause:
- The inherited descriptor is not a listening TCP socket (a UDP socket at descriptor 3, for instance):
NotListening. LISTEN_PIDnames another process:PidMismatch. An application whose servers all use plain addresses is not affected by a stale value.- The index or name nobody handed over:
StartupError::Configurenaming it.
These cases are pinned by examples/40-socket-activation/tests/binary.rs and tests/refusal.rs.
Socket activation is Unix only. Off Unix an inherited endpoint fails startup as unsupported, and an address endpoint works as usual. UDP sockets are not adopted: a UDP RPC link binds its own address.
The example does not show handing in an already-bound std::net::TcpListener without the environment protocol; the public API has no such path yet.