resilience

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BackoffStrategy

type BackoffStrategy int

BackoffStrategy represents the backoff strategy

const (
	BackoffStrategyFixed BackoffStrategy = iota
	BackoffStrategyLinear
	BackoffStrategyExponential
	BackoffStrategyFibonacci
)

func (BackoffStrategy) String

func (s BackoffStrategy) String() string

type CacheFallbackHandler

type CacheFallbackHandler struct {
	// contains filtered or unexported fields
}

CacheFallbackHandler provides cache-based fallback

func NewCacheFallbackHandler

func NewCacheFallbackHandler(name string, priority int) *CacheFallbackHandler

func (*CacheFallbackHandler) Execute

func (h *CacheFallbackHandler) Execute(ctx context.Context, request interface{}) (interface{}, error)

func (*CacheFallbackHandler) GetName

func (h *CacheFallbackHandler) GetName() string

func (*CacheFallbackHandler) GetPriority

func (h *CacheFallbackHandler) GetPriority() int

func (*CacheFallbackHandler) IsAvailable

func (h *CacheFallbackHandler) IsAvailable(ctx context.Context) bool

type CircuitBreaker

type CircuitBreaker struct {
	// contains filtered or unexported fields
}

CircuitBreaker provides circuit breaker functionality

func NewCircuitBreaker

func NewCircuitBreaker(config CircuitBreakerConfig) *CircuitBreaker

NewCircuitBreaker creates a new circuit breaker

func (*CircuitBreaker) Execute

func (cb *CircuitBreaker) Execute(ctx context.Context, fn func() (interface{}, error)) (interface{}, error)

Execute executes a function with circuit breaker protection

func (*CircuitBreaker) GetConfig

func (cb *CircuitBreaker) GetConfig() CircuitBreakerConfig

GetConfig returns the circuit breaker configuration

func (*CircuitBreaker) GetState

func (cb *CircuitBreaker) GetState() CircuitState

GetState returns the current circuit breaker state

func (*CircuitBreaker) GetStats

func (cb *CircuitBreaker) GetStats() CircuitStats

GetStats returns the circuit breaker statistics

func (*CircuitBreaker) IsClosed

func (cb *CircuitBreaker) IsClosed() bool

IsClosed returns true if the circuit is closed

func (*CircuitBreaker) IsHalfOpen

func (cb *CircuitBreaker) IsHalfOpen() bool

IsHalfOpen returns true if the circuit is half-open

func (*CircuitBreaker) IsOpen

func (cb *CircuitBreaker) IsOpen() bool

IsOpen returns true if the circuit is open

func (*CircuitBreaker) Reset

func (cb *CircuitBreaker) Reset()

Reset resets the circuit breaker to closed state

func (*CircuitBreaker) UpdateConfig

func (cb *CircuitBreaker) UpdateConfig(config CircuitBreakerConfig)

UpdateConfig updates the circuit breaker configuration

type CircuitBreakerConfig

type CircuitBreakerConfig struct {
	Name                string         `yaml:"name"`
	MaxRequests         int            `yaml:"max_requests" default:"10"`
	Timeout             time.Duration  `yaml:"timeout" default:"60s"`
	MaxFailures         int            `yaml:"max_failures" default:"5"`
	FailureThreshold    float64        `yaml:"failure_threshold" default:"0.5"`
	SuccessThreshold    float64        `yaml:"success_threshold" default:"0.8"`
	RecoveryTimeout     time.Duration  `yaml:"recovery_timeout" default:"30s"`
	HalfOpenMaxRequests int            `yaml:"half_open_max_requests" default:"3"`
	EnableMetrics       bool           `yaml:"enable_metrics" default:"true"`
	Logger              logger.Logger  `yaml:"-"`
	Metrics             shared.Metrics `yaml:"-"`
}

CircuitBreakerConfig contains circuit breaker configuration

type CircuitBreakerError

type CircuitBreakerError struct {
	State   CircuitState `json:"state"`
	Message string       `json:"message"`
	Time    time.Time    `json:"time"`
}

CircuitBreakerError represents a circuit breaker error

func (*CircuitBreakerError) Error

func (e *CircuitBreakerError) Error() string

