Files
el/ui/vessels/el-services/src/registry.rs
T

60 lines
1.6 KiB
Rust

//! Service registry — the global lookup table for service proxies.
use crate::{proxy::ServiceProxy, ServiceError, ServiceResult};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
/// Global registry of all service proxies.
///
/// The framework maintains one registry per application. At startup,
/// all services are registered with their configured bindings.
/// Components call `registry.get("UserService")` to get the proxy.
#[derive(Default)]
pub struct ServiceRegistry {
services: RwLock<HashMap<String, Arc<ServiceProxy>>>,
}
impl ServiceRegistry {
pub fn new() -> Self {
Self::default()
}
/// Register a service proxy.
pub fn register(&self, proxy: ServiceProxy) {
let name = proxy.service_name.clone();
self.services
.write()
.expect("lock poisoned")
.insert(name, Arc::new(proxy));
}
/// Get a service proxy by name.
pub fn get(&self, name: &str) -> ServiceResult<Arc<ServiceProxy>> {
self.services
.read()
.expect("lock poisoned")
.get(name)
.cloned()
.ok_or_else(|| ServiceError::NotFound(name.to_string()))
}
/// List all registered service names.
pub fn service_names(&self) -> Vec<String> {
self.services
.read()
.expect("lock poisoned")
.keys()
.cloned()
.collect()
}
/// Number of registered services.
pub fn len(&self) -> usize {
self.services.read().expect("lock poisoned").len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}