-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(server): Prevent
broadcast::Receiver
leaks with a custom channel
- Loading branch information
Showing
6 changed files
with
125 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//! # A wrapper around a [`mpsc`] channel that detects disconnects. | ||
//! | ||
//! Implements the [`Deref`] trait (`Target = mpsc::Receiver<T>`), and uses a [`oneshot`] | ||
//! channel to send a single message back when the whole thing gets dropped. | ||
//! | ||
//! **Source & further reading:** <https://github.com/hyperium/tonic/issues/377> | ||
use futures::Stream; | ||
use std::task::{Context, Poll}; | ||
use std::{ops::Deref, pin::Pin}; | ||
use tokio::sync::{mpsc, oneshot}; | ||
|
||
pub struct DisconnectChannel<T> { | ||
pub(crate) signal_sender: Option<oneshot::Sender<()>>, | ||
pub(crate) inner_channel: mpsc::Receiver<T>, | ||
} | ||
|
||
impl<T> Stream for DisconnectChannel<T> { | ||
type Item = T; | ||
|
||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
Pin::new(&mut self.inner_channel).poll_recv(cx) | ||
} | ||
} | ||
|
||
impl<T> Deref for DisconnectChannel<T> { | ||
type Target = mpsc::Receiver<T>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.inner_channel | ||
} | ||
} | ||
|
||
impl<T> Drop for DisconnectChannel<T> { | ||
fn drop(&mut self) { | ||
if let Some(drop_signal) = self.signal_sender.take() { | ||
let _ = drop_signal.send(()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters