Skip to content

Commit 5c2f4db

Browse files
authored
feat(transport): Change channel connect to be async (#107)
This makes it so you can check if the initial connection is established. Before this we used reconnect which would lazily attempt to connect. So if you were trying to connect to a non existant Server you wouldn't find out until after you attempted your first RPC. This simplifies everything by allowing you connect before creating the RPC client. BREAKING CHANGE: `Endpoint::channel` was removed in favor of an async `Endpoint::connect`.
1 parent 108bad0 commit 5c2f4db

File tree

18 files changed

+243
-38
lines changed

18 files changed

+243
-38
lines changed

tonic-build/src/client.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@ fn generate_connect(service_ident: &syn::Ident) -> TokenStream {
5454
quote! {
5555
impl #service_ident<tonic::transport::Channel> {
5656
/// Attempt to create a new client by connecting to a given endpoint.
57-
pub fn connect<D>(dst: D) -> Result<Self, tonic::transport::Error>
57+
pub async fn connect<D>(dst: D) -> Result<Self, tonic::transport::Error>
5858
where
5959
D: std::convert::TryInto<tonic::transport::Endpoint>,
6060
D::Error: Into<StdError>,
6161
{
62-
tonic::transport::Endpoint::new(dst).map(|c| Self::new(c.channel()))
62+
let conn = tonic::transport::Endpoint::new(dst)?.connect().await?;
63+
Ok(Self::new(conn))
6364
}
6465
}
6566
}

tonic-examples/src/authentication/client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1515
HeaderValue::from_static("Bearer some-secret-token"),
1616
);
1717
})
18-
.channel();
18+
.connect()
19+
.await?;
1920

2021
let mut client = EchoClient::new(channel);
2122

tonic-examples/src/gcp/client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3737
headers.insert("authorization", header_value.clone());
3838
})
3939
.tls_config(&tls_config)
40-
.channel();
40+
.connect()
41+
.await?;
4142

4243
let mut service = PublisherClient::new(channel);
4344

tonic-examples/src/helloworld/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use hello_world::{client::GreeterClient, HelloRequest};
66

77
#[tokio::main]
88
async fn main() -> Result<(), Box<dyn std::error::Error>> {
9-
let mut client = GreeterClient::connect("http://[::1]:50051")?;
9+
let mut client = GreeterClient::connect("http://[::1]:50051").await?;
1010

1111
let request = tonic::Request::new(HelloRequest {
1212
name: "Tonic".into(),

tonic-examples/src/multiplex/client.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use tonic::transport::Endpoint;
1212

1313
#[tokio::main]
1414
async fn main() -> Result<(), Box<dyn std::error::Error>> {
15-
let channel = Endpoint::from_static("http://[::1]:50051").channel();
15+
let channel = Endpoint::from_static("http://[::1]:50051")
16+
.connect()
17+
.await?;
1618

1719
let mut greeter_client = GreeterClient::new(channel.clone());
1820
let mut echo_client = EchoClient::new(channel);

tonic-examples/src/routeguide/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ async fn run_route_chat(client: &mut RouteGuideClient<Channel>) -> Result<(), Bo
9191

9292
#[tokio::main]
9393
async fn main() -> Result<(), Box<dyn std::error::Error>> {
94-
let mut client = RouteGuideClient::connect("http://[::1]:10000")?;
94+
let mut client = RouteGuideClient::connect("http://[::1]:10000").await?;
9595

9696
println!("*** SIMPLE RPC ***");
9797
let response = client

tonic-examples/src/tls/client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1717

1818
let channel = Channel::from_static("http://[::1]:50051")
1919
.tls_config(&tls)
20-
.channel();
20+
.connect()
21+
.await?;
2122

2223
let mut client = EchoClient::new(channel);
2324
let request = tonic::Request::new(EchoRequest {

tonic-examples/src/tls_client_auth/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2121

2222
let channel = Channel::from_static("http://[::1]:50051")
2323
.tls_config(&tls)
24-
.clone()
25-
.channel();
24+
.connect()
25+
.await?;
2626

2727
let mut client = EchoClient::new(channel);
2828

tonic-interop/src/bin/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
4141
);
4242
}
4343

44-
let channel = endpoint.channel();
44+
let channel = endpoint.connect().await?;
4545

4646
let mut client = client::TestClient::new(channel.clone());
4747
let mut unimplemented_client = client::UnimplementedClient::new(channel);

tonic/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ transport = [
2929
"hyper",
3030
"tokio",
3131
"tower",
32-
"tower-reconnect",
3332
"tower-balance",
3433
"tower-load",
3534
]
@@ -68,7 +67,6 @@ hyper = { version = "=0.13.0-alpha.4", features = ["unstable-stream"], optional
6867
tokio = { version = "=0.2.0-alpha.6", default-features = false, features = ["tcp"], optional = true }
6968
tower = { version = "=0.3.0-alpha.2", optional = true}
7069
tower-make = "=0.3.0-alpha.2a"
71-
tower-reconnect = { version = "=0.3.0-alpha.2", optional = true }
7270
tower-balance = { version = "=0.3.0-alpha.2", optional = true }
7371
tower-load = { version = "=0.3.0-alpha.2", optional = true }
7472

0 commit comments

Comments
 (0)