improvement: display rate limit messages max once per sec

This commit is contained in:
Greg Heartsfield 2022-01-05 18:30:47 -05:00
parent 5c8390bbe0
commit 4e51e61d16

View File

@ -136,6 +136,7 @@ pub async fn db_writer(
upgrade_db(&mut conn)?; upgrade_db(&mut conn)?;
// get rate limit settings // get rate limit settings
let rps_setting = config.limits.messages_per_sec; let rps_setting = config.limits.messages_per_sec;
let mut most_recent_rate_limit = Instant::now();
let mut lim_opt = None; let mut lim_opt = None;
let clock = governor::clock::QuantaClock::default(); let clock = governor::clock::QuantaClock::default();
if let Some(rps) = rps_setting { if let Some(rps) = rps_setting {
@ -182,8 +183,20 @@ pub async fn db_writer(
if event_write { if event_write {
if let Some(ref lim) = lim_opt { if let Some(ref lim) = lim_opt {
if let Err(n) = lim.check() { if let Err(n) = lim.check() {
info!("Rate limiting event creation"); let wait_for = n.wait_time_from(clock.now());
thread::sleep(n.wait_time_from(clock.now())); // check if we have recently logged rate
// limits, but print out a message only once
// per second.
if most_recent_rate_limit.elapsed().as_secs() > 1 {
warn!(
"rate limit reached for event creation (sleep for {:?})",
wait_for
);
// reset last rate limit message
most_recent_rate_limit = Instant::now();
}
// block event writes, allowing them to queue up
thread::sleep(wait_for);
continue; continue;
} }
} }