| 1 | # listenfd |
| 2 | |
| 3 | [](https://crates.io/crates/listenfd) |
| 4 | [](https://github.com/mitsuhiko/listenfd/blob/master/LICENSE) |
| 5 | [](https://img.shields.io/badge/rust-1.42%2B-orange.svg) |
| 6 | [](https://docs.rs/listenfd) |
| 7 | |
| 8 | listenfd is a crate that provides support for working with externally managed |
| 9 | and passed file descriptors. This lets you work with systems that support |
| 10 | socket activation or similar. |
| 11 | |
| 12 | Currently this supports systemd (including systemd-socket-activate) on Unix and |
| 13 | [systemfd](https://github.com/mitsuhiko/systemfd) on Unix and Windows. |
| 14 | systemfd is very convenient in combination with cargo-watch for development |
| 15 | purposes whereas systemd is useful for production deployments on linux. |
| 16 | |
| 17 | ## Example |
| 18 | |
| 19 | ```rust |
| 20 | use listenfd::ListenFd; |
| 21 | |
| 22 | let mut listenfd = ListenFd::from_env(); |
| 23 | let mut server = make_a_server(); |
| 24 | |
| 25 | // if we are given a tcp listener on listen fd 0, we use that one |
| 26 | server = if let Some(listener) = listenfd.take_tcp_listener(0)? { |
| 27 | server.listen(listener) |
| 28 | // otherwise fall back to local listening |
| 29 | } else { |
| 30 | server.bind("127.0.0.1:3000")? |
| 31 | }; |
| 32 | ``` |
| 33 | |
| 34 | You can then use this with cargo watch and systemfd: |
| 35 | |
| 36 | ``` |
| 37 | $ cargo install systemfd cargo-watch |
| 38 | systemfd --no-pid -s http::3000 -- cargo watch -x run |
| 39 | ``` |
| 40 | |
| 41 | Now systemfd will open the socket and keep it open. cargo watch will recompile |
| 42 | the code on demand and the server will pick up the socket that systemfd opened. |
| 43 | No more connection resets. |
| 44 | |
| 45 | ## License and Links |
| 46 | |
| 47 | - [Documentation](https://docs.rs/listenfd/) |
| 48 | - [Issue Tracker](https://github.com/mitsuhiko/listenfd/issues) |
| 49 | - [Examples](https://github.com/mitsuhiko/listenfd/tree/main/examples) |
| 50 | - License: [Apache-2.0](https://github.com/mitsuhiko/listenfd/blob/main/LICENSE) |