Skip to main content

middleware_core/discovery/
registration.rs

1use std::time::Duration;
2
3use transport_core::Endpoint;
4
5use super::SimpleDiscovery;
6use super::types::DiscoveryState;
7
8impl SimpleDiscovery {
9    pub fn register_topic(&mut self, name: impl Into<String>) {
10        let name = name.into();
11        self.topics.insert(name.clone());
12        self.topic_state.insert(name, DiscoveryState::default());
13    }
14
15    pub fn register_service(&mut self, name: impl Into<String>) {
16        let name = name.into();
17        self.services.insert(name.clone());
18        self.service_state.insert(name, DiscoveryState::default());
19    }
20
21    pub fn register_mission(&mut self, name: impl Into<String>) {
22        let name = name.into();
23        self.missions.insert(name.clone());
24        self.mission_state.insert(name, DiscoveryState::default());
25    }
26
27    pub fn register_topic_with_ttl(&mut self, name: impl Into<String>, ttl: Duration) {
28        let name = name.into();
29        self.topics.insert(name.clone());
30        self.topic_state.insert(name, DiscoveryState::with_ttl(ttl));
31    }
32
33    pub fn register_service_with_ttl(&mut self, name: impl Into<String>, ttl: Duration) {
34        let name = name.into();
35        self.services.insert(name.clone());
36        self.service_state
37            .insert(name, DiscoveryState::with_ttl(ttl));
38    }
39
40    pub fn register_mission_with_ttl(&mut self, name: impl Into<String>, ttl: Duration) {
41        let name = name.into();
42        self.missions.insert(name.clone());
43        self.mission_state
44            .insert(name, DiscoveryState::with_ttl(ttl));
45    }
46
47    pub fn add_labels(&mut self, key: impl Into<String>, labels: Vec<String>) {
48        self.labels.insert(key.into(), labels);
49    }
50
51    pub fn register_endpoint(&mut self, name: impl Into<String>, endpoint: Endpoint) {
52        let name = name.into();
53        self.endpoints.insert(name.clone(), endpoint);
54        self.endpoint_state.insert(name, DiscoveryState::default());
55    }
56
57    pub fn register_endpoint_with_ttl(
58        &mut self,
59        name: impl Into<String>,
60        endpoint: Endpoint,
61        ttl: Duration,
62    ) {
63        let name = name.into();
64        self.endpoints.insert(name.clone(), endpoint);
65        self.endpoint_state
66            .insert(name, DiscoveryState::with_ttl(ttl));
67    }
68
69    pub fn update_endpoint_labels(&mut self, name: &str, labels: Vec<String>) -> bool {
70        let Some(endpoint) = self.endpoints.get_mut(name) else {
71            return false;
72        };
73        endpoint.labels = labels;
74        true
75    }
76
77    pub fn unregister_endpoint(&mut self, name: &str) -> bool {
78        let removed_endpoint = self.endpoints.remove(name).is_some();
79        let removed_state = self.endpoint_state.remove(name).is_some();
80        if removed_endpoint || removed_state {
81            self.remove_labels_if_unused(name);
82        }
83        removed_endpoint || removed_state
84    }
85}