use super::LoadBalancingStrategy;
use crate::client::Client;
use crate::error::FaucetResult;
use crate::worker::WorkerState;
use async_trait::async_trait;
use std::net::IpAddr;
use std::sync::atomic::AtomicUsize;
struct Targets {
targets: &'static [Client],
index: AtomicUsize,
}
const WAIT_TIME_UNTIL_RETRY: std::time::Duration = std::time::Duration::from_micros(500);
impl Targets {
fn new(workers_state: &[WorkerState]) -> FaucetResult<Self> {
let mut targets = Vec::new();
for state in workers_state {
let client = Client::builder(state.clone()).build()?;
targets.push(client);
}
let targets = Box::leak(targets.into_boxed_slice());
Ok(Targets {
targets,
index: AtomicUsize::new(0),
})
}
fn next(&self) -> Client {
let index = self.index.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
self.targets[index % self.targets.len()].clone()
}
}
pub struct RoundRobin {
targets: Targets,
}
impl RoundRobin {
pub(crate) fn new(targets: &[WorkerState]) -> FaucetResult<Self> {
Ok(Self {
targets: Targets::new(targets)?,
})
}
}
#[async_trait]
impl LoadBalancingStrategy for RoundRobin {
async fn entry(&self, _ip: IpAddr) -> Client {
let mut client = self.targets.next();
loop {
if client.is_online() {
break client;
}
tokio::time::sleep(WAIT_TIME_UNTIL_RETRY).await;
client = self.targets.next();
}
}
}