Migration from sony/gobreaker#
AutoBreaker follows the sony/gobreaker v1 API shape. Most code moves over by changing the import and the constructor; the differences are listed below.
Quick Migration#
Replace the import and constructor:
| |
API Compatibility#
Same as gobreaker v1#
Execute(func() (interface{}, error)),Name(),State(),Counts()ErrOpenState,ErrTooManyRequestsCountsfields and typesSettingsfield names and types (MaxRequestsbehaves differently, below)- State constants
StateClosed,StateOpen,StateHalfOpen
Different#
- Constructor:
Newinstead ofNewCircuitBreaker - State values:
StateOpenis 1 andStateHalfOpenis 2; gobreaker numbers them the other way round. Compare against the constants, never stored integers MaxRequests: caps concurrent HalfOpen probes, and the first successful probe closes the breaker. In gobreaker it caps total HalfOpen requests and the breaker closes afterMaxRequestsconsecutive successes, so a breaker withMaxRequests: 3recovers sooner here- No
TwoStepCircuitBreaker
Enhanced Features#
- Adaptive thresholds (percentage-based, opt-in with
AdaptiveThreshold: true) - Runtime updates via
UpdateSettings() - Better observability with
Metrics()andDiagnostics() - Callback safety with panic recovery
Settings Mapping#
| sony/gobreaker | AutoBreaker | Notes |
|---|---|---|
Name | Name | Same |
MaxRequests | MaxRequests | Different: concurrent probe limit; first success closes |
Interval | Interval | Same |
Timeout | Timeout | Same |
ReadyToTrip | ReadyToTrip | Enhanced with adaptive logic |
OnStateChange | OnStateChange | Same, plus panic recovery |
IsSuccessful | IsSuccessful | Same |
| - | AdaptiveThreshold | New: Enable percentage-based thresholds |
| - | FailureRateThreshold | New: Trip at >X% error rate |
| - | MinimumObservations | New: Minimum requests before evaluating |
Behavior Differences#
1. Adaptive Thresholds (Opt-in)#
By default both libraries trip on ConsecutiveFailures > 5.
With AdaptiveThreshold: true, AutoBreaker trips on (TotalFailures / Requests) > FailureRateThreshold (default 0.05) once Requests >= MinimumObservations (default 20):
| |
2. Callback Safety#
AutoBreaker wraps callbacks in panic recovery. If your callback panics, AutoBreaker continues working.
3. Runtime Updates#
AutoBreaker supports updating settings without recreating the breaker.
Migration Checklist#
- Update imports
- Update constructor (
NewCircuitBreaker→New) - Review settings (enable/disable adaptive thresholds as needed)
- Test thoroughly (behavior differs once adaptive thresholds are on)
- Consider enhancements (runtime updates, better observability)
Example Migration#
| |
Benefits of Migration#
- Adaptive thresholds - One threshold across traffic levels (each window needs
MinimumObservationsrequests) - Runtime updates - Change settings without restart
- Better observability - More metrics and diagnostics
- Callback safety - Panics won’t break the circuit
- Active maintenance - Regular updates and improvements
Need Help?#
- GitHub Issues: https://github.com/1mb-dev/autobreaker/issues
- Examples: See examples/