#sockets #systemd #file-descriptor #socket-activation #tcp-listener #system

listenfd

A simple library to work with listenfds passed from the outside (systemd/catflap socket activation)

12 releases (2 stable)

1.0.1 Mar 10, 2023
1.0.0 May 31, 2022
0.5.0 Jan 24, 2022
0.4.0 Jan 15, 2022
0.1.1 May 17, 2018

#32 in Unix APIs

Download history 10953/week @ 2023-11-24 11463/week @ 2023-12-01 10015/week @ 2023-12-08 12433/week @ 2023-12-15 9345/week @ 2023-12-22 10404/week @ 2023-12-29 11397/week @ 2024-01-05 12580/week @ 2024-01-12 14032/week @ 2024-01-19 12899/week @ 2024-01-26 11990/week @ 2024-02-02 11789/week @ 2024-02-09 11766/week @ 2024-02-16 12457/week @ 2024-02-23 11273/week @ 2024-03-01 6170/week @ 2024-03-08

43,832 downloads per month
Used in 27 crates (26 directly)

Apache-2.0

19KB
295 lines

listenfd

Build Status Crates.io License rustc 1.42.0 Documentation

listenfd is a crate that provides support for working with externally managed and passed file descriptors. This lets you work with systems that support socket activation or similar.

Currently this supports systemd on Unix and systemfd on Unix and Windows. systemfd is very convenient in combination with cargo-watch for development purposes whereas systemd is useful for production deployments on linux.

Example

use listenfd::ListenFd;

let mut listenfd = ListenFd::from_env();
let mut server = make_a_server();

// if we are given a tcp listener on listen fd 0, we use that one
server = if let Some(listener) = listenfd.take_tcp_listener(0)? {
    server.listen(listener)
// otherwise fall back to local listening
} else {
    server.bind("127.0.0.1:3000")?
};

You can then use this with cargo watch and systemfd:

$ cargo install systemfd cargo-watch
systemfd --no-pid -s http::3000 -- cargo watch -x run

Now systemfd will open the socket and keep it open. cargo watch will recompile the code on demand and the server will pick up the socket that systemfd opened. No more connection resets.

Dependencies

~0.4–265KB