changes to ratelimits now take effect on config reload

This commit is contained in:
Wez Furlong committed 2019-11-24 20:24:58 -08:00
1 parent 1ba720b28c
commit 80719f419d
3 files changed
+38 -5

No files matched your search

+1 -2
View File
@@ -1,4 +1,3 @@
use crate::config::configuration;
use crate::frontend::{executor, low_pri_executor};
use crate::mux::tab::{Tab, TabId};
use crate::mux::window::{Window, WindowId};
@@ -48,7 +47,7 @@ fn read_from_tab_pty(tab_id: TabId, mut reader: Box<dyn std::io::Read>) {
const BUFSIZE: usize = 32 * 1024;
let mut buf = [0; BUFSIZE];
let mut lim = RateLimiter::new(configuration().ratelimit_output_bytes_per_second);
let mut lim = RateLimiter::new(|config| config.ratelimit_output_bytes_per_second);
loop {
match reader.read(&mut buf) {
+35 -1
View File
@@ -1,22 +1,55 @@
use crate::config::{configuration, ConfigHandle};
use ratelimit_meter::algorithms::NonConformance;
use ratelimit_meter::{DirectRateLimiter, LeakyBucket, NegativeMultiDecision};
use std::time::{Duration, Instant};
pub struct RateLimiter {
lim: DirectRateLimiter<LeakyBucket>,
get_limit_value: Box<dyn Fn(&ConfigHandle) -> u32 + 'static + Send>,
generation: usize,
capacity_per_second: u32,
}
impl RateLimiter {
pub fn new(capacity_per_second: u32) -> Self {
/// Construct a new rate limiter.
/// `get_limit_value` is a function that will extract a limit
/// from a config handle; the limit will be automatically adjusted
/// as the config changes.
/// This will effectively reset the counter if the limit value in
/// the new generation of config is different to the prior value.
pub fn new<F: Fn(&ConfigHandle) -> u32 + 'static + Send>(get_limit_value: F) -> Self {
let config = configuration();
let generation = config.generation();
let get_limit_value = Box::new(get_limit_value);
let capacity_per_second = get_limit_value(&config);
Self {
lim: DirectRateLimiter::<LeakyBucket>::per_second(
std::num::NonZeroU32::new(capacity_per_second)
.expect("RateLimiter capacity to be non-zero"),
),
get_limit_value,
generation,
capacity_per_second,
}
}
fn check_config_reload(&mut self) {
let config = configuration();
let generation = config.generation();
if generation != self.generation {
let value = (self.get_limit_value)(&config);
if value != self.capacity_per_second {
self.lim = DirectRateLimiter::<LeakyBucket>::per_second(
std::num::NonZeroU32::new(value).expect("RateLimiter capacity to be non-zero"),
);
self.capacity_per_second = value;
}
self.generation = generation;
}
}
pub fn non_blocking_admittance_check(&mut self, amount: u32) -> bool {
self.check_config_reload();
self.lim.check_n(amount).is_ok()
}
@@ -26,6 +59,7 @@ impl RateLimiter {
/// If no items can be admitted immediately, returns a duration
/// of time after which the caller should retry to admit.
pub fn admit_check(&mut self, mut amount: u32) -> Result<u32, Duration> {
self.check_config_reload();
loop {
match self.lim.check_n(amount) {
Ok(_) => return Ok(amount),
+2 -2
View File
@@ -425,9 +425,9 @@ struct ClientSurfaceState {
impl ClientSurfaceState {
fn new(cols: usize, rows: usize) -> Self {
let push_limiter = RateLimiter::new(configuration().ratelimit_mux_output_pushes_per_second);
let push_limiter = RateLimiter::new(|config| config.ratelimit_mux_output_pushes_per_second);
let update_limiter =
RateLimiter::new(configuration().ratelimit_mux_output_scans_per_second);
RateLimiter::new(|config| config.ratelimit_mux_output_scans_per_second);
let surface = Surface::new(cols, rows);
Self {
surface,