Skip to content

Quick start

If you already use gobreaker, you only change the import.

Terminal window
go get github.com/deaprima/fluxgo@v0.2.2

Create one breaker per downstream dependency. DefaultConfig() is tuned for typical HTTP services.

breaker.go
import "github.com/deaprima/fluxgo"
cb := fluxgo.NewCircuitBreaker(fluxgo.Settings{
Name: "payment-service",
Config: fluxgo.DefaultConfig(),
OnStateChange: func(name string, from, to fluxgo.State) {
log.Printf("[%s] %s -> %s", name, from, to)
},
})
result, err := cb.Execute(func() (interface{}, error) {
return callDownstream()
})

NewCircuitBreaker panics if the config is invalid, for example when W1 + W2 is not 1.

switch {
case errors.Is(err, fluxgo.ErrOpenState):
// circuit is open: serve a fallback
case errors.Is(err, fluxgo.ErrTooManyRequests):
// half-open and the probe quota is used up
}
FieldDefaultDescription
Alpha0.3EWMA smoothing factor (0 < α ≤ 1)
W1 / W20.4 / 0.6Weights of the EWMA and percentile signals; must sum to 1
ThetaBase0.6Baseline failure-rate threshold
ThetaMin / ThetaMax0.05 / 0.9Clamp range for the adaptive threshold
WarmupDuration30sObservation period before the threshold adapts
RecoveryTimeout60sTime spent Open before moving to Half-Open
MinDwellTime5sMinimum time between state transitions
MinRequests20Requests in the window before the failure rate is evaluated
snap := cb.Snapshot()
fmt.Println(snap.State, snap.ThetaT, snap.FailureRate, snap.IsWarmingUp)

Snapshot() is the one addition to the gobreaker API. It exposes every internal signal (Sₜ, Pₜ, θₜ, degraded requests) for dashboards and debugging.