Configuration#
AutoBreaker provides flexible configuration options for different use cases.
Basic Settings#
| |
With these settings the breaker trips on the 6th consecutive failure (static threshold, the default). For percentage-based tripping, set AdaptiveThreshold: true and an Interval, as in the advanced configuration below; the rate then defaults to 5% over at least 20 requests.
Advanced Configuration#
| |
Runtime Updates#
Update settings without restarting:
| |
Settings Reference#
| Setting | Type | Default | Description |
|---|---|---|---|
Name | string | Required | Circuit breaker identifier |
Timeout | time.Duration | 60 * time.Second | Open → HalfOpen transition time |
MaxRequests | uint32 | 1 | Concurrent requests in half-open state |
Interval | time.Duration | 0 | Fixed window: counts reset every Interval in Closed state. 0 resets only on state changes, so old successes dilute new failures |
AdaptiveThreshold | bool | false | Enable percentage-based thresholds; when false, trips on the 6th consecutive failure |
FailureRateThreshold | float64 | 0.05 | Trip at >5% error rate. Ignored unless AdaptiveThreshold is true |
MinimumObservations | uint32 | 20 | Minimum requests before evaluating. Ignored unless AdaptiveThreshold is true |
OnStateChange | func(string, State, State) | nil | State change callback |
IsSuccessful | func(error) bool | nil | Custom error classification |
Diagnostics().RateSettingsIgnored is true when FailureRateThreshold or MinimumObservations is set (in Settings or through UpdateSettings) but has no effect: AdaptiveThreshold is false, or a custom ReadyToTrip replaces the adaptive rule.
Performance Considerations#
Callback Performance: Callbacks run synchronously inside Execute(): IsSuccessful on each completed request, ReadyToTrip after each failed request in Closed state, OnStateChange on every state change. Keep them <1μs.
| |
Best Practices#
- Use descriptive names - Helps with monitoring and debugging
- Set reasonable timeouts - Match your service’s typical recovery time
- Monitor failure rates - Adjust thresholds based on observed behavior
- Use async callbacks - Keep the hot path fast
- Test with examples - See examples for production patterns