Fix healthcheck interval not resetting

Ref PAN-158
This commit is contained in:
Owen
2025-12-15 12:10:47 -05:00
parent 44470abd54
commit 0637360b31

View File

@@ -58,7 +58,7 @@ type Target struct {
LastCheck time.Time `json:"lastCheck"` LastCheck time.Time `json:"lastCheck"`
LastError string `json:"lastError,omitempty"` LastError string `json:"lastError,omitempty"`
CheckCount int `json:"checkCount"` CheckCount int `json:"checkCount"`
ticker *time.Ticker timer *time.Timer
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
} }
@@ -304,26 +304,26 @@ func (m *Monitor) monitorTarget(target *Target) {
go m.callback(m.GetTargets()) go m.callback(m.GetTargets())
} }
// Set up ticker based on current status // Set up timer based on current status
interval := time.Duration(target.Config.Interval) * time.Second interval := time.Duration(target.Config.Interval) * time.Second
if target.Status == StatusUnhealthy { if target.Status == StatusUnhealthy {
interval = time.Duration(target.Config.UnhealthyInterval) * time.Second interval = time.Duration(target.Config.UnhealthyInterval) * time.Second
} }
logger.Debug("Target %d: initial check interval set to %v", target.Config.ID, interval) logger.Debug("Target %d: initial check interval set to %v", target.Config.ID, interval)
target.ticker = time.NewTicker(interval) target.timer = time.NewTimer(interval)
defer target.ticker.Stop() defer target.timer.Stop()
for { for {
select { select {
case <-target.ctx.Done(): case <-target.ctx.Done():
logger.Info("Stopping health check monitoring for target %d", target.Config.ID) logger.Info("Stopping health check monitoring for target %d", target.Config.ID)
return return
case <-target.ticker.C: case <-target.timer.C:
oldStatus := target.Status oldStatus := target.Status
m.performHealthCheck(target) m.performHealthCheck(target)
// Update ticker interval if status changed // Update timer interval if status changed
newInterval := time.Duration(target.Config.Interval) * time.Second newInterval := time.Duration(target.Config.Interval) * time.Second
if target.Status == StatusUnhealthy { if target.Status == StatusUnhealthy {
newInterval = time.Duration(target.Config.UnhealthyInterval) * time.Second newInterval = time.Duration(target.Config.UnhealthyInterval) * time.Second
@@ -332,11 +332,12 @@ func (m *Monitor) monitorTarget(target *Target) {
if newInterval != interval { if newInterval != interval {
logger.Debug("Target %d: updating check interval from %v to %v due to status change", logger.Debug("Target %d: updating check interval from %v to %v due to status change",
target.Config.ID, interval, newInterval) target.Config.ID, interval, newInterval)
target.ticker.Stop()
target.ticker = time.NewTicker(newInterval)
interval = newInterval interval = newInterval
} }
// Reset timer for next check with current interval
target.timer.Reset(interval)
// Notify callback if status changed // Notify callback if status changed
if oldStatus != target.Status && m.callback != nil { if oldStatus != target.Status && m.callback != nil {
logger.Info("Target %d status changed: %s -> %s", logger.Info("Target %d status changed: %s -> %s",