resilience

package
v0.7.4 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2025 License: Apache-2.0 Imports: 9 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 any) (any, 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() (any, error)) (any, 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            `default:"10"   yaml:"max_requests"`
	Timeout             time.Duration  `default:"60s"  yaml:"timeout"`
	MaxFailures         int            `default:"5"    yaml:"max_failures"`
	FailureThreshold    float64        `default:"0.5"  yaml:"failure_threshold"`
	SuccessThreshold    float64        `default:"0.8"  yaml:"success_threshold"`
	RecoveryTimeout     time.Duration  `default:"30s"  yaml:"recovery_timeout"`
	HalfOpenMaxRequests int            `default:"3"    yaml:"half_open_max_requests"`
	EnableMetrics       bool           `default:"true" yaml:"enable_metrics"`
	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           `default:"true" yaml:"enable_fallbacks"`
	FallbackTimeout        time.Duration  `default:"5s"   yaml:"fallback_timeout"`
	EnableCircuitBreaker   bool           `default:"true" yaml:"enable_circuit_breaker"`
	EnableRetry            bool           `default:"true" yaml:"enable_retry"`
	MaxConcurrentFallbacks int            `default:"10"   yaml:"max_concurrent_fallbacks"`
	EnableMetrics          bool           `default:"true" yaml:"enable_metrics"`
	Logger                 logger.Logger  `yaml:"-"`
	Metrics                shared.Metrics `yaml:"-"`
}

DegradationConfig contains graceful degradation configuration.

type DegradationResult

type DegradationResult struct {
	Success      bool          `json:"success"`
	Result       any           `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 any) (any, 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 any) (*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() (any, error)) (any, 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             `default:"3"                 yaml:"max_attempts"`
	InitialDelay       time.Duration   `default:"100ms"             yaml:"initial_delay"`
	MaxDelay           time.Duration   `default:"30s"               yaml:"max_delay"`
	Multiplier         float64         `default:"2.0"               yaml:"multiplier"`
	Jitter             bool            `default:"true"              yaml:"jitter"`
	BackoffStrategy    BackoffStrategy `default:"exponential"       yaml:"backoff_strategy"`
	RetryableErrors    []string        `yaml:"retryable_errors"`
	NonRetryableErrors []string        `yaml:"non_retryable_errors"`
	EnableMetrics      bool            `default:"true"              yaml:"enable_metrics"`
	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 any) *StaticFallbackHandler

func (*StaticFallbackHandler) Execute

func (h *StaticFallbackHandler) Execute(ctx context.Context, request any) (any, 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