From a1a89631593803119859f881fa794f60a9d8b1f4 Mon Sep 17 00:00:00 2001 From: Cody Lee Date: Sun, 25 Jan 2026 11:53:31 -0600 Subject: [PATCH] Fix authentication retry to prevent data gaps after re-auth Fixes #904 When a poll fails (typically with 401 Unauthorized after ~2 hour token expiration), the code would re-authenticate but then return the original poll error without retrying. This caused a one-minute data gap every 2 hours. Changes: - After successful re-authentication, retry the poll operation - Add 500ms delay before retry to allow controller to process new auth - Rename error variable to avoid shadowing during re-auth attempt This ensures that transient authentication failures during the re-auth window don't cause data gaps. Co-Authored-By: Claude Sonnet 4.5 --- pkg/inputunifi/collector.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/inputunifi/collector.go b/pkg/inputunifi/collector.go index 57232973..adc3a6e3 100644 --- a/pkg/inputunifi/collector.go +++ b/pkg/inputunifi/collector.go @@ -80,9 +80,16 @@ func (u *InputUnifi) collectController(c *Controller) (*poller.Metrics, error) { if err != nil { u.Logf("Re-authenticating to UniFi Controller: %s", c.URL) - if err := u.getUnifi(c); err != nil { - return metrics, fmt.Errorf("re-authenticating to %s: %w", c.URL, err) + if authErr := u.getUnifi(c); authErr != nil { + return metrics, fmt.Errorf("re-authenticating to %s: %w", c.URL, authErr) } + + // Brief delay to allow controller to process new authentication + time.Sleep(500 * time.Millisecond) + + // Retry the poll after successful re-authentication + u.LogDebugf("Retrying poll after re-authentication: %s", c.URL) + metrics, err = u.pollController(c) } return metrics, err