|
| 1 | +//! # Debouncer for notifications. |
| 2 | +//! |
| 3 | +//! Sometimes the client application may be reinstalled |
| 4 | +//! while keeping the notification token. |
| 5 | +//! In this case the same token is stored twice |
| 6 | +//! for the same mailbox on a chatmail relay |
| 7 | +//! and is notified twice for the same message. |
| 8 | +//! Since it is not possible for the chatmail relay |
| 9 | +//! to deduplicate the tokens in this case |
| 10 | +//! as only the notification gateway |
| 11 | +//! can decrypt them, notification gateway needs |
| 12 | +//! to debounce notifications to the same token. |
| 13 | +
|
| 14 | +use std::time::{Instant, Duration}; |
| 15 | +use std::collections::{HashSet, BinaryHeap}; |
| 16 | +use std::cmp::Reverse; |
| 17 | +use std::sync::Mutex; |
| 18 | + |
| 19 | +#[derive(Default)] |
| 20 | +pub(crate) struct Debouncer { |
| 21 | + state: Mutex<DebouncerState> |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Default)] |
| 25 | +struct DebouncerState { |
| 26 | + /// Set of recently notified tokens. |
| 27 | + /// |
| 28 | + /// The tokens are stored in plaintext, |
| 29 | + /// not hashed or encrypted. |
| 30 | + /// No token is stored for a long time anyway. |
| 31 | + tokens: HashSet<String>, |
| 32 | + |
| 33 | + /// Binary heap storing tokens |
| 34 | + /// sorted by the timestamp of the recent notifications. |
| 35 | + /// |
| 36 | + /// `Reverse` is used to turn max-heap into min-heap. |
| 37 | + heap: BinaryHeap<Reverse<(Instant, String)>>, |
| 38 | +} |
| 39 | + |
| 40 | +impl DebouncerState { |
| 41 | + /// Removes old entries for tokens that can be notified again. |
| 42 | + fn cleanup(&mut self) { |
| 43 | + let now = Instant::now(); |
| 44 | + loop { |
| 45 | + let Some(Reverse((timestamp, token))) = self.heap.pop() else { |
| 46 | + break; |
| 47 | + }; |
| 48 | + |
| 49 | + if timestamp.duration_since(now) < Duration::from_secs(10) { |
| 50 | + self.heap.push(Reverse((timestamp, token))); |
| 51 | + break; |
| 52 | + } |
| 53 | + |
| 54 | + self.tokens.remove(&token); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /// Returns true if the token was notified recently |
| 59 | + /// and should not be notified again. |
| 60 | + fn is_debounced(&mut self, token: &String) -> bool { |
| 61 | + self.cleanup(); |
| 62 | + self.tokens.contains(token) |
| 63 | + } |
| 64 | + |
| 65 | + fn notify(&mut self, token: String) { |
| 66 | + if self.tokens.insert(token.clone()) { |
| 67 | + self.heap.push(Reverse((Instant::now(), token))); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl Debouncer { |
| 73 | + pub(crate) fn is_debounced(&self, token: &String) -> bool { |
| 74 | + let mut state = self.state.lock().unwrap(); |
| 75 | + state.is_debounced(token) |
| 76 | + } |
| 77 | + |
| 78 | + pub(crate) fn notify(&self, token: String) { |
| 79 | + self.state.lock().unwrap().notify(token); |
| 80 | + } |
| 81 | +} |
0 commit comments