Skip to content

Commit 6494ac6

Browse files
committed
Do not count downloads as in-flight
Download requests are no longer counted as in-flight and the variable name is updated to reflect this. By default 20% of database connections are still reserved for updating download counts, but other requests will be rejected less often. Additionally this saves 2 atomic updates when serving the most popular request.
1 parent ba858ae commit 6494ac6

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

src/middleware/balance_capacity.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use conduit::{RequestExt, StatusCode};
1818
pub(super) struct BalanceCapacity {
1919
handler: Option<Box<dyn Handler>>,
2020
capacity: usize,
21-
in_flight_requests: AtomicUsize,
21+
in_flight_non_dl_requests: AtomicUsize,
2222
report_only: bool,
2323
log_at_percentage: usize,
2424
throttle_at_percentage: usize,
@@ -30,7 +30,7 @@ impl BalanceCapacity {
3030
Self {
3131
handler: None,
3232
capacity,
33-
in_flight_requests: AtomicUsize::new(0),
33+
in_flight_non_dl_requests: AtomicUsize::new(0),
3434
report_only: env::var("WEB_CAPACITY_REPORT_ONLY").ok().is_some(),
3535
log_at_percentage: read_env_percentage("WEB_CAPACITY_LOG_PCT", 50),
3636
throttle_at_percentage: read_env_percentage("WEB_CAPACITY_THROTTLE_PCT", 70),
@@ -73,18 +73,18 @@ impl AroundMiddleware for BalanceCapacity {
7373

7474
impl Handler for BalanceCapacity {
7575
fn call(&self, request: &mut dyn RequestExt) -> AfterResult {
76+
// Download requests are always accepted and do not affect the request count
77+
if request.path().starts_with("/api/v1/crates/") && request.path().ends_with("/download") {
78+
return self.handle(request);
79+
}
80+
7681
// The _drop_on_exit ensures the counter is decremented for all exit paths (including panics)
77-
let (_drop_on_exit, count) = RequestCounter::add_one(&self.in_flight_requests);
82+
let (_drop_on_exit, count) = RequestCounter::add_one(&self.in_flight_non_dl_requests);
7883
let load = 100 * count / self.capacity;
7984

8085
// Begin logging request count so early stages of load increase can be located
8186
if load >= self.log_at_percentage {
82-
super::log_request::add_custom_metadata(request, "in_flight_requests", count);
83-
}
84-
85-
// Download requests are always accepted
86-
if request.path().starts_with("/api/v1/crates/") && request.path().ends_with("/download") {
87-
return self.handle(request);
87+
super::log_request::add_custom_metadata(request, "in_flight_non_dl_requests", count);
8888
}
8989

9090
// Reject read-only requests as load nears capacity. Bots are likely to send only safe
@@ -110,9 +110,6 @@ fn read_env_percentage(name: &str, default: usize) -> usize {
110110
}
111111
}
112112

113-
// FIXME(JTG): I've copied the following from my `conduit-hyper` crate. Once we transition from
114-
// `civet`, we could pass the in_flight_request count from `condut-hyper` via a request extension.
115-
116113
/// A struct that stores a reference to an atomic counter so it can be decremented when dropped
117114
struct RequestCounter<'a> {
118115
counter: &'a AtomicUsize,

0 commit comments

Comments
 (0)