|
1 | 1 | //! Handles discovery of Monoliths.
|
2 | 2 |
|
| 3 | +use std::collections::HashSet; |
3 | 4 | use std::net::IpAddr;
|
4 | 5 | use std::net::SocketAddr;
|
5 | 6 | use std::time::Duration;
|
| 7 | +use tracing::{debug, error, warn}; |
6 | 8 |
|
7 | 9 | mod dns;
|
8 | 10 | mod fly;
|
@@ -82,3 +84,86 @@ pub enum DiscoveryMode {
|
82 | 84 | Polling(Duration),
|
83 | 85 | Continuous,
|
84 | 86 | }
|
| 87 | +pub struct DiscoveryTask { |
| 88 | + discovery: Box<dyn MonolithDiscoverer + Send + Sync>, |
| 89 | + |
| 90 | + monoliths: HashSet<MonolithConnectionConfig>, |
| 91 | + discovery_tx: tokio::sync::mpsc::Sender<MonolithDiscoveryMsg>, |
| 92 | +} |
| 93 | + |
| 94 | +impl DiscoveryTask { |
| 95 | + pub fn new( |
| 96 | + discovery: impl MonolithDiscoverer + Send + Sync + 'static, |
| 97 | + discovery_tx: tokio::sync::mpsc::Sender<MonolithDiscoveryMsg>, |
| 98 | + ) -> Self { |
| 99 | + Self { |
| 100 | + discovery: Box::new(discovery), |
| 101 | + monoliths: Default::default(), |
| 102 | + discovery_tx, |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + pub async fn do_continuous_discovery(&mut self) { |
| 107 | + loop { |
| 108 | + if let Err(e) = self.do_discovery().await { |
| 109 | + error!("Monolith Discovery failed: {:?}", e); |
| 110 | + tokio::time::sleep(Duration::from_secs(5)).await; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + async fn do_discovery(&mut self) -> anyhow::Result<()> { |
| 116 | + let monoliths = self.discovery.discover().await?; |
| 117 | + debug!("Discovered monoliths: {:?}", monoliths); |
| 118 | + let monoliths_new: HashSet<_> = monoliths.into_iter().collect(); |
| 119 | + let msg = build_discovery_msg(&self.monoliths, &monoliths_new); |
| 120 | + // apply the changes to our state |
| 121 | + for m in &msg.removed { |
| 122 | + self.monoliths.remove(m); |
| 123 | + } |
| 124 | + self.monoliths.extend(monoliths_new); |
| 125 | + // send the message |
| 126 | + self.discovery_tx.send(msg).await?; |
| 127 | + |
| 128 | + if self.monoliths.is_empty() { |
| 129 | + warn!("No monoliths discovered"); |
| 130 | + } |
| 131 | + |
| 132 | + if let DiscoveryMode::Polling(d) = self.discovery.mode() { |
| 133 | + tokio::time::sleep(d).await; |
| 134 | + } |
| 135 | + |
| 136 | + Ok(()) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +fn build_discovery_msg( |
| 141 | + current: &HashSet<MonolithConnectionConfig>, |
| 142 | + new: &HashSet<MonolithConnectionConfig>, |
| 143 | +) -> MonolithDiscoveryMsg { |
| 144 | + let monoliths_added = new.difference(current).cloned().collect::<Vec<_>>(); |
| 145 | + let monoliths_removed = current.difference(new).cloned().collect::<Vec<_>>(); |
| 146 | + MonolithDiscoveryMsg { |
| 147 | + added: monoliths_added, |
| 148 | + removed: monoliths_removed, |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +pub fn start_discovery_task( |
| 153 | + discovery: impl MonolithDiscoverer + Send + Sync + 'static, |
| 154 | + discovery_tx: tokio::sync::mpsc::Sender<MonolithDiscoveryMsg>, |
| 155 | +) -> JoinHandle<()> { |
| 156 | + tokio::task::Builder::new() |
| 157 | + .name("discovery") |
| 158 | + .spawn(async move { |
| 159 | + let mut task = DiscoveryTask::new(discovery, discovery_tx); |
| 160 | + task.do_continuous_discovery().await; |
| 161 | + }) |
| 162 | + .expect("failed to spawn discovery task") |
| 163 | +} |
| 164 | + |
| 165 | +#[derive(Debug, Clone)] |
| 166 | +pub struct MonolithDiscoveryMsg { |
| 167 | + pub added: Vec<MonolithConnectionConfig>, |
| 168 | + pub removed: Vec<MonolithConnectionConfig>, |
| 169 | +} |
0 commit comments