From 68c5a7b09e11a463390036786773644ca523c5dd Mon Sep 17 00:00:00 2001 From: Wiktor Kwapisiewicz Date: Fri, 21 Jan 2022 13:15:10 +0100 Subject: [PATCH] agent: Add `run_listener` to the Agent trait This change exposes `Agent::run_listener` that takes a raw `UnixListener` object. This allows starting the agent with listeners that are backed by raw file descriptors. This in turn makes writing agents that are started with systemd's socket activation [0]. [0]: https://0pointer.de/blog/projects/socket-activation.html The following example starts the agent from systemd's supplied input: ```rs use std::os::unix::io::FromRawFd; let fds = std::env::var("LISTEN_FDS").unwrap(); let fds: i32 = fds.parse()?; let listener = unsafe { std::os::unix::net::UnixListener::from_raw_fd(fds + 2) }; listener.set_nonblocking(true)?; let handle = tokio_reactor::Handle::default(); let listener = tokio_uds::UnixListener::from_std(listener, &handle)?; let agent = Backend::default(); agent.run_listener(listener); ``` --- src/agent.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/agent.rs b/src/agent.rs index 00f7865..32ced8f 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -87,11 +87,14 @@ pub trait Agent: 'static + Sync + Send + Sized { Box::new(FutureResult::from(self.handle(message))) } - fn run_unix(self, path: impl AsRef) -> Result<(), Box> { - let socket = UnixListener::bind(path)?; + fn run_listener(self, socket: UnixListener) -> Result<(), Box> { Ok(tokio::run(handle_clients!(self, socket))) } + fn run_unix(self, path: impl AsRef) -> Result<(), Box> { + self.run_listener(UnixListener::bind(path)?) + } + fn run_tcp(self, addr: &str) -> Result<(), Box> { let socket = TcpListener::bind(&addr.parse::()?)?; Ok(tokio::run(handle_clients!(self, socket)))