Configuration#

AutoBreaker provides flexible configuration options for different use cases.

Basic Settings#

1
2
3
4
breaker := autobreaker.New(autobreaker.Settings{
    Name:    "service-name",
    Timeout: 30 * time.Second, // Open → HalfOpen transition time
})

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#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
breaker := autobreaker.New(autobreaker.Settings{
    Name:                 "service-name",
    Timeout:              30 * time.Second,
    MaxRequests:          3,     // Concurrent requests in half-open state
    Interval:             60 * time.Second, // Counts reset every 60s (fixed window)

    // Adaptive threshold settings
    AdaptiveThreshold:    true,
    FailureRateThreshold: 0.05,  // Trip at 5% error rate
    MinimumObservations:  20,    // Minimum requests before evaluating

    // Callbacks (must be fast: <1μs)
    OnStateChange: func(name string, from, to autobreaker.State) {
        go log.Printf("%s: %v → %v", name, from, to) // Async
    },

    IsSuccessful: func(err error) bool {
        // Don't count 4xx client errors as failures
        if httpErr, ok := err.(*HTTPError); ok {
            return httpErr.Code >= 400 && httpErr.Code < 500
        }
        return err == nil
    },
})

Runtime Updates#

Update settings without restarting:

1
2
3
4
err := breaker.UpdateSettings(autobreaker.SettingsUpdate{
    FailureRateThreshold: autobreaker.Float64Ptr(0.10),
    Timeout:              autobreaker.DurationPtr(15 * time.Second),
})

Settings Reference#

SettingTypeDefaultDescription
NamestringRequiredCircuit breaker identifier
Timeouttime.Duration60 * time.SecondOpen → HalfOpen transition time
MaxRequestsuint321Concurrent requests in half-open state
Intervaltime.Duration0Fixed window: counts reset every Interval in Closed state. 0 resets only on state changes, so old successes dilute new failures
AdaptiveThresholdboolfalseEnable percentage-based thresholds; when false, trips on the 6th consecutive failure
FailureRateThresholdfloat640.05Trip at >5% error rate. Ignored unless AdaptiveThreshold is true
MinimumObservationsuint3220Minimum requests before evaluating. Ignored unless AdaptiveThreshold is true
OnStateChangefunc(string, State, State)nilState change callback
IsSuccessfulfunc(error) boolnilCustom 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.

1
2
3
4
5
6
7
8
9
// Good: Async logging
OnStateChange: func(name string, from, to State) {
    go metrics.Record(name, from, to) // Async, non-blocking
}

// Bad: Blocking I/O in callback
OnStateChange: func(name string, from, to State) {
    db.SaveStateChange(name, from, to) // Blocks!
}

Best Practices#

  1. Use descriptive names - Helps with monitoring and debugging
  2. Set reasonable timeouts - Match your service’s typical recovery time
  3. Monitor failure rates - Adjust thresholds based on observed behavior
  4. Use async callbacks - Keep the hot path fast
  5. Test with examples - See examples for production patterns