use crate::LoadBalancer;
use rand::seq::IteratorRandom;
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tokio::task::JoinHandle;
use tokio::{spawn, time::sleep};
#[derive(Clone)]
pub struct Entry<T>
where
T: Send + Sync + Clone + 'static,
{
pub value: T,
}
pub struct Inner<T>
where
T: Send + Sync + Clone + 'static,
{
pub entries: RwLock<Vec<Entry<T>>>,
}
#[derive(Clone)]
pub struct Random<T>
where
T: Send + Sync + Clone + 'static,
{
inner: Arc<Inner<T>>,
}
impl<T> Random<T>
where
T: Send + Sync + Clone + 'static,
{
pub fn new(entries: Vec<T>) -> Self {
Self {
inner: Arc::new(Inner {
entries: RwLock::new(
entries.into_iter().map(|value| Entry { value }).collect(),
),
}),
}
}
pub async fn update<F, R, N>(&self, handler: F) -> anyhow::Result<N>
where
F: Fn(Arc<Inner<T>>) -> R,
R: Future<Output = anyhow::Result<N>>,
{
handler(self.inner.clone()).await
}
pub async fn update_timer<F, R>(
&self,
handler: F,
interval: Duration,
) -> anyhow::Result<JoinHandle<()>>
where
F: Fn(Arc<Inner<T>>) -> R + Send + Sync + 'static,
R: Future<Output = anyhow::Result<()>> + Send,
{
handler(self.inner.clone()).await?;
let inner = self.inner.clone();
Ok(spawn(async move {
loop {
sleep(interval).await;
let _ = handler(inner.clone()).await;
}
}))
}
}
impl<T> LoadBalancer<T> for Random<T>
where
T: Send + Sync + Clone + 'static,
{
async fn alloc(&self) -> T {
self.inner
.entries
.read()
.await
.iter()
.choose(&mut rand::rng())
.map(|v| v.value.clone())
.unwrap()
}
fn try_alloc(&self) -> Option<T> {
self.inner
.entries
.try_read()
.ok()?
.iter()
.choose(&mut rand::rng())
.map(|v| v.value.clone())
}
}