Repository navigation
Expand file tree
/
Copy pathrequest_limits.rs
More file actions
189 lines (178 loc) · 5.78 KB
/
Copy pathrequest_limits.rs
File metadata and controls
189 lines (178 loc) · 5.78 KB
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
//! Process-wide admission limits for the single-operator HTTP service.
use axum::{
extract::{Request, State},
http::{header, HeaderValue, StatusCode},
middleware::{self, Next},
response::{IntoResponse, Response},
Router,
};
use std::{
sync::{Arc, Mutex},
time::Instant,
};
use tokio::sync::Semaphore;
const BURST: f64 = 120.0;
const PER_SECOND: f64 = 20.0;
const IN_FLIGHT: usize = 32;
struct Bucket {
available: f64,
updated: Instant,
}
impl Bucket {
fn take(&mut self, now: Instant) -> bool {
self.available = (self.available
+ now.saturating_duration_since(self.updated).as_secs_f64() * PER_SECOND)
.min(BURST);
self.updated = now;
if self.available < 1.0 {
return false;
}
self.available -= 1.0;
true
}
}
#[derive(Clone)]
struct Limits {
bucket: Arc<Mutex<Bucket>>,
handlers: Arc<Semaphore>,
}
impl Limits {
fn new() -> Self {
Self {
bucket: Arc::new(Mutex::new(Bucket {
available: BURST,
updated: Instant::now(),
})),
handlers: Arc::new(Semaphore::new(IN_FLIGHT)),
}
}
}
/// Apply outside authentication so failed authentication and unknown routes count.
/// State is bounded and shared by all connections; forwarding headers are ignored.
pub fn protect(router: Router) -> Router {
router.layer(middleware::from_fn_with_state(Limits::new(), admit))
}
fn overloaded() -> Response {
let mut response = crate::error(
StatusCode::TOO_MANY_REQUESTS,
"request_limit_exceeded",
"Control-plane request capacity exceeded. Wait before retrying.",
)
.into_response();
response
.headers_mut()
.insert(header::RETRY_AFTER, HeaderValue::from_static("1"));
response
}
async fn admit(State(limits): State<Limits>, request: Request, next: Next) -> Response {
// Never hold the synchronous mutex across an await or queue excess requests.
let allowed = limits
.bucket
.lock()
.map(|mut b| b.take(Instant::now()))
.unwrap_or(false);
if !allowed {
return overloaded();
}
let Ok(_permit) = limits.handlers.try_acquire() else {
return overloaded();
};
next.run(request).await
}
#[cfg(test)]
mod tests {
use super::*;
use axum::{body::Body, routing::get};
use std::{
sync::atomic::{AtomicUsize, Ordering},
time::Duration,
};
use tower::ServiceExt;
#[test]
fn bucket_refills_and_caps_without_waiting_or_using_wall_clock() {
let start = Instant::now();
let mut bucket = Bucket {
available: BURST,
updated: start,
};
for _ in 0..120 {
assert!(bucket.take(start));
}
assert!(!bucket.take(start));
assert!(!bucket.take(start + Duration::from_millis(25)));
assert!(bucket.take(start + Duration::from_millis(50)));
assert!(!bucket.take(start + Duration::from_millis(50)));
let later = start + Duration::from_secs(60);
for _ in 0..120 {
assert!(bucket.take(later));
}
assert!(!bucket.take(later));
}
fn request() -> Request {
Request::builder()
.uri("/api/v1/health")
.body(Body::empty())
.unwrap()
}
#[tokio::test]
async fn shared_admission_rejects_before_handlers_and_releases_capacity() {
let limits = Limits::new();
let calls = Arc::new(AtomicUsize::new(0));
let observed = calls.clone();
let app = Router::new()
.route(
"/api/v1/health",
get(move || {
observed.fetch_add(1, Ordering::SeqCst);
async { StatusCode::OK }
}),
)
.layer(middleware::from_fn_with_state(limits.clone(), admit));
let permits = limits
.handlers
.acquire_many(IN_FLIGHT as u32)
.await
.unwrap();
let response = app.clone().oneshot(request()).await.unwrap();
assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
assert_eq!(response.headers()[header::RETRY_AFTER], "1");
assert_eq!(calls.load(Ordering::SeqCst), 0);
drop(permits);
assert_eq!(
app.clone().oneshot(request()).await.unwrap().status(),
StatusCode::OK
);
limits.bucket.lock().unwrap().available = 0.0;
limits.bucket.lock().unwrap().updated = Instant::now() + Duration::from_secs(10);
assert_eq!(
app.oneshot(request()).await.unwrap().status(),
StatusCode::TOO_MANY_REQUESTS
);
assert_eq!(calls.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn authentication_and_fallbacks_remain_inside_admission() {
let limits = Limits::new();
let token =
crate::auth::AccessToken::parse("fixture-only-access-token-0123456789abcdef").unwrap();
let app = crate::auth::protect(crate::router(), Some(token))
.layer(middleware::from_fn_with_state(limits.clone(), admit));
assert_eq!(
app.clone().oneshot(request()).await.unwrap().status(),
StatusCode::UNAUTHORIZED
);
limits.bucket.lock().unwrap().available = 0.0;
limits.bucket.lock().unwrap().updated = Instant::now() + Duration::from_secs(10);
let response = app
.oneshot(
Request::builder()
.uri("/unknown")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
assert!(!response.headers().contains_key("x-vmanyware-authenticated"));
}
}