From 9b10118d342cfdfad0742da0fcd6e0f80e1ad53e Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Mon, 8 Feb 2021 11:36:16 -0800 Subject: [PATCH] Metacache add abs entry limit (#11483) Add an absolute limit to the number of metacaches for a bucket. Delete excess caches if they haven't been handed out in an hour. --- cmd/metacache-bucket.go | 24 ++++++++++++++++++++++++ cmd/metacache-manager.go | 1 + 2 files changed, 25 insertions(+) diff --git a/cmd/metacache-bucket.go b/cmd/metacache-bucket.go index 1e0aaa5f2..e958d7b9e 100644 --- a/cmd/metacache-bucket.go +++ b/cmd/metacache-bucket.go @@ -24,6 +24,7 @@ import ( "net/http" "path" "runtime/debug" + "sort" "strings" "sync" "time" @@ -344,6 +345,29 @@ func (b *bucketMetacache) cleanup() { } } + // If above limit, remove the caches with the oldest handout time. + if len(caches)-len(remove) > metacacheMaxEntries { + remainCaches := make([]metacache, 0, len(caches)-len(remove)) + for id, cache := range caches { + if _, ok := remove[id]; ok { + continue + } + remainCaches = append(remainCaches, cache) + } + if len(remainCaches) > metacacheMaxEntries { + // Sort oldest last... + sort.Slice(remainCaches, func(i, j int) bool { + return remainCaches[i].lastHandout.Before(remainCaches[j].lastHandout) + }) + // Keep first metacacheMaxEntries... + for _, cache := range remainCaches[metacacheMaxEntries:] { + if time.Since(cache.lastHandout) > time.Hour { + remove[cache.id] = struct{}{} + } + } + } + } + for id := range remove { b.deleteCache(id) } diff --git a/cmd/metacache-manager.go b/cmd/metacache-manager.go index ed1d2432e..5e48b3972 100644 --- a/cmd/metacache-manager.go +++ b/cmd/metacache-manager.go @@ -43,6 +43,7 @@ type metacacheManager struct { } const metacacheManagerTransientBucket = "**transient**" +const metacacheMaxEntries = 5000 // initManager will start async saving the cache. func (m *metacacheManager) initManager() {