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(ai): debounce approval store writes to reduce disk I/O

rcourtmand9370d88d8ff
diff --git a/internal/ai/approval/store.go b/internal/ai/approval/store.go
index fb944ae37c..58d71309c0 100644
--- a/internal/ai/approval/store.go
+++ b/internal/ai/approval/store.go
@@ -71,6 +71,8 @@ type Store struct {
defaultTimeout time.Duration
maxApprovals int
persist bool
+ saveTimer *time.Timer
+ savePending bool
}
// StoreConfig configures the approval store.
@@ -470,11 +472,59 @@ func (s *Store) save() {
}
}
+// scheduleSave debounces save operations: at most one write per 5 seconds.
+// Must be called while s.mu is held (read or write lock).
+func (s *Store) scheduleSave() {
+ if !s.persist || s.savePending {
+ return
+ }
+ s.savePending = true
+ s.saveTimer = time.AfterFunc(5*time.Second, func() {
+ s.mu.RLock()
+ s.savePending = false
+
+ approvals := make([]*ApprovalRequest, 0, len(s.approvals))
+ for _, a := range s.approvals {
+ approvals = append(approvals, a)
+ }
+ executions := make([]*ExecutionState, 0, len(s.executions))
+ for _, e := range s.executions {
+ executions = append(executions, e)
+ }
+ s.mu.RUnlock()
+
+ if data, err := json.MarshalIndent(approvals, "", " "); err == nil {
+ if err := os.WriteFile(s.approvalsFile(), data, 0600); err != nil {
+ log.Error().Err(err).Msg("Failed to save approvals")
+ }
+ }
+ if data, err := json.MarshalIndent(executions, "", " "); err == nil {
+ if err := os.WriteFile(s.executionsFile(), data, 0600); err != nil {
+ log.Error().Err(err).Msg("Failed to save executions")
+ }
+ }
+ })
+}
+
+// Flush triggers an immediate save, cancelling any pending debounced save.
+// Intended for shutdown paths.
+func (s *Store) Flush() {
+ s.mu.Lock()
+ if s.saveTimer != nil {
+ s.saveTimer.Stop()
+ }
+ s.savePending = false
+ s.mu.Unlock()
+
+ s.save()
+}
+
+// saveAsync is kept as a thin wrapper for backward compatibility.
func (s *Store) saveAsync() {
if !s.persist {
return
}
- go s.save()
+ s.scheduleSave()
}
// StartCleanup begins periodic cleanup of expired approvals and executions.
diff --git a/internal/ai/approval/store_test.go b/internal/ai/approval/store_test.go
index b85dac2a8c..ef62ef3055 100644
--- a/internal/ai/approval/store_test.go
+++ b/internal/ai/approval/store_test.go
@@ -529,8 +529,8 @@ func TestStorePersistence(t *testing.T) {
}
store1.StoreExecution(state)
- // Wait for async persistence to complete
- time.Sleep(100 * time.Millisecond)
+ // Flush debounced writes to disk immediately
+ store1.Flush()
// Create new store from same directory
store2, _ := NewStore(StoreConfig{