Skip to content

Commit

Permalink
refactor: remove unnecessary receiver field from GracefulShutdown (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiroaisen authored Jun 18, 2024
1 parent 72884c5 commit 597f92b
Showing 1 changed file with 4 additions and 7 deletions.
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

0 comments on commit 597f92b

Please sign in to comment.