Skip to content

Commit

Permalink
fix: expiration bug (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
qyuzh committed Oct 27, 2023
1 parent a1fd215 commit 9b93f75
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ impl Db {
.map(|expiration| expiration > when)
.unwrap_or(true);

// Track the expiration.
state.expirations.insert((when, key.clone()));
when
});

Expand All @@ -197,10 +195,17 @@ impl Db {
if let Some(prev) = prev {
if let Some(when) = prev.expires_at {
// clear expiration
state.expirations.remove(&(when, key));
state.expirations.remove(&(when, key.clone()));
}
}

// Track the expiration. If we insert before remove that will cause bug
// when current `(when, key)` equals prev `(when, key)`. Remove then insert
// can avoid this.
if let Some(when) = expires_at {
state.expirations.insert((when, key));
}

// Release the mutex before notifying the background task. This helps
// reduce contention by avoiding the background task waking up only to
// be unable to acquire the mutex due to this function still holding it.
Expand Down

0 comments on commit 9b93f75

Please sign in to comment.