aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2020-12-07 13:35:24 +0100
committerAlex Auvolat <alex@adnab.me>2020-12-07 13:35:24 +0100
commit5a9ae8615ee616b11460a046deaa6981b10d69ab (patch)
treef625d976531902fa267c20e7359bda43c452d9c4 /src/util.rs
parent83789a3076e986782af60ba32b0398414c1c82d7 (diff)
downloadnetapp-5a9ae8615ee616b11460a046deaa6981b10d69ab.tar.gz
netapp-5a9ae8615ee616b11460a046deaa6981b10d69ab.zip
Do not close connections immediately on close signal, await for remaining responses
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs
index f09a3bc..017ef00 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -1,5 +1,7 @@
use serde::Serialize;
+use tokio::sync::watch;
+
/// Utility function: encodes any serializable value in MessagePack binary format
/// using the RMP library.
///
@@ -16,3 +18,21 @@ where
val.serialize(&mut se)?;
Ok(wr)
}
+
+/// This async function returns only when a true signal was received
+/// from a watcher that tells us when to exit.
+/// Usefull in a select statement to interrupt another
+/// future:
+/// ```
+/// select!(
+/// _ = a_long_task() => Success,
+/// _ = await_exit(must_exit) => Interrupted,
+/// )
+/// ```
+pub async fn await_exit(mut must_exit: watch::Receiver<bool>) {
+ loop {
+ if must_exit.recv().await == Some(true) {
+ return;
+ }
+ }
+}