Forwarder: separate update_data_budget (#30920)

This commit is contained in:
Andrew Fitzgerald 2023-03-28 12:58:39 -07:00 committed by GitHub
parent 6285aaee89
commit 2dfc46c71e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 11 deletions

View File

@ -162,17 +162,7 @@ impl Forwarder {
None => return (Ok(()), 0, None),
};
const INTERVAL_MS: u64 = 100;
// 12 MB outbound limit per second
const MAX_BYTES_PER_SECOND: usize = 12_000_000;
const MAX_BYTES_PER_INTERVAL: usize = MAX_BYTES_PER_SECOND * INTERVAL_MS as usize / 1000;
const MAX_BYTES_BUDGET: usize = MAX_BYTES_PER_INTERVAL * 5;
self.data_budget.update(INTERVAL_MS, |bytes| {
std::cmp::min(
bytes.saturating_add(MAX_BYTES_PER_INTERVAL),
MAX_BYTES_BUDGET,
)
});
self.update_data_budget();
let packet_vec: Vec<_> = forwardable_packets
.filter_map(|p| {
@ -227,6 +217,21 @@ impl Forwarder {
(Ok(()), packet_vec_len, Some(leader_pubkey))
}
/// Re-fill the data budget if enough time has passed
fn update_data_budget(&self) {
const INTERVAL_MS: u64 = 100;
// 12 MB outbound limit per second
const MAX_BYTES_PER_SECOND: usize = 12_000_000;
const MAX_BYTES_PER_INTERVAL: usize = MAX_BYTES_PER_SECOND * INTERVAL_MS as usize / 1000;
const MAX_BYTES_BUDGET: usize = MAX_BYTES_PER_INTERVAL * 5;
self.data_budget.update(INTERVAL_MS, |bytes| {
std::cmp::min(
bytes.saturating_add(MAX_BYTES_PER_INTERVAL),
MAX_BYTES_BUDGET,
)
});
}
}
#[cfg(test)]