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

fix: remove unnecesary receiver field from GracefulShutdown #131

Merged
merged 2 commits into from
Jun 18, 2024
Merged
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
11 changes: 4 additions & 7 deletions src/server/graceful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@ use tokio::sync::watch;
/// A graceful shutdown utility
pub struct GracefulShutdown {
tx: watch::Sender<()>,
rx: watch::Receiver<()>,
}

impl GracefulShutdown {
/// Create a new graceful shutdown helper.
pub fn new() -> Self {
let (tx, rx) = watch::channel(());
Self { tx, rx }
let (tx, _) = watch::channel(());
Self { tx }
}

/// Wrap a future for graceful shutdown watching.
pub fn watch<C: GracefulConnection>(&self, conn: C) -> impl Future<Output = C::Output> {
let mut rx = self.rx.clone();
let mut rx = self.tx.subscribe();
GracefulConnectionFuture::new(conn, async move {
let _ = rx.changed().await;
// hold onto the rx until the watched future is completed
Expand All @@ -44,9 +43,7 @@ impl GracefulShutdown {
/// This returns a `Future` which will complete once all watched
/// connections have shutdown.
pub async fn shutdown(self) {
// drop the rx immediately, or else it will hold us up
let Self { tx, rx } = self;
drop(rx);
let Self { tx } = self;

// signal all the watched futures about the change
let _ = tx.send(());
Expand Down