type CircuitState

type CircuitState int

CircuitState represents the state of a circuit breaker

const (
	CircuitStateClosed CircuitState = iota
	CircuitStateOpen
	CircuitStateHalfOpen
)

func (CircuitState) String

func (s CircuitState) String() string

type CircuitStats

type CircuitStats struct {
	TotalRequests      int64     `json:"total_requests"`
	SuccessfulRequests int64     `json:"successful_requests"`
	FailedRequests     int64     `json:"failed_requests"`
	StateChanges       int64     `json:"state_changes"`
	LastStateChange    time.Time `json:"last_state_change"`
	LastFailure        time.Time `json:"last_failure"`
	LastSuccess        time.Time `json:"last_success"`
}

CircuitStats represents circuit breaker statistics

type DegradationConfig

type DegradationConfig struct {
	Name                   string         `yaml:"name"`
	EnableFallbacks        bool           `yaml:"enable_fallbacks" default:"true"`
	FallbackTimeout        time.Duration  `yaml:"fallback_timeout" default:"5s"`
	EnableCircuitBreaker   bool           `yaml:"enable_circuit_breaker" default:"true"`
	EnableRetry            bool           `yaml:"enable_retry" default:"true"`
	MaxConcurrentFallbacks int            `yaml:"max_concurrent_fallbacks" default:"10"`
	EnableMetrics          bool           `yaml:"enable_metrics" default:"true"`
	Logger                 logger.Logger  `yaml:"-"`
	Metrics                shared.Metrics `yaml:"-"`
}

DegradationConfig contains graceful degradation configuration

type DegradationResult

type DegradationResult struct {
	Success      bool          `json:"success"`
	Result       interface{}   `json:"result,omitempty"`
	Error        error         `json:"error,omitempty"`
	Handler      string        `json:"handler"`
	FallbackUsed bool          `json:"fallback_used"`
	Duration     time.Duration `json:"duration"`
	Attempts     int           `json:"attempts"`
}

DegradationResult represents the result of graceful degradation

type DegradationStats

type DegradationStats struct {
	TotalRequests      int64     `json:"total_requests"`
	SuccessfulRequests int64     `json:"successful_requests"`
	FailedRequests     int64     `json:"failed_requests"`
	FallbackRequests   int64     `json:"fallback_requests"`
	LastRequest        time.Time `json:"last_request"`
	LastSuccess        time.Time `json:"last_success"`
	LastFailure        time.Time `json:"last_failure"`
}

DegradationStats represents degradation statistics

type FallbackHandler

type FallbackHandler interface {
	Execute(ctx context.Context, request interface{}) (interface{}, error)
	GetName() string
	GetPriority() int
	IsAvailable(ctx context.Context) bool
}

FallbackHandler represents a fallback handler

type GracefulDegradation

type GracefulDegradation struct {
	// contains filtered or unexported fields
}

GracefulDegradation provides graceful degradation functionality

func NewGracefulDegradation

func NewGracefulDegradation(config DegradationConfig) *GracefulDegradation

NewGracefulDegradation creates a new graceful degradation instance

func (*GracefulDegradation) Execute

func (gd *GracefulDegradation) Execute(ctx context.Context, primaryHandler FallbackHandler, request interface{}) (*DegradationResult, error)

Execute executes a function with graceful degradation

func (*GracefulDegradation) GetConfig

func (gd *GracefulDegradation) GetConfig() DegradationConfig

GetConfig returns the degradation configuration

func (*GracefulDegradation) GetFallback

func (gd *GracefulDegradation) GetFallback(name string) (FallbackHandler, error)

GetFallback returns a fallback handler by name

func (*GracefulDegradation) GetStats

func (gd *GracefulDegradation) GetStats() DegradationStats

GetStats returns the degradation statistics

func (*GracefulDegradation) ListFallbacks

func (gd *GracefulDegradation) ListFallbacks() []FallbackHandler

ListFallbacks returns all registered fallback handlers

func (*GracefulDegradation) RegisterFallback

func (gd *GracefulDegradation) RegisterFallback(handler FallbackHandler)

RegisterFallback registers a fallback handler

func (*GracefulDegradation) Reset

