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 <noreply@anthropic.com>
This commit is contained in:
Cody Lee
2026-01-25 11:53:31 -06:00
parent 092dd69509
commit a1a8963159

View File

@@ -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