|
| 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::cmp::Reverse; |
| 15 | +use std::collections::{BinaryHeap, HashSet}; |
| 16 | +use std::sync::Mutex; |
| 17 | +use std::time::{Duration, Instant}; |
| 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, now: Instant) { |
| 43 | + loop { |
| 44 | + let Some(Reverse((timestamp, token))) = self.heap.pop() else { |
| 45 | + break; |
| 46 | + }; |
| 47 | + |
| 48 | + if now.duration_since(timestamp) < Duration::from_secs(1) { |
| 49 | + self.heap.push(Reverse((timestamp, token))); |
| 50 | + break; |
| 51 | + } |
| 52 | + |
| 53 | + self.tokens.remove(&token); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + fn is_debounced(&mut self, now: Instant, token: &String) -> bool { |
| 58 | + self.cleanup(now); |
| 59 | + self.tokens.contains(token) |
| 60 | + } |
| 61 | + |
| 62 | + fn notify(&mut self, now: Instant, token: String) { |
| 63 | + if self.tokens.insert(token.clone()) { |
| 64 | + self.heap.push(Reverse((now, token))); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + fn count(&self) -> usize { |
| 69 | + let res = self.tokens.len(); |
| 70 | + debug_assert_eq!(res, self.heap.len()); |
| 71 | + res |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl Debouncer { |
| 76 | + /// Returns true if the token was notified recently |
| 77 | + /// and should not be notified again. |
| 78 | + pub(crate) fn is_debounced(&self, now: Instant, token: &String) -> bool { |
| 79 | + let mut state = self.state.lock().unwrap(); |
| 80 | + state.is_debounced(now, token) |
| 81 | + } |
| 82 | + |
| 83 | + pub(crate) fn notify(&self, now: Instant, token: String) { |
| 84 | + self.state.lock().unwrap().notify(now, token); |
| 85 | + } |
| 86 | + |
| 87 | + /// Returns number of currently debounced notification tokens. |
| 88 | + /// |
| 89 | + /// This is used for metrics to display the size of the set. |
| 90 | + /// |
| 91 | + /// This function does not remove expired tokens. |
| 92 | + pub(crate) fn count(&self) -> usize { |
| 93 | + self.state.lock().unwrap().count() |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#[cfg(test)] |
| 98 | +mod tests { |
| 99 | + use super::*; |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn test_debouncer() { |
| 103 | + let mut now = Instant::now(); |
| 104 | + |
| 105 | + let debouncer = Debouncer::default(); |
| 106 | + |
| 107 | + let token1 = "foobar".to_string(); |
| 108 | + let token2 = "barbaz".to_string(); |
| 109 | + |
| 110 | + assert!(!debouncer.is_debounced(now, &token1)); |
| 111 | + assert!(!debouncer.is_debounced(now, &token2)); |
| 112 | + assert_eq!(debouncer.count(), 0); |
| 113 | + |
| 114 | + debouncer.notify(now, token1.clone()); |
| 115 | + |
| 116 | + assert!(debouncer.is_debounced(now, &token1)); |
| 117 | + assert!(!debouncer.is_debounced(now, &token2)); |
| 118 | + assert_eq!(debouncer.count(), 1); |
| 119 | + |
| 120 | + now += Duration::from_secs(5); |
| 121 | + |
| 122 | + assert!(!debouncer.is_debounced(now, &token1)); |
| 123 | + assert!(!debouncer.is_debounced(now, &token2)); |
| 124 | + assert_eq!(debouncer.count(), 0); |
| 125 | + } |
| 126 | +} |
0 commit comments