mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-10-15 05:58:33 +00:00
fix(cache): make in-memory backend Close-able so Shutdown stops its cleaner
cache.Shutdown only called Close() on an io.Closer, which only the Redis backend (CacheWrapper) satisfied -- the in-memory Cache's Shutdown (cancels ctx, stopping its cleanupRoutine background ticker) was never reached, so that goroutine leaked until process exit (and with the default set-up, and whenever Redis is unreachable and EnableCache falls back to memory). Add Close() error to Cache (calls Shutdown, idempotent, leaves the cache usable) so cache.Shutdown handles both backends symmetrically. Update Shutdown doc; add test TestMemoryCacheClose_IdempotentAndStillUsable.
This commit is contained in:
+28
@@ -1,6 +1,7 @@
|
||||
package libpack_cache_memory
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -146,3 +147,30 @@ func TestMemoryCacheConcurrentSetSameNewKey_CounterNotDoubled(t *testing.T) {
|
||||
t.Fatal("key should be retrievable after Set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemoryCacheClose_IdempotentAndStillUsable(t *testing.T) {
|
||||
// Compile-time guard: the memory backend must satisfy io.Closer so
|
||||
// cache.Shutdown() can stop its cleanup goroutine (it otherwise leaks
|
||||
// the background timer until process exit), matching the Redis backend.
|
||||
var _ io.Closer = (*Cache)(nil)
|
||||
|
||||
cache := New(DefaultTestExpiration)
|
||||
cache.Set("key", []byte("value"), DefaultTestExpiration)
|
||||
|
||||
// Close is idempotent: it only cancels the background cleaner.
|
||||
if err := cache.Close(); err != nil {
|
||||
t.Fatalf("first Close: %v", err)
|
||||
}
|
||||
if err := cache.Close(); err != nil {
|
||||
t.Fatalf("second Close: %v", err)
|
||||
}
|
||||
|
||||
// Stopping the cleaner must not render the cache unusable.
|
||||
if _, ok := cache.Get("key"); !ok {
|
||||
t.Fatal("entry should survive Close; Close only stops the background cleaner")
|
||||
}
|
||||
cache.Set("key2", []byte("v2"), DefaultTestExpiration)
|
||||
if _, ok := cache.Get("key2"); !ok {
|
||||
t.Fatal("cache should remain writable after Close")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user