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 shutdown hooks #228

Merged
merged 2 commits into from
Sep 21, 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
28 changes: 28 additions & 0 deletions volo-thrift/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
time::Duration,
};

use futures::future::BoxFuture;
use motore::{
layer::{Identity, Layer, Stack},
service::Service,
Expand Down Expand Up @@ -42,6 +43,7 @@ pub struct Server<S, L, Req, MkC, SP> {
#[cfg(feature = "multiplex")]
multiplex: bool,
span_provider: SP,
shutdown_hooks: Vec<Box<dyn FnOnce() -> BoxFuture<'static, ()>>>,
_marker: PhantomData<Req>,
}

Expand All @@ -66,12 +68,25 @@ impl<S, Req>
#[cfg(feature = "multiplex")]
multiplex: false,
span_provider: DefaultProvider {},
shutdown_hooks: Vec::new(),
_marker: PhantomData,
}
}
}

impl<S, L, Req, MkC, SP> Server<S, L, Req, MkC, SP> {
/// Register shutdown hook.
///
/// Hook functions will be called just before volo's own gracefull existing code starts,
/// in reverse order of registration.
pub fn register_shutdown_hook(
mut self,
hook: impl FnOnce() -> BoxFuture<'static, ()> + 'static,
) -> Self {
self.shutdown_hooks.push(Box::new(hook));
self
}

/// Adds a new inner layer to the server.
///
/// The layer's `Service` should be `Send + Sync + Clone + 'static`.
Expand All @@ -92,6 +107,7 @@ impl<S, L, Req, MkC, SP> Server<S, L, Req, MkC, SP> {
#[cfg(feature = "multiplex")]
multiplex: self.multiplex,
span_provider: self.span_provider,
shutdown_hooks: self.shutdown_hooks,
_marker: PhantomData,
}
}
Expand All @@ -116,6 +132,7 @@ impl<S, L, Req, MkC, SP> Server<S, L, Req, MkC, SP> {
#[cfg(feature = "multiplex")]
multiplex: self.multiplex,
span_provider: self.span_provider,
shutdown_hooks: self.shutdown_hooks,
_marker: PhantomData,
}
}
Expand Down Expand Up @@ -144,6 +161,7 @@ impl<S, L, Req, MkC, SP> Server<S, L, Req, MkC, SP> {
#[cfg(feature = "multiplex")]
multiplex: self.multiplex,
span_provider: self.span_provider,
shutdown_hooks: self.shutdown_hooks,
_marker: PhantomData,
}
}
Expand Down Expand Up @@ -291,6 +309,14 @@ impl<S, L, Req, MkC, SP> Server<S, L, Req, MkC, SP> {
}
}

if !self.shutdown_hooks.is_empty() {
info!("[VOLO] call shutdown hooks");

for hook in self.shutdown_hooks {
(hook)().await;
}
}

// received signal, graceful shutdown now
info!("[VOLO] received signal, gracefully exiting now");
*exit_flag.write() = true;
Expand Down Expand Up @@ -330,6 +356,7 @@ impl<S, L, Req, MkC, SP> Server<S, L, Req, MkC, SP> {
stat_tracer: self.stat_tracer,
multiplex,
span_provider: self.span_provider,
shutdown_hooks: self.shutdown_hooks,
_marker: PhantomData,
}
}
Expand All @@ -343,6 +370,7 @@ impl<S, L, Req, MkC, SP> Server<S, L, Req, MkC, SP> {
#[cfg(feature = "multiplex")]
multiplex: self.multiplex,
span_provider: provider,
shutdown_hooks: self.shutdown_hooks,
_marker: PhantomData,
}
}
Expand Down
5 changes: 1 addition & 4 deletions volo-thrift/src/transport/multiplex/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ where
if !oneway {
return Err(Error::Transport(pilota::thrift::TransportError::new(
TransportErrorKind::EndOfFile,
format!(
"an unexpected end of file from server, cx: {:?}",
cx
),
format!("an unexpected end of file from server, cx: {:?}", cx),
)));
}
}
Expand Down
Loading