From a2246609d1d0df76cd9cd10c158d0f59358eca3c Mon Sep 17 00:00:00 2001 From: nieznanysprawiciel Date: Tue, 8 Oct 2024 14:13:18 +0200 Subject: [PATCH] Remove code that could potentially panic (#3343) --- utils/actix_utils/src/deadline_checker.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/utils/actix_utils/src/deadline_checker.rs b/utils/actix_utils/src/deadline_checker.rs index a737be189..a386ecd4b 100644 --- a/utils/actix_utils/src/deadline_checker.rs +++ b/utils/actix_utils/src/deadline_checker.rs @@ -97,7 +97,12 @@ impl DeadlineChecker { fn on_deadline_elapsed(&mut self, ctx: &mut Context, deadline: DateTime) { let now = Utc::now(); - assert!(now >= deadline); + if now < deadline { + log::warn!( + "Programming error: Deadline didn't really elapse: {now} < {deadline}.\ + This was assert! in previous version, which could cause DeadlineChecker's death." + ); + } let elapsed = self.drain_elapsed(now); @@ -133,7 +138,7 @@ impl DeadlineChecker { elapsed.sort_by(|dead1, dead2| dead1.deadline.cmp(&dead2.deadline)); - // Remove Agreements with empty lists. Otherwise no longer needed Agreements + // Remove Agreements with empty lists. Otherwise, no longer needed Agreements // would remain in HashMap for always. self.deadlines = self .deadlines @@ -165,11 +170,7 @@ impl Handler for DeadlineChecker { type Result = (); fn handle(&mut self, msg: TrackDeadline, ctx: &mut Context) -> Self::Result { - if self.deadlines.get(&msg.category).is_none() { - self.deadlines.insert(msg.category.to_string(), vec![]); - } - - let deadlines = self.deadlines.get_mut(&msg.category).unwrap(); + let deadlines = self.deadlines.entry(msg.category.clone()).or_default(); let idx = match deadlines.binary_search_by(|element| element.deadline.cmp(&msg.deadline)) { // Element with this deadline existed. We add new element behind it (order shouldn't matter since timestamps // are the same, but it's better to keep order of calls to `track_deadline` function).