1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! Upstream selectors
use super::{IntoUpstreamSelector, UpstreamSelector};
use std::{
    fmt::Debug,
    ops::{Deref, DerefMut},
    sync::atomic::{AtomicU8, Ordering},
};
use trillium::Conn;
use url::Url;

#[derive(Debug)]
/// an upstream selector that dispatches to upstreams in the order that they're specified, wrapping
pub struct RoundRobin<T>(Vec<T>, AtomicU8);
impl<T> UpstreamSelector for RoundRobin<T>
where
    T: UpstreamSelector,
{
    fn determine_upstream(&self, conn: &mut Conn) -> Option<Url> {
        let wrapping_count = self.1.fetch_add(1, Ordering::SeqCst);
        let index = (wrapping_count as usize) % self.0.len();
        self.0.get(index).and_then(|u| u.determine_upstream(conn))
    }
}

impl<T> RoundRobin<T>
where
    T: UpstreamSelector,
{
    /// construct a new round robin
    pub fn new<I, U>(urls: I) -> Self
    where
        I: IntoIterator<Item = U>,
        U: IntoUpstreamSelector<UpstreamSelector = T>,
    {
        Self(
            urls.into_iter().map(|u| u.into_upstream()).collect(),
            0.into(),
        )
    }
}

impl<T> Deref for RoundRobin<T>
where
    T: UpstreamSelector,
{
    type Target = [T];
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl<T> DerefMut for RoundRobin<T>
where
    T: UpstreamSelector,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}
impl<U, T> Extend<U> for RoundRobin<T>
where
    T: UpstreamSelector,
    U: IntoUpstreamSelector<UpstreamSelector = T>,
{
    fn extend<I: IntoIterator<Item = U>>(&mut self, iter: I) {
        self.0.extend(iter.into_iter().map(|i| i.into_upstream()));
    }
}

impl<U, V> FromIterator<U> for RoundRobin<V>
where
    U: IntoUpstreamSelector<UpstreamSelector = V>,
    V: UpstreamSelector,
{
    fn from_iter<T>(urls: T) -> Self
    where
        T: IntoIterator<Item = U>,
    {
        Self::new(urls)
    }
}