use crate::component::Endpoint;
use crate::net::address::Address;
use async_broadcast::Receiver;
use faststr::FastStr;
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::Debug;
use std::future::Future;
use std::sync::Arc;
mod dummy;
mod fixed;
use super::ClientError;
use core::marker::Send;
pub use dummy::DummyDiscover;
pub use fixed::FixedDiscover;
pub trait Discover: Send + Sync + 'static {
fn discover<'s>(&'s self, endpoint: &'s Endpoint) -> impl Future<Output = Result<Discovery, ClientError>> + Send;
fn watch(&self, keys: Option<&[FastStr]>) -> Option<Receiver<Discovery>>;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Instance {
pub address: Address,
pub weight: u32,
pub tags: HashMap<Cow<'static, str>, Cow<'static, str>>,
}
#[derive(Debug, Clone)]
pub struct Discovery {
pub key: FastStr,
pub instance_cluster: InstanceCluster,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InstanceCluster {
Lpc,
Rpc(Vec<Arc<Instance>>),
}
#[cfg(test)]
mod tests {
use super::{FixedDiscover, Instance};
use crate::client::discover::{Discover, InstanceCluster};
use crate::component::Endpoint;
use crate::net::Address;
use std::sync::Arc;
#[test]
fn test_fixed_discover() {
let discover = FixedDiscover::from_address_str(vec!["127.0.0.1:8000", "127.0.0.2:9000"]).unwrap();
let resp = futures::executor::block_on(async { discover.discover(&Endpoint::default()).await }).unwrap();
let expected = InstanceCluster::Rpc(vec![
Arc::new(Instance {
address: Address::Ip("127.0.0.1:8000".parse().unwrap()),
weight: 1,
tags: Default::default(),
}),
Arc::new(Instance {
address: Address::Ip("127.0.0.2:9000".parse().unwrap()),
weight: 1,
tags: Default::default(),
}),
]);
assert_eq!(resp.instance_cluster, expected);
}
}