platform/monitoring/
service_registry.rs1use super::ServiceInfo;
4use actrix_proto::ServiceStatus;
5use std::collections::HashMap;
6use std::sync::Arc;
7use tokio::sync::RwLock;
8
9#[derive(Clone, Debug)]
13pub struct ServiceCollector {
14 inner: Arc<RwLock<HashMap<String, ServiceInfo>>>,
15}
16
17impl Default for ServiceCollector {
18 fn default() -> Self {
19 Self {
20 inner: Arc::new(RwLock::new(HashMap::new())),
21 }
22 }
23}
24
25impl ServiceCollector {
26 pub fn new() -> Self {
28 Self::default()
29 }
30
31 pub async fn values(&self) -> Vec<ServiceInfo> {
33 self.inner
34 .read()
35 .await
36 .values()
37 .cloned()
38 .collect::<Vec<_>>()
39 }
40
41 pub async fn insert(&self, key: String, value: ServiceInfo) {
43 self.inner.write().await.insert(key, value);
44 }
45
46 pub async fn get(&self, key: &str) -> Option<ServiceInfo> {
48 self.inner.read().await.get(key).cloned()
49 }
50
51 pub async fn remove(&self, key: &str) -> Option<ServiceInfo> {
53 self.inner.write().await.remove(key)
54 }
55
56 pub async fn all_statuses(&self) -> Vec<ServiceStatus> {
60 let map = self.inner.read().await;
61 map.values()
62 .map(|info| ServiceStatus::from(info.clone()))
63 .collect()
64 }
65
66 pub async fn all_values(&self) -> Vec<ServiceInfo> {
68 self.values().await
69 }
70}