Skip to content

Commit 7be19f4

Browse files
committed
Add a report-only mode to the BalanceCapacity middleware
This adds a single environment variable that can be quickly set or removed to toggle capacity enforcement. If this variable is set to any value (including the empty string) then no requests will be rejected due to load. This will allow us to evolve the heuristics used in this middleware to find the right balance before turning on enforcement. This also increases the default logging percentage to a higher default.
1 parent 47e65bb commit 7be19f4

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/middleware/balance_capacity.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//! we should avoid dropping download requests even if that means rejecting some legitimate
99
//! requests to other endpoints.
1010
11+
use std::env;
1112
use std::sync::atomic::{AtomicUsize, Ordering};
1213

1314
use super::prelude::*;
@@ -18,6 +19,7 @@ pub(super) struct BalanceCapacity {
1819
handler: Option<Box<dyn Handler>>,
1920
capacity: usize,
2021
in_flight_requests: AtomicUsize,
22+
report_only: bool,
2123
log_at_percentage: usize,
2224
throttle_at_percentage: usize,
2325
dl_only_at_percentage: usize,
@@ -29,7 +31,8 @@ impl BalanceCapacity {
2931
handler: None,
3032
capacity,
3133
in_flight_requests: AtomicUsize::new(0),
32-
log_at_percentage: read_env_percentage("WEB_CAPACITY_LOG_PCT", 20),
34+
report_only: env::var("WEB_CAPACITY_REPORT_ONLY").ok().is_some(),
35+
log_at_percentage: read_env_percentage("WEB_CAPACITY_LOG_PCT", 50),
3336
throttle_at_percentage: read_env_percentage("WEB_CAPACITY_THROTTLE_PCT", 70),
3437
dl_only_at_percentage: read_env_percentage("WEB_CAPACITY_DL_ONLY_PCT", 80),
3538
}
@@ -54,6 +57,11 @@ impl Handler for BalanceCapacity {
5457
super::log_request::add_custom_metadata(request, "in_flight_requests", count);
5558
}
5659

60+
// In report-only mode we serve all requests and only enforce the logging limit above
61+
if self.report_only {
62+
return handler.call(request);
63+
}
64+
5765
// Download requests are always accepted
5866
if request.path().starts_with("/api/v1/crates/") && request.path().ends_with("/download") {
5967
return handler.call(request);
@@ -86,7 +94,7 @@ fn over_capacity_response(request: &mut dyn RequestExt) -> AfterResult {
8694
}
8795

8896
fn read_env_percentage(name: &str, default: usize) -> usize {
89-
if let Ok(value) = std::env::var(name) {
97+
if let Ok(value) = env::var(name) {
9098
value.parse().unwrap_or(default)
9199
} else {
92100
default

0 commit comments

Comments
 (0)