Documentation
¶
Index ¶
Constants ¶
View Source
const (
// NumBuckets is the number of buckets used for time-based sliding window.
NumBuckets = 10
)
Variables ¶
View Source
var ( // ErrCircuitOpen is returned when the circuit breaker is in the Open state. ErrCircuitOpen = errors.New("circuit breaker is open") )
Functions ¶
This section is empty.
Types ¶
type Breaker ¶
type Breaker struct {
// contains filtered or unexported fields
}
Breaker implements a circuit breaker state machine with sliding windows.
func (*Breaker) Health ¶ added in v1.0.6
func (b *Breaker) Health() <-chan StateEvent
Health returns a channel that emits state change events. The channel is created lazily on first access. The caller should use a select with default to handle non-blocking receive.
type Config ¶
type Config struct {
// WindowType specifies the type of sliding window.
WindowType WindowType
// WindowSize is the size of the count-based sliding window.
// Default is 100.
WindowSize uint32
// WindowDuration is the duration of the time-based sliding window.
// Default is 60 seconds.
WindowDuration time.Duration
// FailureRateThreshold is the failure rate percentage (0.0 to 100.0).
// If failures/total >= FailureRateThreshold/100, the circuit opens.
// Default is 50.0.
FailureRateThreshold float64
// MinimumCalls is the minimum number of calls in the current window before the failure rate is calculated.
// Default is 10.
MinimumCalls uint64
// ResetTimeout is the duration to wait in Open state before moving to Half-Open.
// Default is 60 seconds.
ResetTimeout time.Duration
// HalfOpenMaxCalls is the number of calls allowed in Half-Open state.
// Default is 10.
HalfOpenMaxCalls uint64
}
Config defines the configuration for the circuit breaker.
type HealthState ¶ added in v1.0.6
type HealthState int
State represents the health state of a resilience component.
const ( // StateHealthy represents a healthy state where requests are allowed. HealthStateHealthy HealthState = iota // StateDegraded represents a degraded but functional state. HealthStateDegraded // StateUnhealthy represents an unhealthy state (e.g., circuit open). HealthStateUnhealthy )
type StateEvent ¶ added in v1.0.6
type StateEvent struct {
Component string
State HealthState
Timestamp time.Time
Message string
}
StateEvent represents a state change event emitted by resilience components.
type WindowType ¶ added in v1.0.4
type WindowType int
WindowType specifies whether the window is count-based or time-based.
const ( // WindowCountBased uses the last N calls to calculate failure rate. WindowCountBased WindowType = iota // WindowTimeBased uses calls in the last N duration to calculate failure rate. WindowTimeBased )
Click to show internal directories.
Click to hide internal directories.