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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! Upstream selectors
use super::{IntoUpstreamSelector, UpstreamSelector};
use std::cmp::Ordering::*;
use std::{
    fmt::Debug,
    ops::{Deref, DerefMut},
};
use trillium::Conn;
use trillium_server_common::{CloneCounter, CloneCounterObserver};
use url::Url;

#[derive(Debug)]
/// an upstream selector that attempts to send requests to the upstream with the fewest open connections.
///
/// if there are several with the same lowest number of connections, a random upstream is chosen from them.
pub struct ConnectionCounting<T>(Vec<(T, CloneCounterObserver)>);
impl<T> ConnectionCounting<T>
where
    T: UpstreamSelector,
{
    ///
    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(), CloneCounterObserver::new()))
                .collect(),
        )
    }
}

#[allow(dead_code)]
struct ConnectionCount(CloneCounter);

impl<T> UpstreamSelector for ConnectionCounting<T>
where
    T: UpstreamSelector,
{
    fn determine_upstream(&self, conn: &mut Conn) -> Option<Url> {
        let mut current_lowest = usize::MAX;
        let mut current_selection = vec![];
        for (u, c) in &self.0 {
            let current = c.current();
            match current.cmp(&current_lowest) {
                Less => {
                    current_lowest = current;
                    current_selection = vec![(u, c)];
                }

                Equal => {
                    current_selection.push((u, c));
                }

                Greater => {}
            }
        }

        fastrand::choice(current_selection).and_then(|(u, cc)| {
            conn.insert_state(ConnectionCount(cc.counter()));
            u.determine_upstream(conn)
        })
    }
}

impl<T> Deref for ConnectionCounting<T>
where
    T: UpstreamSelector,
{
    type Target = [(T, CloneCounterObserver)];
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl<T> DerefMut for ConnectionCounting<T>
where
    T: UpstreamSelector,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}
impl<U, T> Extend<U> for ConnectionCounting<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(), CloneCounterObserver::new())),
        );
    }
}

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