Skip to content

Commit

Permalink
Remove code that could potentially panic (#3343)
Browse files Browse the repository at this point in the history
  • Loading branch information
nieznanysprawiciel authored Oct 8, 2024
1 parent 7f6a3ae commit a224660
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions utils/actix_utils/src/deadline_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ impl DeadlineChecker {

fn on_deadline_elapsed(&mut self, ctx: &mut Context<Self>, deadline: DateTime<Utc>) {
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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -165,11 +170,7 @@ impl Handler<TrackDeadline> for DeadlineChecker {
type Result = ();

fn handle(&mut self, msg: TrackDeadline, ctx: &mut Context<Self>) -> 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).
Expand Down

0 comments on commit a224660

Please sign in to comment.