1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use tokio::sync::watch;
use bat_markets_core::{ErrorKind, HealthReport, MarketError, Result};
/// RAII-style watcher for runtime status changes.
///
/// Dropping the watcher releases the local subscription. Rust ownership scopes
/// the subscription without a global stop method.
pub struct StatusWatch {
receiver: watch::Receiver<HealthReport>,
}
impl StatusWatch {
pub(crate) const fn new(receiver: watch::Receiver<HealthReport>) -> Self {
Self { receiver }
}
/// Return the current cached status snapshot without waiting.
#[must_use]
pub fn current(&self) -> HealthReport {
self.receiver.borrow().clone()
}
/// Wait for the next status change.
pub async fn recv(&mut self) -> Result<HealthReport> {
self.receiver.changed().await.map_err(|error| {
MarketError::new(
ErrorKind::TransportError,
format!("status watch receive failed: {error}"),
)
})?;
Ok(self.current())
}
/// Explicitly end the watch.
///
/// This mirrors other watch handles. Dropping the handle is equivalent for
/// the local health subscription.
pub async fn shutdown(self) -> Result<()> {
Ok(())
}
}