use super::ServiceInfo;
use actrix_proto::ServiceStatus;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
#[derive(Clone, Debug)]
pub struct ServiceCollector {
inner: Arc<RwLock<HashMap<String, ServiceInfo>>>,
}
impl Default for ServiceCollector {
fn default() -> Self {
Self {
inner: Arc::new(RwLock::new(HashMap::new())),
}
}
}
impl ServiceCollector {
pub fn new() -> Self {
Self::default()
}
pub async fn values(&self) -> Vec<ServiceInfo> {
self.inner
.read()
.await
.values()
.cloned()
.collect::<Vec<_>>()
}
pub async fn insert(&self, key: String, value: ServiceInfo) {
self.inner.write().await.insert(key, value);
}
pub async fn get(&self, key: &str) -> Option<ServiceInfo> {
self.inner.read().await.get(key).cloned()
}
pub async fn remove(&self, key: &str) -> Option<ServiceInfo> {
self.inner.write().await.remove(key)
}
pub async fn all_statuses(&self) -> Vec<ServiceStatus> {
let map = self.inner.read().await;
map.values()
.map(|info| ServiceStatus::from(info.clone()))
.collect()
}
pub async fn all_values(&self) -> Vec<ServiceInfo> {
self.values().await
}
}