feat: close/restart cleanup goroutine in runtime#214
feat: close/restart cleanup goroutine in runtime#214ssagarverma merged 3 commits intohashicorp:mainfrom
Conversation
|
Thank you for your submission! We require that all contributors sign our Contributor License Agreement ("CLA") before we can accept the contribution. Read and sign the agreement Learn more about why HashiCorp requires a CLA and what the CLA includes Have you signed the CLA already but the status is still pending? Recheck it. |
|
I added cf37413 because the cleanup goroutine might have been sleeping when |
| select { | ||
| case <-time.After(timeToExpire): | ||
| case <-done: | ||
| return | ||
| } |
There was a problem hiding this comment.
This is merely an optimization to exit this goroutine ASAP instead of letting it sleep up to the ttl.
| select { | ||
| case <-done: | ||
| // Done channel closed while sleeping, return without deleting entries | ||
| return | ||
| default: | ||
| } |
There was a problem hiding this comment.
This is the correctness fix from cf37413. Without checking the done channel after sleeping, if Close() was called during time.Sleep (now time.After) then the cleanup goroutine would have made one more deletion pass after Close() was called.
| } | ||
|
|
||
| // startGoroutine starts the cleanup goroutine for expired entries. | ||
| func (c *LRU[K, V]) startGoroutine() { |
There was a problem hiding this comment.
Nitpick: might be nice for this function to be named what it does and not how it's implemented.
|
Just a thought that I don't needs to be required here but if you wrapped the check on the |
|
Would want this too, because testing the cache is a bit difficult without the ability to stop it. And clearing things, and this just generally leaks the cleanup go routine without a proper stopping. |
|
@tgross could we get review from hashicorp/team-ip-compliance? |
Sorry about that, I'm not in that group so I can't merge this myself. I've given that group a gentle nudge. |
|
Yay 🎉 ❤️ |
Description
This PR adds the ability to stop and restart the cleanup goroutine at runtime for the expirable LRU cache. This feature provides better control over cache behavior and resource management, particularly useful in scenarios where you need to temporarily preserve expired entries or gracefully shutdown the cache.
New Methods
Close(): Stops the background cleanup goroutine that removes expired entries. After calling this method, expired items remain accessible in the cache until manually removed or the cleanup is restarted.Restart(): Restarts the cleanup goroutine after it has been stopped with Close(). If the goroutine is already running, this method safely does nothing.Implementation Details
cleanupStoppedboolean field to track the cleanup goroutine stateGet,Peek,Keys,Values) to return expired items when the cleanup goroutine is stoppedBehavior
When cleanup is running (default):
Get(),Peek(), etc. return false for expired itemsWhen cleanup is stopped (after
Close()):Use Cases
Breaking Changes
None. The changes are fully backward compatible. Existing code will continue to work as before, with the cleanup goroutine running by default.
Related Issue
How Has This Been Tested?
Added comprehensive test coverage including:
TestCache_CloseGoRoutine: Verifies expired items remain accessible after Close()TestCache_RestartGoRoutine: Confirms cleanup resumes after Restart()