Description
Hi there!
I was looking for a way to create a Hyper server instance from an existing TcpListener
instance, and I couldn't find one. I was wondering it might be possible to add a method to allow for this.
Note: I might totally have missed it if there's already a method that allows for this. Apologies if any of this is redundant!
Motivation
Systemd has the capability of creating sockets and handing them off to applications using the $LISTEN_FD
env var. Crates such as listenfd and systemfd make clever use of this to provide an uninterrupted reload experience during development.
As part of the CLI WG we've written clap-port-flag, which can create a socket from --port
, $PORT
, or $LISTEN_FD
. It does this by exposing a .bind()
method that contains all logic necessary to do this.
fn main() {
let args = Cli::from_args();
let _tcp_listener = args.port.bind().unwrap();
}
In order for Systemd's $LISTEN_FD
to work with Hyper, a method would be needed to pass in a TcpListener
instance.
Prior Art
actix-web
has a server.listen()
method that takes a TcpListener
. It also exposes a server.bind()
method which acts similar to Hyper's .bind()
method.
Implementation
I propose adding a hyper::server::Server::listen()
method with the following signature:
pub fn listen(listener: &TcpListener) -> Builder<AddrIncoming>
Note: To be honest I'm not completely sure if Builder<AddrIncoming>
would be the right return type here. I hope it is!
Other Considerations
It looks like actix-web
also exposes a .listen_tls()
method which also accepts a TLS struct. I'm not sure what the interactions between TLS and external TcpListener
initialization would be in Hyper.
References
- https://github.com/mitsuhiko/rust-listenfd
- https://github.com/rust-clique/clap-port-flag (maintained by yours truly)
- https://github.com/mitsuhiko/systemfd
- https://twitter.com/mitsuhiko/status/997227876327002112
- https://github.com/passcod/catflap
Thanks so much for your time; I hope this is useful!