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

feat(tonic): support setting timeout for each request #156

Merged
merged 4 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.2.25] - 2023-07-03

### Added

- tonic: Support setting timeout for each request.

### Fixed

- s3: Fix compile error.

## [0.2.24] - 2023-06-26

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion madsim-aws-sdk-s3/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "madsim-aws-sdk-s3"
version = "0.2.24+0.28"
version = "0.2.25+0.28"
edition = "2021"
authors = ["Kevin Axel <[email protected]>"]
description = "The s3 simulator on madsim."
Expand Down
2 changes: 1 addition & 1 deletion madsim-tonic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "madsim-tonic"
version = "0.2.21"
version = "0.2.25"
edition = "2021"
authors = ["Runji Wang <[email protected]>"]
description = "The `tonic` simulator on madsim."
Expand Down
8 changes: 4 additions & 4 deletions madsim-tonic/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<F: Interceptor> Grpc<crate::transport::Channel, F> {
M1: Send + Sync + 'static,
M2: Send + Sync + 'static,
{
let timeout = self.inner.timeout;
let timeout = request.timeout().or(self.inner.timeout);
let future = async move {
request.append_metadata();
let request = request.intercept(&mut self.interceptor)?.boxed();
Expand Down Expand Up @@ -90,7 +90,7 @@ impl<F: Interceptor> Grpc<crate::transport::Channel, F> {
M1: Send + Sync + 'static,
M2: Send + Sync + 'static,
{
let timeout = self.inner.timeout;
let timeout = request.timeout().or(self.inner.timeout);
let future = async move {
request.append_metadata();
let request = request.intercept(&mut self.interceptor)?;
Expand Down Expand Up @@ -121,7 +121,7 @@ impl<F: Interceptor> Grpc<crate::transport::Channel, F> {
M1: Send + Sync + 'static,
M2: Send + Sync + 'static,
{
let timeout = self.inner.timeout;
let timeout = request.timeout().or(self.inner.timeout);
let future = async move {
request.append_metadata();
let request = request.intercept(&mut self.interceptor)?.boxed();
Expand Down Expand Up @@ -151,7 +151,7 @@ impl<F: Interceptor> Grpc<crate::transport::Channel, F> {
M1: Send + Sync + 'static,
M2: Send + Sync + 'static,
{
let timeout = self.inner.timeout;
let timeout = request.timeout().or(self.inner.timeout);
let future = async move {
request.append_metadata();
let request = request.intercept(&mut self.interceptor)?;
Expand Down
18 changes: 18 additions & 0 deletions madsim-tonic/src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl<T> AppendMetadata for Result<Response<T>, Status> {
pub mod codegen {
use std::any::Any;
pub use std::net::SocketAddr;
use std::time::Duration;
use tonic::{
metadata::MetadataMap, service::Interceptor, Extensions, Request, Response, Status,
};
Expand All @@ -60,6 +61,7 @@ pub mod codegen {
pub type IdentityInterceptor = fn(Request<()>) -> Result<Request<()>, Status>;

pub trait RequestExt<T>: Sized {
fn timeout(&self) -> Option<Duration>;
fn set_remote_addr(&mut self, addr: SocketAddr);
fn intercept<F: Interceptor>(self, interceptor: &mut F) -> Result<Self, Status>;
fn boxed(self) -> Request<BoxMessage>
Expand All @@ -68,6 +70,22 @@ pub mod codegen {
}

impl<T> RequestExt<T> for Request<T> {
/// Get the timeout of Request.
fn timeout(&self) -> Option<Duration> {
let s = self.metadata().get("grpc-timeout")?.to_str().unwrap();
let (value, unit) = s.split_at(s.len() - 1);
let value = value.parse::<u64>().expect("invalid grpc-timeout value");
Some(match unit {
"H" => Duration::from_secs(value * 60 * 60),
"M" => Duration::from_secs(value * 60),
"S" => Duration::from_secs(value),
"m" => Duration::from_millis(value),
"u" => Duration::from_micros(value),
"n" => Duration::from_nanos(value),
_ => panic!("invalid grpc-timeout unit: {unit}"),
})
}

/// Set the remote address of Request.
fn set_remote_addr(&mut self, addr: SocketAddr) {
let tcp_info: tonic::transport::server::TcpConnectInfo =
Expand Down
8 changes: 8 additions & 0 deletions tonic-example/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,14 @@ async fn request_timeout() {
let error = client.delay(request()).await.unwrap_err();
assert_eq!(error.code(), tonic::Code::DeadlineExceeded);
assert!(t0.elapsed() < Duration::from_secs(2));

// timeout set on request should override channel timeout
let mut req = request();
req.set_timeout(Duration::from_secs(5));
let t0 = Instant::now();
let error = client.delay(req).await.unwrap_err();
assert_eq!(error.code(), tonic::Code::DeadlineExceeded);
assert!(t0.elapsed() >= Duration::from_secs(5));
})
.await
.unwrap();
Expand Down