Files
Pulse/internal/monitoring/backoff.go
rcourtman b1f445b33d feat: implement error handling with circuit breakers and backoff (Phase 2 Task 7)
Adds comprehensive error resilience:
- Circuit breaker with closed/open/half-open states (3 failures = trip)
- Exponential backoff with jitter (2s initial, 2x multiplier, 5min max)
- Dead-letter queue for tasks exceeding 5 retry attempts
- Error classification (transient vs permanent) using internal/errors helpers
- Per-instance failure tracking and breaker state management
- Integration with staleness tracker for outcome recording

Task 7 of 10 complete (70%). Ready for API surfaces and testing.
2025-10-20 15:13:37 +00:00

40 lines
705 B
Go

package monitoring
import (
"math"
"time"
)
type backoffConfig struct {
Initial time.Duration
Multiplier float64
Jitter float64
Max time.Duration
}
func (cfg backoffConfig) nextDelay(attempt int, rng float64) time.Duration {
if attempt < 0 {
attempt = 0
}
base := float64(cfg.Initial)
if base <= 0 {
base = float64(2 * time.Second)
}
multiplier := cfg.Multiplier
if multiplier <= 1 {
multiplier = 2
}
delay := base * math.Pow(multiplier, float64(attempt))
if cfg.Jitter > 0 {
j := cfg.Jitter
if j > 1 {
j = 1
}
delay = delay * (1 + (rng*2-1)*j)
}
if cfg.Max > 0 && delay > float64(cfg.Max) {
delay = float64(cfg.Max)
}
return time.Duration(delay)
}