|
| 1 | +#[cfg(test)] |
| 2 | +mod test; |
| 3 | + |
| 4 | +use std::time::Duration; |
| 5 | + |
| 6 | +use super::{ |
| 7 | + monitor::DEFAULT_HEARTBEAT_FREQUENCY, |
| 8 | + state::{Topology, TopologyState, WeakTopology}, |
| 9 | +}; |
| 10 | +use crate::{ |
| 11 | + error::{Error, Result}, |
| 12 | + options::{ClientOptions, StreamAddress}, |
| 13 | + srv::SrvResolver, |
| 14 | + RUNTIME, |
| 15 | +}; |
| 16 | + |
| 17 | +const DEFAULT_RESCAN_SRV_INTERVAL: Duration = Duration::from_secs(60); |
| 18 | + |
| 19 | +pub(crate) struct SrvPollingMonitor { |
| 20 | + initial_hostname: String, |
| 21 | + resolver: Option<SrvResolver>, |
| 22 | + topology: WeakTopology, |
| 23 | + rescan_interval: Option<Duration>, |
| 24 | + client_options: ClientOptions, |
| 25 | +} |
| 26 | + |
| 27 | +struct LookupHosts { |
| 28 | + hosts: Vec<StreamAddress>, |
| 29 | + min_ttl: Option<Duration>, |
| 30 | +} |
| 31 | + |
| 32 | +impl SrvPollingMonitor { |
| 33 | + pub(crate) fn new(topology: WeakTopology) -> Option<Self> { |
| 34 | + let client_options = topology.client_options().clone(); |
| 35 | + |
| 36 | + let initial_hostname = match client_options.original_srv_hostname() { |
| 37 | + Some(hostname) => hostname.clone(), |
| 38 | + None => return None, |
| 39 | + }; |
| 40 | + |
| 41 | + Some(Self { |
| 42 | + initial_hostname, |
| 43 | + resolver: None, |
| 44 | + topology, |
| 45 | + rescan_interval: None, |
| 46 | + client_options, |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + /// Starts a monitoring task that periodically performs SRV record lookups to determine if the |
| 51 | + /// set of mongos in the cluster have changed. A weak reference is used to ensure that the |
| 52 | + /// monitoring task doesn't keep the topology alive after the client has been dropped. |
| 53 | + pub(super) fn start(topology: WeakTopology) { |
| 54 | + RUNTIME.execute(async move { |
| 55 | + if let Some(mut monitor) = Self::new(topology) { |
| 56 | + monitor.execute().await; |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + async fn execute(&mut self) { |
| 62 | + while let Some(topology) = self.topology.upgrade() { |
| 63 | + let state = topology.clone_state().await; |
| 64 | + |
| 65 | + if state.is_sharded() || state.is_unknown() { |
| 66 | + let hosts = self.lookup_hosts().await; |
| 67 | + self.update_hosts(hosts, topology, state).await; |
| 68 | + } |
| 69 | + |
| 70 | + RUNTIME |
| 71 | + .delay_for(self.rescan_interval.unwrap_or(DEFAULT_RESCAN_SRV_INTERVAL)) |
| 72 | + .await; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + async fn update_hosts( |
| 77 | + &mut self, |
| 78 | + lookup: Result<LookupHosts>, |
| 79 | + topology: Topology, |
| 80 | + mut topology_state: TopologyState, |
| 81 | + ) { |
| 82 | + let lookup = match lookup { |
| 83 | + Ok(LookupHosts { hosts, .. }) if hosts.is_empty() => { |
| 84 | + self.no_valid_hosts(None); |
| 85 | + |
| 86 | + return; |
| 87 | + } |
| 88 | + Ok(lookup) => lookup, |
| 89 | + Err(err) => { |
| 90 | + self.no_valid_hosts(Some(err)); |
| 91 | + |
| 92 | + return; |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + self.rescan_interval = lookup.min_ttl; |
| 97 | + |
| 98 | + let diff = |
| 99 | + topology_state.update_hosts(&lookup.hosts.into_iter().collect(), &self.client_options); |
| 100 | + topology.update_state(diff, topology_state).await; |
| 101 | + } |
| 102 | + |
| 103 | + async fn lookup_hosts(&mut self) -> Result<LookupHosts> { |
| 104 | + let initial_hostname = self.initial_hostname.clone(); |
| 105 | + let resolver = self.get_or_create_srv_resolver().await?; |
| 106 | + let mut new_hosts = Vec::new(); |
| 107 | + |
| 108 | + for host in resolver.get_srv_hosts(&initial_hostname).await? { |
| 109 | + #[allow(clippy::single_match)] |
| 110 | + match host { |
| 111 | + Ok(host) => new_hosts.push(host), |
| 112 | + Err(_) => { |
| 113 | + // TODO RUST-230: Log error with host that was returned. |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + Ok(LookupHosts { |
| 119 | + hosts: new_hosts, |
| 120 | + min_ttl: resolver |
| 121 | + .min_ttl() |
| 122 | + .map(|ttl| Duration::from_secs(ttl as u64)), |
| 123 | + }) |
| 124 | + } |
| 125 | + |
| 126 | + async fn get_or_create_srv_resolver(&mut self) -> Result<&mut SrvResolver> { |
| 127 | + if let Some(ref mut resolver) = self.resolver { |
| 128 | + return Ok(resolver); |
| 129 | + } |
| 130 | + |
| 131 | + let resolver = SrvResolver::new().await?; |
| 132 | + |
| 133 | + // Since the connection was not `Some` above, this will always insert the new connection and |
| 134 | + // return a reference to it. |
| 135 | + Ok(self.resolver.get_or_insert(resolver)) |
| 136 | + } |
| 137 | + |
| 138 | + fn no_valid_hosts(&mut self, _error: Option<Error>) { |
| 139 | + // TODO RUST-230: Log error/lack of valid results. |
| 140 | + |
| 141 | + self.rescan_interval = Some(self.heartbeat_freq()); |
| 142 | + } |
| 143 | + |
| 144 | + fn heartbeat_freq(&self) -> Duration { |
| 145 | + self.client_options |
| 146 | + .heartbeat_freq |
| 147 | + .unwrap_or(DEFAULT_HEARTBEAT_FREQUENCY) |
| 148 | + } |
| 149 | +} |
0 commit comments