From a8ef9dd6cee10b9eacbd52fba5c4e17634d410fd Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Sun, 5 Apr 2026 02:40:31 +0000 Subject: [PATCH] fix(crowdsec): use read lock for non-mutating cache lookups --- .../api/handlers/crowdsec_dashboard_cache.go | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/backend/internal/api/handlers/crowdsec_dashboard_cache.go b/backend/internal/api/handlers/crowdsec_dashboard_cache.go index d439b8b5..1f738193 100644 --- a/backend/internal/api/handlers/crowdsec_dashboard_cache.go +++ b/backend/internal/api/handlers/crowdsec_dashboard_cache.go @@ -23,18 +23,26 @@ func newDashboardCache() *dashboardCache { } func (c *dashboardCache) Get(key string) (interface{}, bool) { - c.mu.Lock() - defer c.mu.Unlock() - + c.mu.RLock() entry, ok := c.entries[key] if !ok { + c.mu.RUnlock() return nil, false } - if time.Now().After(entry.expiresAt) { + if time.Now().Before(entry.expiresAt) { + data := entry.data + c.mu.RUnlock() + return data, true + } + c.mu.RUnlock() + + c.mu.Lock() + defer c.mu.Unlock() + entry, ok = c.entries[key] + if ok && time.Now().After(entry.expiresAt) { delete(c.entries, key) - return nil, false } - return entry.data, true + return nil, false } func (c *dashboardCache) Set(key string, data interface{}, ttl time.Duration) {