|
| 1 | +use std::{collections::HashMap, sync::atomic::AtomicBool}; |
| 2 | + |
| 3 | +use tokio::sync::{mpsc, RwLock}; |
| 4 | + |
| 5 | +use super::store::DeploymentId; |
| 6 | + |
| 7 | +const DEFAULT_BUFFER_SIZE: usize = 100; |
| 8 | + |
| 9 | +#[derive(Debug)] |
| 10 | +pub struct Subscriptions<T> { |
| 11 | + inner: RwLock<HashMap<DeploymentId, mpsc::Sender<T>>>, |
| 12 | +} |
| 13 | + |
| 14 | +/// A control structure for managing tracing subscriptions. |
| 15 | +#[derive(Debug)] |
| 16 | +pub struct TracingControl<T> { |
| 17 | + enabled: AtomicBool, |
| 18 | + subscriptions: Subscriptions<T>, |
| 19 | + default_buffer_size: usize, |
| 20 | +} |
| 21 | + |
| 22 | +impl<T> Default for TracingControl<T> { |
| 23 | + fn default() -> Self { |
| 24 | + Self { |
| 25 | + enabled: AtomicBool::new(false), |
| 26 | + subscriptions: Subscriptions { |
| 27 | + inner: RwLock::new(HashMap::new()), |
| 28 | + }, |
| 29 | + default_buffer_size: DEFAULT_BUFFER_SIZE, |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl<T> TracingControl<T> { |
| 35 | + pub fn new(default_buffer_size: Option<usize>) -> Self { |
| 36 | + Self { |
| 37 | + enabled: AtomicBool::new(false), |
| 38 | + subscriptions: Subscriptions { |
| 39 | + inner: RwLock::new(HashMap::new()), |
| 40 | + }, |
| 41 | + default_buffer_size: default_buffer_size.unwrap_or(DEFAULT_BUFFER_SIZE), |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /// Creates a channel sender for the given deployment ID. Only one subscription |
| 46 | + /// can exist for a given deployment ID. If tracing is disabled or no subscription |
| 47 | + /// exists, it will return None. Calling producer when a dead subscription exists |
| 48 | + /// will incur a cleanup cost. |
| 49 | + pub async fn producer(&self, key: DeploymentId) -> Option<mpsc::Sender<T>> { |
| 50 | + if !self.enabled.load(std::sync::atomic::Ordering::Relaxed) { |
| 51 | + return None; |
| 52 | + } |
| 53 | + |
| 54 | + let subs = self.subscriptions.inner.read().await; |
| 55 | + let tx = subs.get(&key); |
| 56 | + |
| 57 | + match tx { |
| 58 | + Some(tx) if tx.is_closed() => { |
| 59 | + drop(subs); |
| 60 | + let mut subs = self.subscriptions.inner.write().await; |
| 61 | + subs.remove(&key); |
| 62 | + |
| 63 | + if subs.is_empty() { |
| 64 | + self.enabled |
| 65 | + .store(false, std::sync::atomic::Ordering::Relaxed); |
| 66 | + } |
| 67 | + |
| 68 | + None |
| 69 | + } |
| 70 | + None => None, |
| 71 | + tx => tx.cloned(), |
| 72 | + } |
| 73 | + } |
| 74 | + pub async fn subscribe_with_chan_size( |
| 75 | + &self, |
| 76 | + key: DeploymentId, |
| 77 | + buffer_size: usize, |
| 78 | + ) -> mpsc::Receiver<T> { |
| 79 | + let (tx, rx) = mpsc::channel(buffer_size); |
| 80 | + let mut guard = self.subscriptions.inner.write().await; |
| 81 | + guard.insert(key, tx); |
| 82 | + self.enabled |
| 83 | + .store(true, std::sync::atomic::Ordering::Relaxed); |
| 84 | + |
| 85 | + rx |
| 86 | + } |
| 87 | + |
| 88 | + /// Creates a new subscription for a given deployment ID. If a subscription already |
| 89 | + /// exists, it will be replaced. |
| 90 | + pub async fn subscribe(&self, key: DeploymentId) -> mpsc::Receiver<T> { |
| 91 | + self.subscribe_with_chan_size(key, self.default_buffer_size) |
| 92 | + .await |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +#[cfg(test)] |
| 97 | +mod test { |
| 98 | + |
| 99 | + use super::*; |
| 100 | + use std::sync::atomic::Ordering::Relaxed; |
| 101 | + use std::sync::Arc; |
| 102 | + |
| 103 | + #[tokio::test] |
| 104 | + async fn test_tracing_control() { |
| 105 | + let control: TracingControl<()> = TracingControl::default(); |
| 106 | + let control = Arc::new(control); |
| 107 | + assert_eq!(false, control.enabled.load(Relaxed)); |
| 108 | + |
| 109 | + let tx = control.producer(DeploymentId(123)).await; |
| 110 | + assert!(tx.is_none()); |
| 111 | + |
| 112 | + let rx = control.subscribe(DeploymentId(123)).await; |
| 113 | + assert_eq!(true, control.enabled.load(Relaxed)); |
| 114 | + |
| 115 | + drop(rx); |
| 116 | + let tx = control.producer(DeploymentId(123)).await; |
| 117 | + assert!(tx.is_none()); |
| 118 | + assert_eq!(false, control.enabled.load(Relaxed)); |
| 119 | + |
| 120 | + _ = control.subscribe(DeploymentId(123)).await; |
| 121 | + assert_eq!(true, control.enabled.load(Relaxed)); |
| 122 | + } |
| 123 | +} |
0 commit comments