Documentation
¶
Overview ¶
Package circuitbreaker implements the circuit breaker pattern
This package provides a circuit breaker implementation to protect services from overloading and to improve system resilience by temporarily stopping requests to failing services.
The circuit breaker has three states: - Closed: All requests are allowed - Open: No requests are allowed, returns an error immediately - Half-Open: A limited number of requests are allowed to test if the service has recovered
Example:
cb := circuitbreaker.New(circuitbreaker.DefaultConfig())
err := cb.Execute(ctx, func() error {
// Call external service
return externalService.Call()
})
if err != nil {
// Handle error (may be circuit breaker error)
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker implements the circuit breaker pattern
The circuit breaker protects services from overloading by temporarily stopping requests to failing services. It has three states: - Closed: All requests are allowed - Open: No requests are allowed, returns an error immediately - Half-Open: A limited number of requests are allowed to test if the service has recovered
Example:
cb := New(DefaultConfig())
err := cb.Execute(ctx, func() error {
// Call external service
return externalService.Call()
})
if err != nil {
// Handle error (may be circuit breaker error)
}
func New ¶
func New(config Config) *CircuitBreaker
New creates a new circuit breaker
Parameters: - config: Configuration for the circuit breaker
Returns: - *CircuitBreaker: New circuit breaker instance
func (*CircuitBreaker) Execute ¶
func (cb *CircuitBreaker) Execute(ctx context.Context, fn func() error) error
Execute executes the given function with circuit breaker protection
This method wraps the execution of a function with circuit breaker logic: 1. Checks if execution is allowed based on the current state 2. Executes the function 3. Records the result and updates the circuit breaker state
Parameters: - ctx: Context for cancellation - fn: Function to execute
Returns: - error: Error from the function or circuit breaker error
func (*CircuitBreaker) Reset ¶
func (cb *CircuitBreaker) Reset()
Reset resets the circuit breaker to closed state
This method resets the circuit breaker to its initial closed state, clearing all failure and success counters.
func (*CircuitBreaker) State ¶
func (cb *CircuitBreaker) State() State
State returns the current state
Returns: - State: Current circuit breaker state (Closed, Open, or HalfOpen)
type Config ¶
Config configures the circuit breaker
This struct defines the configuration parameters for the circuit breaker, including failure threshold, timeout, and half-open state limits.
Example:
config := Config{
MaxFailures: 10, // Open circuit after 10 failures
Timeout: 1 * time.Minute, // Wait 1 minute before trying again
HalfOpenMax: 5, // Allow 5 requests in half-open state
}
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a default configuration
Returns a configuration with sensible defaults: - MaxFailures: 5 - Timeout: 30 seconds - HalfOpenMax: 3