Skip to content

Commit

Permalink
fix: remove unnecesary receiver field from GracefulShutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiroaisen committed Jun 18, 2024
1 parent 72884c5 commit 4b7d0dd
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 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 @@ -45,8 +44,7 @@ impl GracefulShutdown {
/// 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 4b7d0dd

Please sign in to comment.