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(transport): Add server graceful shutdown #169

Merged
merged 8 commits into from
Dec 11, 2019
Merged
38 changes: 29 additions & 9 deletions tonic/src/transport/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub struct Server {
init_connection_window_size: Option<u32>,
max_concurrent_streams: Option<u32>,
tcp_keepalive: Option<Duration>,
shutdown: Option<Box<dyn Future<Output=()>>>,
tcp_nodelay: bool,
}

Expand Down Expand Up @@ -110,6 +111,14 @@ impl Server {
}
}

/// Set the graceful shutdown thing
pub fn graceful_shutdown(self, shutdown: Box<dyn Future<Output=()>>) -> Self {
Copy link
Member

Choose a reason for hiding this comment

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

I'd probably make this impl Future<Output = ()> and then box it internally.

Server {
shutdown: Some(shutdown),
..self
}
}

// FIXME: tower-timeout currentlly uses `From` instead of `Into` for the error
// so our services do not align.
// pub fn timeout(&mut self, timeout: Duration) -> &mut Self {
Expand Down Expand Up @@ -268,15 +277,26 @@ impl Server {
concurrency_limit,
// timeout,
};
xd009642 marked this conversation as resolved.
Show resolved Hide resolved

hyper::Server::builder(incoming)
.http2_only(true)
.http2_initial_connection_window_size(init_connection_window_size)
.http2_initial_stream_window_size(init_stream_window_size)
.http2_max_concurrent_streams(max_concurrent_streams)
.serve(svc)
.await
.map_err(map_err)?;
if let Some(rx) = self.shutdown {
hyper::server::builder(incoming)
.http2_only(true)
.http2_initial_connection_window_size(init_connection_window_size)
.http2_initial_stream_window_size(init_stream_window_size)
.http2_max_concurrent_streams(max_concurrent_streams)
.serve(svc)
.with_graceful_shutdown(rx)
.await
.map_err(map_err)?;
} else {
hyper::server::builder(incoming)
.http2_only(true)
.http2_initial_connection_window_size(init_connection_window_size)
.http2_initial_stream_window_size(init_stream_window_size)
.http2_max_concurrent_streams(max_concurrent_streams)
.serve(svc)
.await
.map_err(map_err)?;
}

Ok(())
}
Expand Down