Bind Targets
Listen on an address, or serve on a socket a supervisor already opened.
A server is given where to listen when it is built. The text, a SocketAddr or an Endpoint all convert into the argument. The example is examples/40-socket-activation.
use ulo_http_hyper::Server;
use ulo_net::Endpoint;
Server::new("127.0.0.1:3000") // an address the app binds itself
Server::new("127.0.0.1:0") // port 0: the OS picks, `app.addresses()` reports it
Server::new(Endpoint::inherited_index(0)) // the first socket a supervisor handed over
Server::new(Endpoint::inherited("web")) // the socket named `web` in LISTEN_FDNAMESServer is ulo_http_hyper::Server. Text is parsed when the server prepares, so main writes no ? on an endpoint and one startup reports every configuration failure together.
The bound address
listen() binds every server. app.addresses() returns the addresses bound, one per server with the transport's name, so a port-0 server reports the port the OS chose:
for bound in app.addresses() {
println!("listening on http://{}", bound.addr);
}Inherited sockets
A supervisor such as systemd, ulo dev --listen or a test binds and listens, then starts the process with the socket as file descriptor 3, LISTEN_FDS=1 and LISTEN_PID set to the process. Ulo reads those variables itself, so no listenfd crate is needed. Connections made before the app exists wait in the kernel's backlog and are answered once it is up, so a restart drops none.
The example chooses between an address and an inherited socket in one function:
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}"))
}
}main passes activated as std::env::var_os("LISTEN_FDS").is_some(). Run as ulo dev --listen 127.0.0.1:3000 -p ex-40-socket-activation, the supervisor holds the socket across rebuilds.
ulo dev sets ULO_DEV=1 in every child it starts. Under it, a server written Server::new("127.0.0.1:3000") adopts the held socket named after that address when the process has one, and binds the address itself in production. An application that never names an inherited socket needs no change to restart under ulo dev --listen.
Checks
Every inherited descriptor is vetted before any is adopted. The process id in LISTEN_PID must equal this process's. A mismatch fails startup as PidMismatch. A descriptor must be a listening stream socket with an IPv4 or IPv6 local address. One that is not (a UDP socket at descriptor 3, for example) fails as NotListening, and none is adopted. An index or name nobody handed over fails with the endpoint named:
no inherited socket #0
no inherited socket is named `web`These are StartupError::Configure, reported before any port conflict. The variables stay in the environment after adoption. A child spawned later fails the LISTEN_PID check, so no cleanup is needed.
Inherited sockets are Unix only. Elsewhere an inherited endpoint fails startup as unsupported, and an Endpoint::Addr is unaffected.
Not covered
Handing in an already bound std::net::TcpListener without the environment protocol has no public path. The example's README records it as an open item.
TLS is configured on the server: see HTTP Servers.