-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Connection Timeout for client request #498
Comments
Here is a PoC code that insert TimeoutConnector before passing $ git di
diff --git a/tonic/Cargo.toml b/tonic/Cargo.toml
index 39b0276..f355d89 100644
--- a/tonic/Cargo.toml
+++ b/tonic/Cargo.toml
@@ -65,6 +65,7 @@ async-trait = { version = "0.1.13", optional = true }
# transport
hyper = { version = "0.13.4", features = ["stream"], optional = true }
+hyper-timeout = "0.3.1"
tokio = { version = "0.2.13", features = ["tcp"], optional = true }
tower = { version = "0.3", optional = true}
tower-make = { version = "0.3", features = ["connect"] }
diff --git a/tonic/src/transport/channel/endpoint.rs b/tonic/src/transport/channel/endpoint.rs
index 788fa2e..6a4a744 100644
--- a/tonic/src/transport/channel/endpoint.rs
+++ b/tonic/src/transport/channel/endpoint.rs
@@ -243,6 +243,8 @@ impl Endpoint {
#[cfg(not(feature = "tls"))]
let connector = service::connector(http);
+ let mut connector = hyper_timeout::TimeoutConnector::new(connector);
+
Channel::connect(connector, self.clone()).await
}
@@ -262,6 +264,8 @@ impl Endpoint {
#[cfg(not(feature = "tls"))]
let connector = service::connector(http);
+ let mut connector = hyper_timeout::TimeoutConnector::new(connector);
+
Ok(Channel::new(connector, self.clone()))
}
@@ -283,6 +287,8 @@ impl Endpoint {
#[cfg(not(feature = "tls"))]
let connector = service::connector(connector);
+ let mut connector = hyper_timeout::TimeoutConnector::new(connector);
+
Channel::connect(connector, self.clone()).await
} |
This seems like a good idea 👍 I plan on doing some rework of the transport so will keep this in mind. Sorry for the delay. |
@akiradeveloper is there an advantage in using the I recently hit an issue with connection timeouts in Tonic and resolved it with this PR. Note that you can also work around this by using connect_with_connector and supplying a Hyper HttpConnector with the connect timeout set appropriately, though this does expose the Tonic client to Hyper directly which is not ideal. @LucioFranco depending on when you intend to do the rework of the transport code, would you consider an interim PR such as any of the options discussed above to add the ability to set a connect timeout? |
No I don't think so. There can be possible implementations but your way looks better to me. I am looking forward to the new feature. |
Pulls in [hyper-timeout] which has a connector that can have a timeout applied. I did consider vendoring hyper-timeout since its a fairly small crate. I guess we can always do that later since its not exposed publicly. I would also like to add a test but I'm not sure about the best way of testing this. Fixes #498 [hyper-timeout]: https://github.com/hjr3/hyper-timeout
Pulls in [hyper-timeout] which has a connector that can have a timeout applied. I did consider vendoring hyper-timeout since its a fairly small crate. I guess we can always do that later since its not exposed publicly. I would also like to add a test but I'm not sure about the best way of testing this. Fixes #498 [hyper-timeout]: https://github.com/hjr3/hyper-timeout Co-authored-by: Lucio Franco <[email protected]>
Feature Request
Crates
TimeoutConnector
. I think this is doing basically the same thing asconnect_timeout
in reqwest because both uses tokio::time::Timeout wrapper.Motivation
We can send a http/https request using
Endpoint
but it doesn't support connection timeout.Yes, it has
timeout
method but it is a timeout for the entire request.Proposal
Add
connect_timeout
toEndpoint
and wrap the connector byTimeoutConnector
.Alternatives
(1)
TimeoutConnector
is just aService -> Service
function. So other solution is addstacking_fn: Service -> Service
toconnect
function. This is a more versatile solution.(2)
Endpoint
has 3 connect variants but they all callChannel::connect
at the end. My idea is factoring out the code before that. This make it easy to wrap the connector by users.The text was updated successfully, but these errors were encountered: