diff --git a/src/mux/mod.rs b/src/mux/mod.rs index e507d2757..3c4e47635 100644 --- a/src/mux/mod.rs +++ b/src/mux/mod.rs @@ -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) { 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) { diff --git a/src/ratelim.rs b/src/ratelim.rs index c63d12031..974e470cb 100644 --- a/src/ratelim.rs +++ b/src/ratelim.rs @@ -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, + get_limit_value: Box 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 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::::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::::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 { + self.check_config_reload(); loop { match self.lim.check_n(amount) { Ok(_) => return Ok(amount), diff --git a/src/server/listener.rs b/src/server/listener.rs index 32f82233a..332a88f30 100644 --- a/src/server/listener.rs +++ b/src/server/listener.rs @@ -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,