fix: expiration bug (#137)

This commit is contained in:
qyuzh
2023-10-28 04:52:59 +08:00
committed by GitHub
parent a1fd215d2a
commit 9b93f751b3

View File

@@ -177,8 +177,6 @@ impl Db {
.map(|expiration| expiration > when)
.unwrap_or(true);
// Track the expiration.
state.expirations.insert((when, key.clone()));
when
});
@@ -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.