hyperium / tonic

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

How to create channel without tls when tls feature flag is enabled #1702

Closed Sevenannn closed 4 months ago

Sevenannn commented 4 months ago

Is there any way to dynamically create the channel with/without TLS during the runtime?

I’m updating a client library using tonic. In the library, I’m calling Endpoint::from_shared to build the channel to the server.

    async fn create_client(&self) -> Result<SparkSession, SparkError> {
        let channel = Endpoint::from_shared(endpoint)
            .expect("Failed to create endpoint")
            .connect()
            .await
            .expect("Failed to create channel");

The endpoint always follows the format of: format!("https://{}:{}", self.host, self.port)

In my use case, whether connecting to a server with / without tls is only known at runtime. So I turned on the tls feature. The channel creation works well when the server is configured with tls. But fails at Endpoint.connect() and gives the following error when the server is not configured with tls. Failed to create channel: tonic::transport::Error(Transport, hyper::Error(Connect, Custom { kind: InvalidData, error: InvalidMessage(InvalidContentType) })) and the error message from my non-tls server looks like io.netty.handler.codec.http2.Http2Exception: HTTP/2 client preface string missing or corrupt. Hex dump for received bytes: 16030100f0010000ec030384282b68ce70edf6f0237bb3aa

When turning off the tls feature of tonic, the channel creation works well for the server not configured with TLS.

My question is, is there any way to create a channel without tls when tls feature is enabled in the tonic crate? Am I missing any configuration to disable/enable tls when creating the channel during the runtime?

Sevenannn commented 4 months ago

Nevermind found the cause of bug in my library