use crate::transport::BoxFuture;
use crate::types::{DiscoveredKB, DiscoveryDocument};
use futures_core::Stream;
use serde_json::Value;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;
use tokio::sync::mpsc;
use tokio::task::AbortHandle;
const VERSION: i64 = 1;
pub const DISCOVERY_INTERVAL: Duration = Duration::from_secs(5);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiscoveryAbsentReason {
NotFound,
NotJson,
Invalid,
UnsupportedVersion,
Unreadable,
}
#[derive(Debug, Clone, PartialEq)]
pub enum DiscoveryState {
Absent {
reason: DiscoveryAbsentReason,
diagnostic: Option<String>,
},
Managed {
kbs: Vec<DiscoveredKB>,
},
}
impl DiscoveryState {
fn absent(reason: DiscoveryAbsentReason, diagnostic: impl Into<String>) -> DiscoveryState {
DiscoveryState::Absent {
reason,
diagnostic: Some(diagnostic.into()),
}
}
fn kbs(&self) -> &[DiscoveredKB] {
match self {
DiscoveryState::Managed { kbs } => kbs,
DiscoveryState::Absent { .. } => &[],
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum DiscoveryRead {
State(DiscoveryState),
Unchanged,
}
pub trait DiscoveryTransport: Send + Sync + 'static {
fn read(&self) -> BoxFuture<'_, DiscoveryRead>;
}
pub fn parse_discovery_document(text: &str) -> DiscoveryState {
use DiscoveryAbsentReason::{Invalid, NotJson, UnsupportedVersion};
let parsed: Value = match serde_json::from_str(text) {
Ok(parsed) => parsed,
Err(error) => return DiscoveryState::absent(NotJson, error.to_string()),
};
if !parsed.is_object() {
return DiscoveryState::absent(Invalid, "document is not an object");
}
match parsed["version"].as_i64() {
None => return DiscoveryState::absent(Invalid, "missing numeric \"version\""),
Some(VERSION) => {}
Some(other) => {
return DiscoveryState::absent(
UnsupportedVersion,
format!(
"discovery document version {other} is not the one this client speaks ({VERSION}); it is not read in part"
),
);
}
}
let document: DiscoveryDocument = match serde_json::from_value(parsed) {
Ok(document) => document,
Err(error) => return DiscoveryState::absent(Invalid, error.to_string()),
};
if let Some(kb) = document
.kbs
.iter()
.find(|kb| !(1..=65535).contains(&kb.port))
{
return DiscoveryState::absent(
Invalid,
format!(
"kb entry at {} has port {}, which is not a port",
kb.host, kb.port
),
);
}
DiscoveryState::Managed { kbs: document.kbs }
}
struct TextDiscovery<R> {
read: R,
}
impl<R, Fut> DiscoveryTransport for TextDiscovery<R>
where
R: Fn() -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<Option<String>, String>> + Send + 'static,
{
fn read(&self) -> BoxFuture<'_, DiscoveryRead> {
let reading = (self.read)();
Box::pin(async move {
DiscoveryRead::State(match reading.await {
Err(why) => DiscoveryState::absent(DiscoveryAbsentReason::Unreadable, why),
Ok(None) => DiscoveryState::Absent {
reason: DiscoveryAbsentReason::NotFound,
diagnostic: None,
},
Ok(Some(text)) => parse_discovery_document(&text),
})
})
}
}
pub fn text_discovery<R, Fut>(read: R) -> impl DiscoveryTransport
where
R: Fn() -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<Option<String>, String>> + Send + 'static,
{
TextDiscovery { read }
}
#[derive(Debug, Clone, PartialEq)]
pub struct DiscoveryDiff {
pub state: DiscoveryState,
pub added: Vec<DiscoveredKB>,
pub updated: Vec<DiscoveredKB>,
pub removed: Vec<DiscoveredKB>,
pub duplicated: Vec<String>,
}
fn address(kb: &DiscoveredKB) -> String {
format!("{}:{}", kb.host, kb.port)
}
fn diff(last: Option<&DiscoveryState>, next: DiscoveryState) -> Option<DiscoveryDiff> {
let before_kbs = last.map(DiscoveryState::kbs).unwrap_or_default();
let before: HashMap<String, &DiscoveredKB> =
before_kbs.iter().map(|kb| (address(kb), kb)).collect();
let after: HashMap<String, &DiscoveredKB> =
next.kbs().iter().map(|kb| (address(kb), kb)).collect();
let mut duplicated: Vec<String> = Vec::new();
let mut seen: Vec<String> = Vec::new();
for kb in next.kbs() {
let at = address(kb);
if seen.contains(&at) && !duplicated.contains(&at) {
duplicated.push(at.clone());
}
seen.push(at);
}
let added: Vec<DiscoveredKB> = next
.kbs()
.iter()
.filter(|kb| !before.contains_key(&address(kb)))
.cloned()
.collect();
let removed: Vec<DiscoveredKB> = before_kbs
.iter()
.filter(|kb| !after.contains_key(&address(kb)))
.cloned()
.collect();
let updated: Vec<DiscoveredKB> = next
.kbs()
.iter()
.filter(|kb| before.get(&address(kb)).is_some_and(|was| was != kb))
.cloned()
.collect();
let state_changed = match (last, &next) {
(None, _) => true,
(Some(DiscoveryState::Managed { .. }), DiscoveryState::Managed { .. }) => false,
(
Some(DiscoveryState::Absent { reason: was, .. }),
DiscoveryState::Absent { reason: is, .. },
) => was != is,
_ => true,
};
(state_changed || !added.is_empty() || !updated.is_empty() || !removed.is_empty()).then_some(
DiscoveryDiff {
state: next,
added,
updated,
removed,
duplicated,
},
)
}
pub struct DiscoveryDiffs {
diffs: mpsc::UnboundedReceiver<DiscoveryDiff>,
reading: AbortHandle,
}
impl DiscoveryDiffs {
pub async fn next(&mut self) -> Option<DiscoveryDiff> {
self.diffs.recv().await
}
}
impl Stream for DiscoveryDiffs {
type Item = DiscoveryDiff;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.diffs.poll_recv(cx)
}
}
impl Drop for DiscoveryDiffs {
fn drop(&mut self) {
self.reading.abort();
}
}
pub fn subscribe_discovery(
transport: Arc<dyn DiscoveryTransport>,
interval: Duration,
) -> DiscoveryDiffs {
let (tell, diffs) = mpsc::unbounded_channel();
let reading = tokio::spawn(async move {
let mut last: Option<DiscoveryState> = None;
loop {
if let DiscoveryRead::State(state) = transport.read().await
&& let Some(change) = diff(last.as_ref(), state)
{
last = Some(change.state.clone());
if tell.send(change).is_err() {
return;
}
}
tokio::time::sleep(interval).await;
}
});
DiscoveryDiffs {
diffs,
reading: reading.abort_handle(),
}
}