hyperium / tonic

A native gRPC client & server implementation with async/await support.
https://docs.rs/tonic
MIT License
9.76k stars 997 forks source link

Add serve_with_listener fro tonic::transport::server::Router #1096

Closed MaxKingPor closed 1 year ago

MaxKingPor commented 1 year ago

Feature Request

Crates

Motivation

Proposal

    pub async fn serve_with_listener<ResBody>(self, listener: TcpListener) -> Result<(), super::Error>
    where
        L: Layer<Routes>,
        L::Service: Service<Request<Body>, Response = Response<ResBody>> + Clone + Send + 'static,
        <<L as Layer<Routes>>::Service as Service<Request<Body>>>::Future: Send + 'static,
        <<L as Layer<Routes>>::Service as Service<Request<Body>>>::Error: Into<crate::Error> + Send,
        ResBody: http_body::Body<Data = Bytes> + Send + 'static,
        ResBody::Error: Into<crate::Error>,
    {
        let incoming = TcpIncoming::from_listener(
            listener,
            self.server.tcp_nodelay,
            self.server.tcp_keepalive,
        )
        .map_err(super::Error::from_source)?;

        self.server
            .serve_with_shutdown::<_, _, future::Ready<()>, _, _, ResBody>(
                self.routes,
                incoming,
                None,
            )
            .await
    }

image

Alternatives

w41ter commented 1 year ago

You should use server_with_incoming instead of server_with_listener.

Here is an example:

    let listener = TcpListener::bind(addr).await?;
    let incoming = TcpIncoming::from_listener(listener, true);

    let server = Server::builder()
        .accept_http1(true) // Support http1 for admin service.
        .add_service(NodeServer::new(server.clone()))
        .add_service(RaftServer::new(server.clone()))
        .add_service(RootServer::new(server.clone()))
        .add_service(make_admin_service(server.clone()))
        .add_optional_service(proxy_server.map(EngulaServer::new))
        .serve_with_incoming(incoming);
MaxKingPor commented 1 year ago

You should use server_with_incoming instead of server_with_listener.

Here is an example:

    let listener = TcpListener::bind(addr).await?;
    let incoming = TcpIncoming::from_listener(listener, true);

    let server = Server::builder()
        .accept_http1(true) // Support http1 for admin service.
        .add_service(NodeServer::new(server.clone()))
        .add_service(RaftServer::new(server.clone()))
        .add_service(RootServer::new(server.clone()))
        .add_service(make_admin_service(server.clone()))
        .add_optional_service(proxy_server.map(EngulaServer::new))
        .serve_with_incoming(incoming);

0A32A476 I created a PR , I hope it will be adopted

w41ter commented 1 year ago

@MaxKingPor Hi, here already exists a PR: https://github.com/hyperium/tonic/pull/1093.

MaxKingPor commented 1 year ago

I'm sorry. I didn't see it. It's been closed.