-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathregistar.rs
More file actions
101 lines (83 loc) · 3.17 KB
/
registar.rs
File metadata and controls
101 lines (83 loc) · 3.17 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//! Registar. Registers Services for Health Check.
use std::collections::HashMap;
use std::sync::Arc;
use derive_more::Constructor;
use tokio::sync::Mutex;
use tokio::task::JoinHandle;
use torrust_tracker_primitives::service_binding::ServiceBinding;
/// A [`ServiceHeathCheckResult`] is returned by a completed health check.
pub type ServiceHeathCheckResult = Result<String, String>;
/// The [`ServiceHealthCheckJob`] has a health check job with it's metadata
///
/// The `job` awaits a [`ServiceHeathCheckResult`].
#[derive(Debug, Constructor)]
pub struct ServiceHealthCheckJob {
pub service_binding: ServiceBinding,
pub info: String,
pub service_type: String,
pub job: JoinHandle<ServiceHeathCheckResult>,
}
/// The function specification [`FnSpawnServiceHeathCheck`].
///
/// A function fulfilling this specification will spawn a new [`ServiceHealthCheckJob`].
pub type FnSpawnServiceHeathCheck = fn(&ServiceBinding) -> ServiceHealthCheckJob;
/// A [`ServiceRegistration`] is provided to the [`Registar`] for registration.
///
/// Each registration includes a function that fulfils the [`FnSpawnServiceHeathCheck`] specification.
#[derive(Clone, Debug, Constructor)]
pub struct ServiceRegistration {
service_binding: ServiceBinding,
check_fn: FnSpawnServiceHeathCheck,
}
impl ServiceRegistration {
#[must_use]
pub fn spawn_check(&self) -> ServiceHealthCheckJob {
(self.check_fn)(&self.service_binding)
}
}
/// A [`ServiceRegistrationForm`] will return a completed [`ServiceRegistration`] to the [`Registar`].
pub type ServiceRegistrationForm = tokio::sync::oneshot::Sender<ServiceRegistration>;
/// The [`ServiceRegistry`] contains each unique [`ServiceRegistration`] by it's [`SocketAddr`].
pub type ServiceRegistry = Arc<Mutex<HashMap<ServiceBinding, ServiceRegistration>>>;
/// The [`Registar`] manages the [`ServiceRegistry`].
#[derive(Clone, Debug)]
pub struct Registar {
registry: ServiceRegistry,
}
#[allow(clippy::derivable_impls)]
impl Default for Registar {
fn default() -> Self {
Self {
registry: ServiceRegistry::default(),
}
}
}
impl Registar {
pub fn new(register: ServiceRegistry) -> Self {
Self { registry: register }
}
/// Registers a Service
#[must_use]
pub fn give_form(&self) -> ServiceRegistrationForm {
let (tx, rx) = tokio::sync::oneshot::channel::<ServiceRegistration>();
let register = self.clone();
tokio::spawn(async move {
register.insert(rx).await;
});
tx
}
/// Inserts a listing into the registry.
async fn insert(&self, rx: tokio::sync::oneshot::Receiver<ServiceRegistration>) {
tracing::debug!("Waiting for the started service to send registration data ...");
let service_registration = rx
.await
.expect("it should receive the service registration from the started service");
let mut mutex = self.registry.lock().await;
mutex.insert(service_registration.service_binding.clone(), service_registration);
}
/// Returns the [`ServiceRegistry`] of services
#[must_use]
pub fn entries(&self) -> ServiceRegistry {
self.registry.clone()
}
}