Skip to content

Commit

Permalink
support shutdown hooks (#228)
Browse files Browse the repository at this point in the history
* support shutdown hooks

* fix: cargo fmt
  • Loading branch information
Yanhao authored Sep 21, 2023
1 parent ee94d93 commit 5b06e2c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
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

0 comments on commit 5b06e2c

Please sign in to comment.