Skip to content
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

Support timeouts with "grpc-timeout" header #516

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions tests/integration_tests/tests/timeout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use futures_util::FutureExt;
use integration_tests::pb::{test_client, test_server, Input, Output};
use std::time::Duration;
use tokio::sync::oneshot;
use tonic::{
transport::Server,
Request, Response, Status,
};

#[tokio::test]
async fn cancelation_on_timeout() {
struct Svc;

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, _req: Request<Input>) -> Result<Response<Output>, Status> {
// Wait for a time longer than the timeout
tokio::time::delay_for(Duration::from_millis(1_000)).await;
Ok(Response::new(Output {}))
}
}

let svc = test_server::TestServer::new(Svc);

let (tx, rx) = oneshot::channel::<()>();

let jh = tokio::spawn(async move {
Server::builder()
.add_service(svc)
.serve_with_shutdown("127.0.0.1:1322".parse().unwrap(), rx.map(drop))
.await
.unwrap();
});

tokio::time::delay_for(Duration::from_millis(100)).await;

let mut client = test_client::TestClient::connect("http://127.0.0.1:1322")
.await
.unwrap();

let mut req = Request::new(Input {});
req.metadata_mut().insert("grpc-timeout", "500m".parse().unwrap());

let err = client.unary_call(req).await.unwrap_err();
assert!(err.message().contains("Timeout expired"));
// TODO: Need to return the correct type of code
// assert_eq!(err.code(), Code::Cancelled);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like something that should be fixed before merging 🤔


tx.send(()).unwrap();

jh.await.unwrap();
}
2 changes: 1 addition & 1 deletion tonic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async-trait = { version = "0.1.13", optional = true }

# transport
hyper = { version = "0.13.4", features = ["stream"], optional = true }
tokio = { version = "0.2.13", features = ["tcp"], optional = true }
tokio = { version = "0.2.13", features = ["tcp", "time"], optional = true }
tower = { version = "0.3", optional = true}
tower-make = { version = "0.3", features = ["connect"] }
tower-balance = { version = "0.3", optional = true }
Expand Down
3 changes: 1 addition & 2 deletions tonic/src/metadata/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,10 @@ pub struct OccupiedEntry<'a, VE: ValueEncoding> {

impl MetadataMap {
// Headers reserved by the gRPC protocol.
pub(crate) const GRPC_RESERVED_HEADERS: [&'static str; 8] = [
pub(crate) const GRPC_RESERVED_HEADERS: [&'static str; 7] = [
"te",
"user-agent",
"content-type",
"grpc-timeout",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing this definitely isn't right, but otherwise the header was getting stripped when I tried to set it in the integration test. Would love some guidance on how to set this header from a test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its fine. As the user is now allowed to set it I guess its not reserved anymore.

"grpc-message",
"grpc-encoding",
"grpc-message-type",
Expand Down
5 changes: 2 additions & 3 deletions tonic/src/transport/service/connection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::super::BoxFuture;
use super::{layer::ServiceBuilderExt, reconnect::Reconnect, AddOrigin, UserAgent};
use super::{layer::ServiceBuilderExt, reconnect::Reconnect, AddOrigin, Timeout, UserAgent};
use crate::{body::BoxBody, transport::Endpoint};
use http::Uri;
use hyper::client::conn::Builder;
Expand All @@ -13,7 +13,6 @@ use tokio::io::{AsyncRead, AsyncWrite};
use tower::{
layer::Layer,
limit::{concurrency::ConcurrencyLimitLayer, rate::RateLimitLayer},
timeout::TimeoutLayer,
util::BoxService,
ServiceBuilder, ServiceExt,
};
Expand Down Expand Up @@ -53,7 +52,7 @@ impl Connection {
let stack = ServiceBuilder::new()
.layer_fn(|s| AddOrigin::new(s, endpoint.uri.clone()))
.layer_fn(|s| UserAgent::new(s, endpoint.user_agent.clone()))
.optional_layer(endpoint.timeout.map(TimeoutLayer::new))
.layer_fn(|s| Timeout::new(s, endpoint.timeout))
.optional_layer(endpoint.concurrency_limit.map(ConcurrencyLimitLayer::new))
.optional_layer(endpoint.rate_limit.map(|(l, d)| RateLimitLayer::new(l, d)))
.into_inner();
Expand Down
2 changes: 2 additions & 0 deletions tonic/src/transport/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod io;
mod layer;
mod reconnect;
mod router;
mod timeout;
#[cfg(feature = "tls")]
mod tls;
mod user_agent;
Expand All @@ -17,6 +18,7 @@ pub(crate) use self::discover::DynamicServiceStream;
pub(crate) use self::io::ServerIo;
pub(crate) use self::layer::ServiceBuilderExt;
pub(crate) use self::router::{Or, Routes};
pub(crate) use self::timeout::Timeout;
#[cfg(feature = "tls")]
pub(crate) use self::tls::{TlsAcceptor, TlsConnector};
pub(crate) use self::user_agent::UserAgent;
Loading