Documentation
¶
Overview ¶
Package circuit provides functionality for circuit breaking on external dependencies.
This package implements the circuit breaker pattern to protect against cascading failures when external dependencies are unavailable.
Index ¶
- func Execute[T any](ctx context.Context, cb *CircuitBreaker, operation string, ...) (T, error)
- func ExecuteWithFallback[T any](ctx context.Context, cb *CircuitBreaker, operation string, ...) (T, error)
- type CircuitBreaker
- type Config
- func (c Config) WithEnabled(enabled bool) Config
- func (c Config) WithErrorThreshold(errorThreshold float64) Config
- func (c Config) WithMaxConcurrent(maxConcurrent int) Config
- func (c Config) WithSleepWindow(sleepWindow time.Duration) Config
- func (c Config) WithTimeout(timeout time.Duration) Config
- func (c Config) WithVolumeThreshold(volumeThreshold int) Config
- type Options
- type State
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Execute ¶
func Execute[T any](ctx context.Context, cb *CircuitBreaker, operation string, fn func(ctx context.Context) (T, error)) (T, error)
Execute executes the given function with circuit breaking If the circuit is open, it will return an error immediately If the circuit is closed or half-open, it will execute the function and update the circuit state based on the result
func ExecuteWithFallback ¶
func ExecuteWithFallback[T any](ctx context.Context, cb *CircuitBreaker, operation string, fn func(ctx context.Context) (T, error), fallback func(ctx context.Context, err error) (T, error)) (T, error)
ExecuteWithFallback executes the given function with circuit breaking If the circuit is open or the function fails, it will execute the fallback function
Types ¶
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker implements the circuit breaker pattern
func NewCircuitBreaker ¶
func NewCircuitBreaker(config Config, options Options) *CircuitBreaker
NewCircuitBreaker creates a new circuit breaker
func (*CircuitBreaker) GetState ¶
func (cb *CircuitBreaker) GetState() State
GetState returns the current state of the circuit breaker
func (*CircuitBreaker) Reset ¶
func (cb *CircuitBreaker) Reset()
Reset resets the circuit breaker to its initial state
type Config ¶
type Config struct {
// Enabled determines if the circuit breaker is enabled
Enabled bool
// Timeout is the maximum time allowed for a request
Timeout time.Duration
// MaxConcurrent is the maximum number of concurrent requests allowed
MaxConcurrent int
// ErrorThreshold is the percentage of errors that will trip the circuit (0.0-1.0)
ErrorThreshold float64
// VolumeThreshold is the minimum number of requests before the error threshold is checked
VolumeThreshold int
// SleepWindow is the time to wait before allowing a single request through in half-open state
SleepWindow time.Duration
}
Config contains circuit breaker configuration parameters
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a default circuit breaker configuration
func (Config) WithEnabled ¶
WithEnabled sets whether the circuit breaker is enabled
func (Config) WithErrorThreshold ¶
WithErrorThreshold sets the percentage of errors that will trip the circuit
func (Config) WithMaxConcurrent ¶
WithMaxConcurrent sets the maximum number of concurrent requests allowed
func (Config) WithSleepWindow ¶
WithSleepWindow sets the time to wait before allowing a single request through in half-open state
func (Config) WithTimeout ¶
WithTimeout sets the maximum time allowed for a request
func (Config) WithVolumeThreshold ¶
WithVolumeThreshold sets the minimum number of requests before the error threshold is checked
type Options ¶
type Options struct {
// Logger is used for logging circuit breaker operations
Logger *logging.ContextLogger
// Tracer is used for tracing circuit breaker operations
Tracer telemetry.Tracer
// Name is the name of the circuit breaker
Name string
}
Options contains additional options for the circuit breaker
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns default options for circuit breaker operations
func (Options) WithLogger ¶
func (o Options) WithLogger(logger *logging.ContextLogger) Options
WithLogger sets the logger for the circuit breaker
type State ¶
type State int
State represents the state of the circuit breaker
const ( // Closed means the circuit is closed and requests are allowed through Closed State = iota // Open means the circuit is open and requests are not allowed through Open // HalfOpen means the circuit is allowing a limited number of requests through to test if the dependency is healthy HalfOpen )