Mountain/IPC/Common/ServiceInfo/
ServiceRegistry.rs1
2use std::{
8 collections::HashMap,
9 time::{Duration, Instant},
10};
11
12use crate::IPC::Common::ServiceInfo::ServiceInfo;
13
14#[derive(Debug, Clone)]
15pub struct Struct {
16 pub Services:HashMap<String, ServiceInfo::Struct>,
17
18 pub LastDiscovery:Instant,
19
20 pub DiscoveryInterval:Duration,
21}
22
23impl Struct {
24 pub fn new(DiscoveryInterval:Duration) -> Self {
25 Self { Services:HashMap::new(), LastDiscovery:Instant::now(), DiscoveryInterval }
26 }
27
28 pub fn Register(&mut self, Service:ServiceInfo::Struct) {
29 self.Services.insert(Service.Name.clone(), Service);
30
31 self.LastDiscovery = Instant::now();
32 }
33
34 pub fn Unregister(&mut self, Name:&str) -> Option<ServiceInfo::Struct> {
35 self.Services.remove(Name).map(|Service| {
36 self.LastDiscovery = Instant::now();
37 Service
38 })
39 }
40
41 pub fn Get(&self, Name:&str) -> Option<&ServiceInfo::Struct> { self.Services.get(Name) }
42
43 pub fn GetMut(&mut self, Name:&str) -> Option<&mut ServiceInfo::Struct> { self.Services.get_mut(Name) }
44
45 pub fn ShouldDiscover(&self) -> bool { self.LastDiscovery.elapsed() >= self.DiscoveryInterval }
46
47 pub fn HealthyServices(&self) -> Vec<&ServiceInfo::Struct> {
48 self.Services.values().filter(|S| S.IsHealthy()).collect()
49 }
50
51 pub fn UnhealthyServices(&self) -> Vec<&ServiceInfo::Struct> {
52 self.Services.values().filter(|S| !S.IsHealthy()).collect()
53 }
54
55 pub fn MarkDiscovery(&mut self) { self.LastDiscovery = Instant::now(); }
56}
57
58impl Default for Struct {
59 fn default() -> Self { Self::new(Duration::from_secs(60)) }
60}