Documentation
¶
Overview ¶
Package circuit provides circuit breaker implementation for resilient service calls. It implements the circuit breaker pattern to prevent cascading failures.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetGlobalManager ¶
func SetGlobalManager(manager *Manager)
SetGlobalManager sets the global circuit breaker manager.
Types ¶
type Breaker ¶
type Breaker interface {
// Execute executes the given request with circuit breaker protection
Execute(ctx context.Context, req func() (interface{}, error)) (interface{}, error)
// ExecuteWithFallback executes with fallback on circuit open
ExecuteWithFallback(ctx context.Context, req func() (interface{}, error), fallback func(error) (interface{}, error)) (interface{}, error)
// State returns the current circuit breaker state
State() State
// Counts returns the current circuit breaker counts
Counts() Counts
// Name returns the circuit breaker name
Name() string
}
Breaker defines the interface for circuit breaker functionality.
func GetBreaker ¶
GetBreaker returns a circuit breaker from the global manager.
type BulkheadPattern ¶
type BulkheadPattern struct {
// contains filtered or unexported fields
}
BulkheadPattern provides bulkhead isolation for different service calls.
func NewBulkheadPattern ¶
func NewBulkheadPattern(manager *Manager) *BulkheadPattern
NewBulkheadPattern creates a new bulkhead pattern implementation.
type Counts ¶
type Counts struct {
Requests uint32 `json:"requests"`
TotalSuccesses uint32 `json:"total_successes"`
TotalFailures uint32 `json:"total_failures"`
ConsecutiveSuccesses uint32 `json:"consecutive_successes"`
ConsecutiveFailures uint32 `json:"consecutive_failures"`
}
Counts represents circuit breaker statistics.
type HTTPClient ¶
HTTPClient defines the interface for HTTP clients.
func WrapHTTPClient ¶
func WrapHTTPClient(breaker Breaker, client HTTPClient) HTTPClient
WrapHTTPClient wraps an HTTP client with circuit breaker protection.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager manages multiple circuit breakers.
func GetGlobalManager ¶
func GetGlobalManager() *Manager
GetGlobalManager returns the global circuit breaker manager.
func NewManager ¶
func NewManager(cfg config.CircuitBreakerConfig, logger *zap.Logger, metrics MetricsCollector) *Manager
NewManager creates a new circuit breaker manager.
func (*Manager) GetAllBreakers ¶
GetAllBreakers returns all registered circuit breakers.
func (*Manager) GetBreaker ¶
GetBreaker returns a circuit breaker by name, creating it if it doesn't exist.
func (*Manager) HealthCheck ¶
HealthCheck performs health checks on all circuit breakers.
func (*Manager) ResetAllBreakers ¶
func (m *Manager) ResetAllBreakers()
ResetAllBreakers resets all circuit breakers.
type MetricsCollector ¶
type MetricsCollector interface {
RecordStateChange(name string, from, to State)
RecordRequest(name string, success bool, duration time.Duration)
RecordFallback(name string)
}
MetricsCollector defines the interface for collecting circuit breaker metrics.
type NoOpMetricsCollector ¶
type NoOpMetricsCollector struct{}
NoOpMetricsCollector is a no-op implementation of MetricsCollector.
func (*NoOpMetricsCollector) RecordFallback ¶
func (n *NoOpMetricsCollector) RecordFallback(name string)
func (*NoOpMetricsCollector) RecordRequest ¶
func (n *NoOpMetricsCollector) RecordRequest(name string, success bool, duration time.Duration)
func (*NoOpMetricsCollector) RecordStateChange ¶
func (n *NoOpMetricsCollector) RecordStateChange(name string, from, to State)