func (gd *GracefulDegradation) Reset()

Reset resets the degradation statistics

func (*GracefulDegradation) UnregisterFallback

func (gd *GracefulDegradation) UnregisterFallback(name string)

UnregisterFallback unregisters a fallback handler

func (*GracefulDegradation) UpdateConfig

func (gd *GracefulDegradation) UpdateConfig(config DegradationConfig)

UpdateConfig updates the degradation configuration

type GracefulDegradationStats

type GracefulDegradationStats struct {
	TotalRequests      int64
	SuccessfulRequests int64
	FailedRequests     int64
	FallbackRequests   int64
	LastRequest        time.Time
}

DegradationStats contains degradation statistics

type Retry

type Retry struct {
	// contains filtered or unexported fields
}

Retry provides retry functionality with various backoff strategies

func NewRetry

func NewRetry(config RetryConfig) *Retry

NewRetry creates a new retry instance

func (*Retry) Execute

func (r *Retry) Execute(ctx context.Context, fn func() (interface{}, error)) (interface{}, error)

Execute executes a function with retry logic

func (*Retry) GetConfig

func (r *Retry) GetConfig() RetryConfig

GetConfig returns the retry configuration

func (*Retry) GetStats

func (r *Retry) GetStats() RetryStats

GetStats returns the retry statistics

func (*Retry) Reset

func (r *Retry) Reset()

Reset resets the retry statistics

func (*Retry) UpdateConfig

func (r *Retry) UpdateConfig(config RetryConfig)

UpdateConfig updates the retry configuration

type RetryConfig

type RetryConfig struct {
	Name               string          `yaml:"name"`
	MaxAttempts        int             `yaml:"max_attempts" default:"3"`
	InitialDelay       time.Duration   `yaml:"initial_delay" default:"100ms"`
	MaxDelay           time.Duration   `yaml:"max_delay" default:"30s"`
	Multiplier         float64         `yaml:"multiplier" default:"2.0"`
	Jitter             bool            `yaml:"jitter" default:"true"`
	BackoffStrategy    BackoffStrategy `yaml:"backoff_strategy" default:"exponential"`
	RetryableErrors    []string        `yaml:"retryable_errors"`
	NonRetryableErrors []string        `yaml:"non_retryable_errors"`
	EnableMetrics      bool            `yaml:"enable_metrics" default:"true"`
	Logger             logger.Logger   `yaml:"-"`
	Metrics            shared.Metrics  `yaml:"-"`
}

RetryConfig contains retry configuration

type RetryError

type RetryError struct {
	Attempts   int           `json:"attempts"`
	TotalDelay time.Duration `json:"total_delay"`
	LastError  error         `json:"last_error"`
	AllErrors  []error       `json:"all_errors"`
	Time       time.Time     `json:"time"`
}

RetryError represents a retry error

func (*RetryError) Error

func (e *RetryError) Error() string

type RetryStats

type RetryStats struct {
	TotalAttempts     int64         `json:"total_attempts"`
	SuccessfulRetries int64         `json:"successful_retries"`
	FailedRetries     int64         `json:"failed_retries"`
	TotalDelay        time.Duration `json:"total_delay"`
	LastAttempt       time.Time     `json:"last_attempt"`
	LastSuccess       time.Time     `json:"last_success"`
	LastFailure       time.Time     `json:"last_failure"`
}

RetryStats represents retry statistics

type StaticFallbackHandler

type StaticFallbackHandler struct {
	// contains filtered or unexported fields
}

StaticFallbackHandler provides static response fallback

func NewStaticFallbackHandler

func NewStaticFallbackHandler(name string, priority int, response interface{}) *StaticFallbackHandler

func (*StaticFallbackHandler) Execute

func (h *StaticFallbackHandler) Execute(ctx context.Context, request interface{}) (interface{}, error)

func (*StaticFallbackHandler) GetName

func (h *StaticFallbackHandler) GetName() string

func (*StaticFallbackHandler) GetPriority

func (h *StaticFallbackHandler) GetPriority() int

func (*StaticFallbackHandler) IsAvailable

func (h *StaticFallbackHandler) IsAvailable(ctx context.Context) bool

Jump to

Keyboard shortcuts

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