Skip to main content

middleware_core/discovery/static_io/
dump.rs

1use super::super::SimpleDiscovery;
2
3impl SimpleDiscovery {
4    /// Dump current discovery state using static-discovery directives.
5    pub fn dump_static_payload(&self) -> String {
6        let now = std::time::Instant::now();
7        let mut lines = Vec::new();
8
9        let mut topics: Vec<&String> = self
10            .topics
11            .iter()
12            .filter(|topic| self.is_topic_active(topic, now))
13            .collect();
14        topics.sort();
15        for topic in topics {
16            if let Some(raw_labels) = self.labels.get(topic) {
17                let mut labels = raw_labels.clone();
18                labels.sort();
19                if labels.is_empty() {
20                    lines.push(format!("topic {}", topic));
21                } else {
22                    lines.push(format!("topic {} {}", topic, labels.join(",")));
23                }
24            } else {
25                lines.push(format!("topic {}", topic));
26            }
27        }
28
29        let mut services: Vec<&String> = self
30            .services
31            .iter()
32            .filter(|service| self.is_service_active(service, now))
33            .collect();
34        services.sort();
35        for service in services {
36            if let Some(raw_labels) = self.labels.get(service) {
37                let mut labels = raw_labels.clone();
38                labels.sort();
39                if labels.is_empty() {
40                    lines.push(format!("service {}", service));
41                } else {
42                    lines.push(format!("service {} {}", service, labels.join(",")));
43                }
44            } else {
45                lines.push(format!("service {}", service));
46            }
47        }
48
49        let mut missions: Vec<&String> = self
50            .missions
51            .iter()
52            .filter(|mission| self.is_mission_active(mission, now))
53            .collect();
54        missions.sort();
55        for mission in missions {
56            if let Some(raw_labels) = self.labels.get(mission) {
57                let mut labels = raw_labels.clone();
58                labels.sort();
59                if labels.is_empty() {
60                    lines.push(format!("mission {}", mission));
61                } else {
62                    lines.push(format!("mission {} {}", mission, labels.join(",")));
63                }
64            } else {
65                lines.push(format!("mission {}", mission));
66            }
67        }
68
69        let mut endpoint_names: Vec<&String> = self
70            .endpoints
71            .keys()
72            .filter(|name| self.is_endpoint_active(name, now))
73            .collect();
74        endpoint_names.sort();
75        for name in endpoint_names {
76            let endpoint = self
77                .endpoints
78                .get(name)
79                .expect("endpoint key must exist during dump");
80            let mut labels = endpoint.labels.clone();
81            labels.sort();
82            if labels.is_empty() {
83                lines.push(format!("endpoint {} {}", name, endpoint.address));
84            } else {
85                lines.push(format!(
86                    "endpoint {} {} {}",
87                    name,
88                    endpoint.address,
89                    labels.join(",")
90                ));
91            }
92        }
93
94        lines.join("\n")
95    }
96}