Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

perf(unifiedresources): share clean view cache reads

pulse-triage[bot]bab9b8dba07c
diff --git a/internal/unifiedresources/registry.go b/internal/unifiedresources/registry.go
index fa85deb042..69003927fd 100644
--- a/internal/unifiedresources/registry.go
+++ b/internal/unifiedresources/registry.go
@@ -5622,15 +5622,29 @@ func (rr *ResourceRegistry) ensureViewsLocked() {
rr.rebuildViews()
}
-// withViewCache acquires a write lock to ensure views are fresh, then
-// downgrades to a read lock and calls fn. This avoids TOCTOU gaps.
+// withViewCache reads clean cached views under a shared lock. If a rebuild is
+// needed, it ensures the cache under the write lock and retries the shared
+// read; retrying also handles invalidation between rebuilding and reading.
func withViewCache[T any](rr *ResourceRegistry, fn func() T) T {
- rr.mu.Lock()
- rr.ensureViewsLocked()
- rr.mu.Unlock()
+ for {
+ if value, clean := readCleanViewCache(rr, fn); clean {
+ return value
+ }
+
+ rr.mu.Lock()
+ rr.ensureViewsLocked()
+ rr.mu.Unlock()
+ }
+}
+
+func readCleanViewCache[T any](rr *ResourceRegistry, fn func() T) (T, bool) {
rr.mu.RLock()
defer rr.mu.RUnlock()
- return fn()
+ if rr.viewsDirty {
+ var zero T
+ return zero, false
+ }
+ return fn(), true
}
// rebuildViews recomputes all cached view slices from the current resource map.
diff --git a/internal/unifiedresources/registry_test.go b/internal/unifiedresources/registry_test.go
index d6af61d4cb..34fc0727e6 100644
--- a/internal/unifiedresources/registry_test.go
+++ b/internal/unifiedresources/registry_test.go
@@ -12,6 +12,27 @@ import (
"github.com/rcourtman/pulse-go-rewrite/internal/storagehealth"
)
+func TestRegistry_CachedReadsUseSharedLock(t *testing.T) {
+ rr := NewRegistry(nil)
+ // A clean, empty registry already has a valid empty cache. Holding another
+ // read lock must not prevent a cached accessor from completing.
+ rr.mu.RLock()
+ done := make(chan struct{})
+ go func() {
+ _ = rr.VMs()
+ close(done)
+ }()
+
+ select {
+ case <-done:
+ rr.mu.RUnlock()
+ case <-time.After(5 * time.Second):
+ rr.mu.RUnlock()
+ <-done
+ t.Fatal("cached view read waited for an exclusive registry lock")
+ }
+}
+
// TestMemoryStore_RecordActionAuditAppliesRedaction is an integration check
// at the registry-store boundary. The MemoryStore is the backing store the
// registry uses in tests and contract examples, and operator-authored audit