use async_trait::async_trait;
use rand::prelude::IndexedRandom as _;
use crate::error::{ProxyError, ProxyResult};
use crate::strategy::{ProxyCandidate, RotationStrategy, healthy_candidates};
#[derive(Debug, Default, Clone, Copy)]
pub struct RandomStrategy;
#[async_trait]
impl RotationStrategy for RandomStrategy {
async fn select<'a>(
&self,
candidates: &'a [ProxyCandidate],
) -> ProxyResult<&'a ProxyCandidate> {
let healthy = healthy_candidates(candidates);
healthy
.choose(&mut rand::rng())
.copied()
.ok_or(ProxyError::AllProxiesUnhealthy)
}
}