|
| 1 | +use std::{collections::HashMap, sync::Arc, time::Duration}; |
| 2 | + |
| 3 | +use tokio::sync::{mpsc, watch::Receiver, RwLock}; |
| 4 | + |
| 5 | +use super::store::DeploymentId; |
| 6 | + |
| 7 | +const DEFAULT_BUFFER_SIZE: usize = 100; |
| 8 | + |
| 9 | +#[derive(Debug, Clone)] |
| 10 | +pub struct Subscriptions<T> { |
| 11 | + inner: Arc<RwLock<HashMap<DeploymentId, mpsc::Sender<T>>>>, |
| 12 | +} |
| 13 | + |
| 14 | +impl<T> Default for Subscriptions<T> { |
| 15 | + fn default() -> Self { |
| 16 | + Self { |
| 17 | + inner: Arc::new(RwLock::new(HashMap::new())), |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +pub type SubscriptionsWatcher<T> = Receiver<Subscriptions<T>>; |
| 23 | + |
| 24 | +/// A control structure for managing tracing subscriptions. |
| 25 | +#[derive(Debug)] |
| 26 | +pub struct TracingControl<T> { |
| 27 | + watcher: Receiver<HashMap<DeploymentId, mpsc::Sender<T>>>, |
| 28 | + subscriptions: Subscriptions<T>, |
| 29 | + default_buffer_size: usize, |
| 30 | +} |
| 31 | + |
| 32 | +impl<T: Send + Clone + 'static> Default for TracingControl<T> { |
| 33 | + fn default() -> Self { |
| 34 | + let subscriptions = Subscriptions::default(); |
| 35 | + let subs = subscriptions.clone(); |
| 36 | + let watcher = std::thread::spawn(move || { |
| 37 | + let runtime = tokio::runtime::Builder::new_current_thread() |
| 38 | + .enable_all() |
| 39 | + .build() |
| 40 | + .unwrap(); |
| 41 | + runtime.block_on(indexer_watcher::new_watcher( |
| 42 | + Duration::from_secs(30), |
| 43 | + move || { |
| 44 | + let subs = subs.clone(); |
| 45 | + |
| 46 | + async move { Ok(subs.inner.read().await.clone()) } |
| 47 | + }, |
| 48 | + )) |
| 49 | + }) |
| 50 | + .join() |
| 51 | + .unwrap() |
| 52 | + .unwrap(); |
| 53 | + |
| 54 | + Self { |
| 55 | + subscriptions, |
| 56 | + default_buffer_size: DEFAULT_BUFFER_SIZE, |
| 57 | + watcher, |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl<T: Send + Clone + 'static> TracingControl<T> { |
| 63 | + pub fn new(default_buffer_size: Option<usize>) -> Self { |
| 64 | + Self { |
| 65 | + default_buffer_size: default_buffer_size.unwrap_or(DEFAULT_BUFFER_SIZE), |
| 66 | + ..Default::default() |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + pub fn producer(&self, key: DeploymentId) -> Option<mpsc::Sender<T>> { |
| 71 | + self.watcher |
| 72 | + .borrow() |
| 73 | + .get(&key) |
| 74 | + .cloned() |
| 75 | + .filter(|sender| !sender.is_closed()) |
| 76 | + } |
| 77 | + |
| 78 | + pub async fn subscribe_with_chan_size( |
| 79 | + &self, |
| 80 | + key: DeploymentId, |
| 81 | + buffer_size: usize, |
| 82 | + ) -> mpsc::Receiver<T> { |
| 83 | + let (tx, rx) = mpsc::channel(buffer_size); |
| 84 | + let mut guard = self.subscriptions.inner.write().await; |
| 85 | + guard.insert(key, tx); |
| 86 | + |
| 87 | + rx |
| 88 | + } |
| 89 | + |
| 90 | + /// Creates a new subscription for a given deployment ID. If a subscription already |
| 91 | + /// exists, it will be replaced. |
| 92 | + pub async fn subscribe(&self, key: DeploymentId) -> mpsc::Receiver<T> { |
| 93 | + self.subscribe_with_chan_size(key, self.default_buffer_size) |
| 94 | + .await |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +mod test { |
| 100 | + |
| 101 | + use super::*; |
| 102 | + use std::sync::Arc; |
| 103 | + |
| 104 | + #[tokio::test] |
| 105 | + async fn test_tracing_control() { |
| 106 | + let control: TracingControl<()> = TracingControl::default(); |
| 107 | + let control = Arc::new(control); |
| 108 | + |
| 109 | + // produce before subscription |
| 110 | + let tx = control.producer(DeploymentId(123)); |
| 111 | + assert!(tx.is_none()); |
| 112 | + |
| 113 | + // drop the subscription |
| 114 | + let rx = control.subscribe(DeploymentId(123)); |
| 115 | + drop(rx); |
| 116 | + |
| 117 | + // check subscription is none because channel is closed |
| 118 | + let tx = control.producer(DeploymentId(123)); |
| 119 | + assert!(tx.is_none()); |
| 120 | + |
| 121 | + // re-create subscription |
| 122 | + let _rx = control.subscribe(DeploymentId(123)); |
| 123 | + // check old subscription was replaced |
| 124 | + let tx = control.producer(DeploymentId(123)); |
| 125 | + assert!(!tx.unwrap().is_closed()) |
| 126 | + } |
| 127 | +} |
0 commit comments