-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathruntime_service_metadata.rs
More file actions
83 lines (70 loc) · 2.87 KB
/
Copy pathruntime_service_metadata.rs
File metadata and controls
83 lines (70 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use url::Url;
use crate::{ConfigurationInstanceId, ServiceRole};
/// Immutable listener-specific metadata attached to a started service registration.
///
/// It combines the identity of the source configuration entry with configured
/// observability data that describes the same listener. The registry stores
/// this tracker-owned value without assigning it application semantics.
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Clone)]
pub struct RuntimeServiceMetadata {
/// Identifies the source configuration entry for this listener.
configuration_instance_id: ConfigurationInstanceId,
/// Configured, operator-declared external endpoint for this listener.
///
/// This does not identify the local bind address or its post-bind service
/// binding.
public_url: Option<Url>,
}
impl RuntimeServiceMetadata {
/// Creates metadata for a canonical tracker service instance.
#[must_use]
pub const fn new(configuration_instance_id: ConfigurationInstanceId) -> Self {
Self {
configuration_instance_id,
public_url: None,
}
}
/// Adds the configured public URL for the listener.
#[must_use]
pub fn with_public_url(mut self, public_url: Option<Url>) -> Self {
self.public_url = public_url;
self
}
/// Returns the role implemented by the started listener.
#[must_use]
pub const fn service_role(&self) -> ServiceRole {
self.configuration_instance_id.service_role()
}
/// Returns the source configuration instance for the listener.
#[must_use]
pub const fn configuration_instance_id(&self) -> ConfigurationInstanceId {
self.configuration_instance_id
}
/// Returns the configured public URL for the listener, when present.
#[must_use]
pub fn public_url(&self) -> Option<&Url> {
self.public_url.as_ref()
}
}
#[cfg(test)]
mod tests {
use url::Url;
use crate::{ConfigurationInstanceId, RuntimeServiceMetadata, ServiceRole};
#[test]
fn it_should_derive_the_role_from_the_configuration_instance_identity() {
let configuration_instance_id = ConfigurationInstanceId::new(ServiceRole::UdpTracker, 1);
let metadata = RuntimeServiceMetadata::new(configuration_instance_id);
assert_eq!(metadata.service_role(), ServiceRole::UdpTracker);
assert_eq!(metadata.configuration_instance_id(), configuration_instance_id);
assert_eq!(metadata.public_url(), None);
}
#[test]
fn it_should_store_an_optional_configured_public_url() {
let metadata = RuntimeServiceMetadata::new(ConfigurationInstanceId::new(ServiceRole::HttpTracker, 0))
.with_public_url(Some(Url::parse("https://tracker.example.test/announce").unwrap()));
assert_eq!(
metadata.public_url().map(Url::as_str),
Some("https://tracker.example.test/announce")
);
}
}