circuit

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2025 License: MIT Imports: 9 Imported by: 0

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.

func WrapDatabaseCall

func WrapDatabaseCall(breaker Breaker, operation string, fn func() error) error

WrapDatabaseCall wraps database calls with circuit breaker protection.

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

func GetBreaker(name string) Breaker

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.

func (*BulkheadPattern) Execute

func (bp *BulkheadPattern) Execute(bulkhead string, ctx context.Context, req func() (interface{}, error)) (interface{}, error)

Execute executes a request in the specified bulkhead.

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

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

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

func (m *Manager) GetAllBreakers() map[string]Breaker

GetAllBreakers returns all registered circuit breakers.

func (*Manager) GetBreaker

func (m *Manager) GetBreaker(name string) Breaker

GetBreaker returns a circuit breaker by name, creating it if it doesn't exist.

func (*Manager) HealthCheck

func (m *Manager) HealthCheck() map[string]interface{}

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)

type State

type State int

State represents circuit breaker states.

const (
	StateClosed State = iota
	StateHalfOpen
	StateOpen
)

func (State) String

func (s State) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL