trillium_proxy/upstream/
round_robin.rs1use super::{IntoUpstreamSelector, UpstreamSelector};
3use std::{
4 fmt::Debug,
5 ops::{Deref, DerefMut},
6 sync::atomic::{AtomicU8, Ordering},
7};
8use trillium::Conn;
9use url::Url;
10
11#[derive(Debug)]
12pub struct RoundRobin<T>(Vec<T>, AtomicU8);
14impl<T> UpstreamSelector for RoundRobin<T>
15where
16 T: UpstreamSelector,
17{
18 fn determine_upstream(&self, conn: &mut Conn) -> Option<Url> {
19 let wrapping_count = self.1.fetch_add(1, Ordering::SeqCst);
20 let index = (wrapping_count as usize) % self.0.len();
21 self.0.get(index).and_then(|u| u.determine_upstream(conn))
22 }
23}
24
25impl<T> RoundRobin<T>
26where
27 T: UpstreamSelector,
28{
29 pub fn new<I, U>(urls: I) -> Self
31 where
32 I: IntoIterator<Item = U>,
33 U: IntoUpstreamSelector<UpstreamSelector = T>,
34 {
35 Self(
36 urls.into_iter().map(|u| u.into_upstream()).collect(),
37 0.into(),
38 )
39 }
40}
41
42impl<T> Deref for RoundRobin<T>
43where
44 T: UpstreamSelector,
45{
46 type Target = [T];
47 fn deref(&self) -> &Self::Target {
48 &self.0
49 }
50}
51impl<T> DerefMut for RoundRobin<T>
52where
53 T: UpstreamSelector,
54{
55 fn deref_mut(&mut self) -> &mut Self::Target {
56 &mut self.0
57 }
58}
59impl<U, T> Extend<U> for RoundRobin<T>
60where
61 T: UpstreamSelector,
62 U: IntoUpstreamSelector<UpstreamSelector = T>,
63{
64 fn extend<I: IntoIterator<Item = U>>(&mut self, iter: I) {
65 self.0.extend(iter.into_iter().map(|i| i.into_upstream()));
66 }
67}
68
69impl<U, V> FromIterator<U> for RoundRobin<V>
70where
71 U: IntoUpstreamSelector<UpstreamSelector = V>,
72 V: UpstreamSelector,
73{
74 fn from_iter<T>(urls: T) -> Self
75 where
76 T: IntoIterator<Item = U>,
77 {
78 Self::new(urls)
79 }
80}