fix: correctly notify auto-cleaner when an item's TTL is shortened#206
Open
PiAreSquared wants to merge 1 commit into
Open
fix: correctly notify auto-cleaner when an item's TTL is shortened#206PiAreSquared wants to merge 1 commit into
PiAreSquared wants to merge 1 commit into
Conversation
Coverage Report for CI Build 24898134592Coverage increased (+0.003%) to 99.738%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a bug where the background auto-cleaner fails to wake up early if an existing item's TTL is shortened, causing the item to live past its new expiration time. This seems to have been first reported in #204.
Root Cause
When
Set()(orGet()with touch) is called on an existing item, the item's internalexpiresAtis mutated first. Afterwards,updateExpirations()is called to reorder the heap and notify the auto-cleaner's timer channel.Previously,
updateExpirationsattempted to snapshot the old closest expiration time after the item had already been mutated. If the updated item was already at the front of the expiration queue,oldExpiresAtperfectly matched the newly updated time. Because the method didn't detect a change in the root expiration time, it silently returned without notifying thetimerCh, causing the auto-cleaner to oversleep.Fix
updateExpirationssignature to takeoldExpiresAtas a parameterexpQueueinset()andget()before mutating the item viaitem.touch()oritem.update()oldExpiresAtintoupdateExpirationsTesting
I updated the existing
Test_Cache_updateExpirationsto accommodate the signature change and added a new regression test.Test_Cache_ShortenedTTLNotifiesAutoCleanesets an item with a 1-hour TTL, immediately updates it to 10ms, and asserts that the auto-cleaner wakes up and evicts the item on time.