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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright (c) 2022 Espresso Systems (espressosys.com)
// This file is part of the tide-disco library.

// You should have received a copy of the MIT License
// along with the tide-disco library. If not, see <https://mit-license.org/>.

use crate::StatusCode;
use async_lock::Semaphore;
use async_std::{
    net::TcpListener,
    sync::Arc,
    task::{sleep, spawn},
};
use async_trait::async_trait;
use derivative::Derivative;
use futures::stream::StreamExt;
use std::{
    fmt::{self, Display, Formatter},
    io::{self, ErrorKind},
    net::SocketAddr,
    time::Duration,
};
use tide::{
    http,
    listener::{ListenInfo, Listener, ToListener},
    Server,
};

/// TCP listener which accepts only a limited number of connections at a time.
///
/// This listener is based on `tide::listener::TcpListener` and should match the semantics of that
/// listener in every way, accept that when there are more simultaneous outstanding requests than
/// the configured limit, excess requests will fail immediately with error code 429 (Too Many
/// Requests).
#[derive(Derivative)]
#[derivative(Debug(bound = "State: Send + Sync + 'static"))]
pub struct RateLimitListener<State> {
    addr: SocketAddr,
    listener: Option<TcpListener>,
    server: Option<Server<State>>,
    info: Option<ListenInfo>,
    permit: Arc<Semaphore>,
}

impl<State> RateLimitListener<State> {
    /// Listen at the given address.
    pub fn new(addr: SocketAddr, limit: usize) -> Self {
        Self {
            addr,
            listener: None,
            server: None,
            info: None,
            permit: Arc::new(Semaphore::new(limit)),
        }
    }

    /// Listen at the given port on all interfaces.
    pub fn with_port(port: u16, limit: usize) -> Self {
        Self::new(([0, 0, 0, 0], port).into(), limit)
    }
}

#[async_trait]
impl<State> Listener<State> for RateLimitListener<State>
where
    State: Clone + Send + Sync + 'static,
{
    async fn bind(&mut self, app: Server<State>) -> io::Result<()> {
        if self.server.is_some() {
            return Err(io::Error::new(
                ErrorKind::AlreadyExists,
                "`bind` should only be called once",
            ));
        }
        self.server = Some(app);
        self.listener = Some(TcpListener::bind(&[self.addr][..]).await?);

        // Format the listen information.
        let conn_string = format!("{}", self);
        let transport = "tcp".to_owned();
        let tls = false;
        self.info = Some(ListenInfo::new(conn_string, transport, tls));

        Ok(())
    }

    async fn accept(&mut self) -> io::Result<()> {
        let server = self.server.take().ok_or_else(|| {
            io::Error::other("`Listener::bind` must be called before `Listener::accept`")
        })?;
        let listener = self.listener.take().ok_or_else(|| {
            io::Error::other("`Listener::bind` must be called before `Listener::accept`")
        })?;

        let mut incoming = listener.incoming();
        while let Some(stream) = incoming.next().await {
            match stream {
                Err(err) if is_transient_error(&err) => continue,
                Err(err) => {
                    tracing::warn!(%err, "TCP error");
                    sleep(Duration::from_millis(500)).await;
                    continue;
                }
                Ok(stream) => {
                    let app = server.clone();
                    let permit = self.permit.clone();
                    spawn(async move {
                        let local_addr = stream.local_addr().ok();
                        let peer_addr = stream.peer_addr().ok();

                        let fut = async_h1::accept(stream, |mut req| async {
                            // Handle the request if we can get a permit.
                            if let Some(_guard) = permit.try_acquire() {
                                req.set_local_addr(local_addr);
                                req.set_peer_addr(peer_addr);
                                app.respond(req).await
                            } else {
                                // Otherwise, we are rate limited. Respond immediately with an
                                // error.
                                Ok(http::Response::new(StatusCode::TOO_MANY_REQUESTS))
                            }
                        });

                        if let Err(error) = fut.await {
                            tracing::error!(%error, "HTTP error");
                        }
                    });
                }
            };
        }
        Ok(())
    }

    fn info(&self) -> Vec<ListenInfo> {
        match &self.info {
            Some(info) => vec![info.clone()],
            None => vec![],
        }
    }
}

impl<State> ToListener<State> for RateLimitListener<State>
where
    State: Clone + Send + Sync + 'static,
{
    type Listener = Self;

    fn to_listener(self) -> io::Result<Self::Listener> {
        Ok(self)
    }
}

impl<State> Display for RateLimitListener<State> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match &self.listener {
            Some(listener) => {
                let addr = listener.local_addr().expect("Could not get local addr");
                write!(f, "http://{}", addr)
            }
            None => write!(f, "http://{}", self.addr),
        }
    }
}

fn is_transient_error(e: &io::Error) -> bool {
    matches!(
        e.kind(),
        ErrorKind::ConnectionRefused | ErrorKind::ConnectionAborted | ErrorKind::ConnectionReset
    )
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::{
        error::ServerError,
        testing::{setup_test, Client},
        App,
    };
    use futures::future::{try_join_all, FutureExt};
    use portpicker::pick_unused_port;
    use toml::toml;
    use vbs::version::{StaticVersion, StaticVersionType};

    type StaticVer01 = StaticVersion<0, 1>;

    #[async_std::test]
    async fn test_rate_limiting() {
        setup_test();

        let mut app = App::<_, ServerError>::with_state(());
        let api_toml = toml! {
            [route.test]
            PATH = ["/test"]
            METHOD = "GET"
        };
        {
            let mut api = app
                .module::<ServerError, StaticVer01>("mod", api_toml)
                .unwrap();
            api.get("test", |_req, _state| {
                async move {
                    // Make a really slow endpoint so we can have many simultaneous requests.
                    sleep(Duration::from_secs(30)).await;
                    Ok(())
                }
                .boxed()
            })
            .unwrap();
        }

        let limit = 10;
        let port = pick_unused_port().unwrap();
        spawn(app.serve(
            RateLimitListener::with_port(port, limit),
            StaticVer01::instance(),
        ));
        let client = Client::new(format!("http://localhost:{port}").parse().unwrap()).await;

        // Start the maximum number of simultaneous requests.
        let reqs = (0..limit)
            .map(|_| spawn(client.get("mod/test").send()))
            .collect::<Vec<_>>();

        // Wait a bit for those requests to get accepted.
        sleep(Duration::from_secs(5)).await;

        // The next request gets rate limited.
        let res = client.get("mod/test").send().await.unwrap();
        assert_eq!(StatusCode::TOO_MANY_REQUESTS, res.status());

        // The other requests eventually complete successfully.
        for res in try_join_all(reqs).await.unwrap() {
            assert_eq!(StatusCode::OK, res.status());
        }
    }
}