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.

fix(pro): add cleanup goroutine for alert analyzer memory leak

rcourtmanc2de1b256b7c
diff --git a/internal/ai/alert_triggered.go b/internal/ai/alert_triggered.go
index d9551a4351..a125e9702c 100644
--- a/internal/ai/alert_triggered.go
+++ b/internal/ai/alert_triggered.go
@@ -27,6 +27,10 @@ type AlertTriggeredAnalyzer struct {
// Track pending analyses to deduplicate concurrent alerts
pending map[string]bool
+
+ // Cleanup goroutine management
+ cleanupTicker *time.Ticker
+ stopCh chan struct{}
}
// NewAlertTriggeredAnalyzer creates a new alert-triggered analyzer
@@ -38,7 +42,53 @@ func NewAlertTriggeredAnalyzer(patrolService *PatrolService, stateProvider State
lastAnalyzed: make(map[string]time.Time),
cooldown: 5 * time.Minute, // Don't re-analyze the same resource within 5 minutes
pending: make(map[string]bool),
+ stopCh: make(chan struct{}),
+ }
+}
+
+// Start begins the background cleanup goroutine
+func (a *AlertTriggeredAnalyzer) Start() {
+ a.mu.Lock()
+ defer a.mu.Unlock()
+
+ if a.cleanupTicker != nil {
+ return // Already started
+ }
+
+ ticker := time.NewTicker(30 * time.Minute)
+ a.cleanupTicker = ticker
+ stopCh := a.stopCh
+
+ go func() {
+ for {
+ select {
+ case <-ticker.C:
+ a.CleanupOldCooldowns()
+ case <-stopCh:
+ return
+ }
+ }
+ }()
+ log.Debug().Msg("Alert-triggered analyzer cleanup goroutine started")
+}
+
+// Stop stops the background cleanup goroutine
+func (a *AlertTriggeredAnalyzer) Stop() {
+ a.mu.Lock()
+ defer a.mu.Unlock()
+
+ if a.cleanupTicker != nil {
+ a.cleanupTicker.Stop()
+ a.cleanupTicker = nil
+ }
+ select {
+ case <-a.stopCh:
+ // Already closed
+ default:
+ close(a.stopCh)
}
+ a.stopCh = make(chan struct{}) // Reset for potential restart
+ log.Debug().Msg("Alert-triggered analyzer cleanup goroutine stopped")
}
// SetEnabled enables or disables alert-triggered analysis
diff --git a/internal/ai/alert_triggered_test.go b/internal/ai/alert_triggered_test.go
index ba18f3ccb0..65f586ef3c 100644
--- a/internal/ai/alert_triggered_test.go
+++ b/internal/ai/alert_triggered_test.go
@@ -1047,3 +1047,83 @@ func TestAlertTriggeredAnalyzer_AnalyzeGuestFromAlert_ContainerBackup(t *testing
t.Error("Expected findings for container with backup set, got 0")
}
}
+
+func TestAlertTriggeredAnalyzer_StartStop(t *testing.T) {
+ analyzer := NewAlertTriggeredAnalyzer(nil, nil)
+
+ // Verify not started initially
+ analyzer.mu.RLock()
+ tickerBefore := analyzer.cleanupTicker
+ analyzer.mu.RUnlock()
+
+ if tickerBefore != nil {
+ t.Error("Expected cleanupTicker to be nil before Start")
+ }
+
+ // Start the cleanup goroutine
+ analyzer.Start()
+
+ analyzer.mu.RLock()
+ tickerAfter := analyzer.cleanupTicker
+ analyzer.mu.RUnlock()
+
+ if tickerAfter == nil {
+ t.Error("Expected cleanupTicker to be set after Start")
+ }
+
+ // Calling Start again should be a no-op
+ analyzer.Start()
+
+ analyzer.mu.RLock()
+ tickerAfterSecondStart := analyzer.cleanupTicker
+ analyzer.mu.RUnlock()
+
+ if tickerAfterSecondStart != tickerAfter {
+ t.Error("Expected cleanupTicker to remain the same after second Start")
+ }
+
+ // Stop the cleanup goroutine
+ analyzer.Stop()
+
+ analyzer.mu.RLock()
+ tickerAfterStop := analyzer.cleanupTicker
+ analyzer.mu.RUnlock()
+
+ if tickerAfterStop != nil {
+ t.Error("Expected cleanupTicker to be nil after Stop")
+ }
+}
+
+func TestAlertTriggeredAnalyzer_CleanupTickerRuns(t *testing.T) {
+ analyzer := NewAlertTriggeredAnalyzer(nil, nil)
+
+ // Add an old entry
+ analyzer.mu.Lock()
+ analyzer.lastAnalyzed["old-entry"] = time.Now().Add(-2 * time.Hour)
+ analyzer.mu.Unlock()
+
+ // Start with a very short ticker for testing (we'll manually trigger cleanup)
+ analyzer.Start()
+ defer analyzer.Stop()
+
+ // Verify the entry exists
+ analyzer.mu.RLock()
+ _, existsBefore := analyzer.lastAnalyzed["old-entry"]
+ analyzer.mu.RUnlock()
+
+ if !existsBefore {
+ t.Fatal("Expected 'old-entry' to exist before cleanup")
+ }
+
+ // Manually trigger cleanup
+ analyzer.CleanupOldCooldowns()
+
+ // Verify the entry was removed
+ analyzer.mu.RLock()
+ _, existsAfter := analyzer.lastAnalyzed["old-entry"]
+ analyzer.mu.RUnlock()
+
+ if existsAfter {
+ t.Error("Expected 'old-entry' to be removed after cleanup")
+ }
+}
diff --git a/internal/ai/service.go b/internal/ai/service.go
index 52f4b8fa0c..5554a9f015 100644
--- a/internal/ai/service.go
+++ b/internal/ai/service.go
@@ -429,14 +429,14 @@ func (s *Service) GetLicenseState() (string, bool) {
return checker.GetLicenseStateString()
}
-// FeatureAIPatrol is the license feature constant for AI Patrol
-const FeatureAIPatrol = "ai_patrol"
-
-// FeatureAIAlerts is the license feature constant for AI Alert Analysis
-const FeatureAIAlerts = "ai_alerts"
-
-// FeatureAIAutoFix is the license feature constant for AI Auto-Fix
-const FeatureAIAutoFix = "ai_autofix"
+// Feature constants are convenience aliases for license.Feature* constants.
+// These allow the ai package to be used without importing the license package directly.
+// IMPORTANT: These MUST match the values in internal/license/features.go
+const (
+ FeatureAIPatrol = "ai_patrol" // license.FeatureAIPatrol
+ FeatureAIAlerts = "ai_alerts" // license.FeatureAIAlerts
+ FeatureAIAutoFix = "ai_autofix" // license.FeatureAIAutoFix
+)
// StartPatrol starts the background patrol service
func (s *Service) StartPatrol(ctx context.Context) {
@@ -486,6 +486,7 @@ func (s *Service) StartPatrol(ctx context.Context) {
enabled = false
}
alertAnalyzer.SetEnabled(enabled)
+ alertAnalyzer.Start() // Start cleanup goroutine
log.Info().
Bool("enabled", enabled).
Msg("Alert-triggered AI analysis configured")
@@ -501,11 +502,15 @@ func (s *Service) StartPatrol(ctx context.Context) {
func (s *Service) StopPatrol() {
s.mu.RLock()
patrol := s.patrolService
+ alertAnalyzer := s.alertTriggeredAnalyzer
s.mu.RUnlock()
if patrol != nil {
patrol.Stop()
}
+ if alertAnalyzer != nil {
+ alertAnalyzer.Stop()
+ }
}
// ReconfigurePatrol updates the patrol configuration without restarting