From 4e51e61d16c76e932e0de2b40df420284021bac8 Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Wed, 5 Jan 2022 18:30:47 -0500 Subject: [PATCH] improvement: display rate limit messages max once per sec --- src/db.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/db.rs b/src/db.rs index 373541a..1bdf04b 100644 --- a/src/db.rs +++ b/src/db.rs @@ -136,6 +136,7 @@ pub async fn db_writer( upgrade_db(&mut conn)?; // get rate limit settings let rps_setting = config.limits.messages_per_sec; + let mut most_recent_rate_limit = Instant::now(); let mut lim_opt = None; let clock = governor::clock::QuantaClock::default(); if let Some(rps) = rps_setting { @@ -182,8 +183,20 @@ pub async fn db_writer( if event_write { if let Some(ref lim) = lim_opt { if let Err(n) = lim.check() { - info!("Rate limiting event creation"); - thread::sleep(n.wait_time_from(clock.now())); + let wait_for = 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; } }