Skip to content

Create server instance by passing in TcpListener #1602

Closed
@yoshuawuyts

Description

@yoshuawuyts

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


Thanks so much for your time; I hope this is useful!

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-serverArea: server.B-rfcBlocked: More comments would be useful in determine next steps.C-featureCategory: feature. This is adding a new feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions