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