| 1 | //! listenfd is a crate that provides support for working with externally |
| 2 | //! managed and passed file descriptors. This lets you work with systems |
| 3 | //! that support socket activation or similar. |
| 4 | //! |
| 5 | //! Currently this supports a slightly modified systemd protocl on unix and |
| 6 | //! a custom protocol on Windows. If you want to use this for development |
| 7 | //! you can use the [systemfd](https://github.com/mitsuhiko/systemfd) |
| 8 | //! utility which implements both those protocols. |
| 9 | //! |
| 10 | //! The systemd extension is that if the `LISTEN_PID` variable is not set or |
| 11 | //! empty, the check for the pid is removed. This is useful when binaries |
| 12 | //! are proxied in between like cargo-watch. For the windows protocol |
| 13 | //! have a look at the systemfd documentation. |
| 14 | //! |
| 15 | //! ## Example |
| 16 | //! |
| 17 | //! This example shows how to use this crate with an `actix-web` server: |
| 18 | //! |
| 19 | //! ```rust |
| 20 | //! # use std::io; |
| 21 | //! # struct Server; |
| 22 | //! # impl Server { |
| 23 | //! # fn listen<X>(self, _: X) -> Self { self } |
| 24 | //! # fn bind<X>(self, _: X) -> io::Result<Self> { Ok(self) } |
| 25 | //! # } |
| 26 | //! # fn make_a_server() -> Server { Server }; |
| 27 | //! # fn test() -> io::Result<()> { |
| 28 | //! use listenfd::ListenFd; |
| 29 | //! |
| 30 | //! let mut listenfd = ListenFd::from_env(); |
| 31 | //! let mut server = make_a_server(); |
| 32 | //! |
| 33 | //! // if we are given a tcp listener on listen fd 0, we use that one |
| 34 | //! server = if let Some(listener) = listenfd.take_tcp_listener(0)? { |
| 35 | //! server.listen(listener) |
| 36 | //! // otherwise fall back to local listening |
| 37 | //! } else { |
| 38 | //! server.bind("127.0.0.1:3000")? |
| 39 | //! }; |
| 40 | //! # Ok(()) } |
| 41 | //! ``` |
| 42 | //! |
| 43 | //! You can then use this with cargo watch and systemfd: |
| 44 | //! |
| 45 | //! ```plain |
| 46 | //! $ cargo install systemfd cargo-watch |
| 47 | //! systemfd --no-pid -s 3000 -- cargo watch -x run |
| 48 | //! ``` |
| 49 | //! |
| 50 | //! Now systemfd will open the socket and keep it open. cargo watch will |
| 51 | //! recompile the code on demand and the server will pick up the socket |
| 52 | //! that systemfd opened. No more connection resets. |
| 53 | //! |
| 54 | //! The `--no-pid` flag is necessary to ensure that the `LISTEN_PID` environment |
| 55 | //! variable is not set or the socket passing will be prevented by the pid check. |
| 56 | |
| 57 | #[cfg(not(windows))] |
| 58 | mod unix; |
| 59 | #[cfg(windows)] |
| 60 | mod windows; |
| 61 | |
| 62 | mod manager; |
| 63 | pub use manager::*; |