use ahash::AHashMap;
use snafu::Snafu;
use std::collections::HashMap;
use std::sync::Arc;
mod backend_circuit_state;
mod backend_stats;
mod hash_strategy;
mod peer_tracer;
mod upstream;
static LOG_TARGET: &str = "pingap::upstream";
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("Common error, category: {category}, {message}"))]
Common { message: String, category: String },
}
pub type Upstreams = AHashMap<String, Arc<Upstream>>;
pub trait UpstreamProvider: Send + Sync {
fn get(&self, name: &str) -> Option<Arc<Upstream>>;
fn list(&self) -> Vec<(String, Arc<Upstream>)>;
fn healthy_status(&self) -> HashMap<String, UpstreamHealthyStatus> {
let upstreams = self.list();
let mut healthy_status = HashMap::with_capacity(upstreams.len());
upstreams.iter().for_each(|(k, v)| {
let mut total = 0;
let mut healthy = 0;
let mut unhealthy_backends = vec![];
if let Some(backends) = v.get_backends() {
let backend_set = backends.get_backend();
total = backend_set.len();
backend_set.iter().for_each(|backend| {
if backends.ready(backend) {
healthy += 1;
} else {
unhealthy_backends.push(backend.to_string());
}
});
}
healthy_status.insert(
k.to_string(),
UpstreamHealthyStatus {
healthy,
total: total as u32,
unhealthy_backends,
},
);
});
healthy_status
}
fn get_all_stats(&self) -> HashMap<String, UpstreamStats> {
let upstreams = self.list();
let mut stats_list = HashMap::with_capacity(upstreams.len());
upstreams.iter().for_each(|(k, v)| {
stats_list.insert(k.to_string(), v.stats());
});
stats_list
}
}
pub use hash_strategy::HashStrategy;
pub use upstream::*;