Documentation
¶
Overview ¶
Package resilience provides reliability patterns for tako-cli operations. This file provides a circuit breaker wrapper using sony/gobreaker.
Index ¶
- Variables
- func DefaultRetryClassifier(err error) bool
- func ExecuteWithResult[T any](b *ServiceBreaker, fn func() (T, error)) (T, error)
- func IsRetryable(err error) bool
- func PermanentError(err error) error
- func Retry(ctx context.Context, config RetryConfig, operation func() error) error
- func RetryWithBackoff(ctx context.Context, operation func() error, opts ...BackoffRetryOption) error
- type BackoffRetryOption
- func WithInitialDelay(d time.Duration) BackoffRetryOption
- func WithMaxDelay(d time.Duration) BackoffRetryOption
- func WithMaxElapsed(d time.Duration) BackoffRetryOption
- func WithMaxRetries(n uint64) BackoffRetryOption
- func WithMultiplier(m float64) BackoffRetryOption
- func WithOnRetry(fn func(err error, duration time.Duration)) BackoffRetryOption
- func WithRetryClassifier(fn func(error) bool) BackoffRetryOption
- type BreakerOption
- type CircuitBreaker
- func (cb *CircuitBreaker) AllowRequest() bool
- func (cb *CircuitBreaker) Execute(operation func() error) error
- func (cb *CircuitBreaker) GetFailureCount() int
- func (cb *CircuitBreaker) GetState() CircuitState
- func (cb *CircuitBreaker) RecordFailure()
- func (cb *CircuitBreaker) RecordSuccess()
- func (cb *CircuitBreaker) Reset()
- type CircuitState
- type RetryConfig
- type RetryableOperation
- type ServiceBreaker
- func (b *ServiceBreaker) Counts() gobreaker.Counts
- func (b *ServiceBreaker) Execute(fn func() error) error
- func (b *ServiceBreaker) IsClosed() bool
- func (b *ServiceBreaker) IsHalfOpen() bool
- func (b *ServiceBreaker) IsOpen() bool
- func (b *ServiceBreaker) Name() string
- func (b *ServiceBreaker) State() string
Constants ¶
This section is empty.
Variables ¶
var ErrCircuitOpen = fmt.Errorf("circuit breaker is open")
ErrCircuitOpen is returned when the circuit breaker is open
Functions ¶
func DefaultRetryClassifier ¶ added in v0.1.4
DefaultRetryClassifier determines if an error is retryable. Network errors and timeouts are retryable; auth and validation errors are not.
func ExecuteWithResult ¶ added in v0.1.4
func ExecuteWithResult[T any](b *ServiceBreaker, fn func() (T, error)) (T, error)
ExecuteWithResult runs an operation that returns a result through the circuit breaker.
func IsRetryable ¶ added in v0.1.4
IsRetryable checks if an error should be retried
func PermanentError ¶ added in v0.1.4
PermanentError wraps an error to indicate it should not be retried
func Retry ¶
func Retry(ctx context.Context, config RetryConfig, operation func() error) error
Retry executes an operation with exponential backoff
func RetryWithBackoff ¶ added in v0.1.4
func RetryWithBackoff(ctx context.Context, operation func() error, opts ...BackoffRetryOption) error
RetryWithBackoff provides advanced retry with exponential backoff using cenkalti/backoff. This is more robust than the basic Retry function and supports: - Maximum elapsed time limit - Maximum retry count - Customizable backoff parameters - Retry classification (determine if error is retryable) - Context cancellation
Types ¶
type BackoffRetryOption ¶ added in v0.1.4
type BackoffRetryOption func(*backoffConfig)
BackoffRetryOption configures backoff retry behavior
func WithInitialDelay ¶ added in v0.1.4
func WithInitialDelay(d time.Duration) BackoffRetryOption
WithInitialDelay sets the initial delay between retries
func WithMaxDelay ¶ added in v0.1.4
func WithMaxDelay(d time.Duration) BackoffRetryOption
WithMaxDelay sets the maximum delay between retries
func WithMaxElapsed ¶ added in v0.1.4
func WithMaxElapsed(d time.Duration) BackoffRetryOption
WithMaxElapsed sets the maximum total time for retries
func WithMaxRetries ¶ added in v0.1.4
func WithMaxRetries(n uint64) BackoffRetryOption
WithMaxRetries sets the maximum number of retry attempts
func WithMultiplier ¶ added in v0.1.4
func WithMultiplier(m float64) BackoffRetryOption
WithMultiplier sets the backoff multiplier
func WithOnRetry ¶ added in v0.1.4
func WithOnRetry(fn func(err error, duration time.Duration)) BackoffRetryOption
WithOnRetry sets a callback for each retry attempt
func WithRetryClassifier ¶ added in v0.1.4
func WithRetryClassifier(fn func(error) bool) BackoffRetryOption
WithRetryClassifier sets a function to determine if an error is retryable
type BreakerOption ¶ added in v0.1.4
BreakerOption configures a ServiceBreaker
func WithFailureThreshold ¶ added in v0.1.4
func WithFailureThreshold(n uint32) BreakerOption
WithFailureThreshold sets the number of consecutive failures before opening
func WithInterval ¶ added in v0.1.4
func WithInterval(d time.Duration) BreakerOption
WithInterval sets the cyclic period of the closed state for clearing counts
func WithMaxRequests ¶ added in v0.1.4
func WithMaxRequests(n uint32) BreakerOption
WithMaxRequests sets the maximum number of requests allowed in half-open state
func WithOnStateChange ¶ added in v0.1.4
func WithOnStateChange(fn func(name string, from, to string)) BreakerOption
WithOnStateChange sets a callback for state changes
func WithTimeout ¶ added in v0.1.4
func WithTimeout(d time.Duration) BreakerOption
WithTimeout sets the period of the open state before becoming half-open
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker prevents cascading failures by monitoring errors
func NewCircuitBreaker ¶
func NewCircuitBreaker(name string, failureThreshold int, resetTimeout time.Duration) *CircuitBreaker
NewCircuitBreaker creates a new circuit breaker
func (*CircuitBreaker) AllowRequest ¶
func (cb *CircuitBreaker) AllowRequest() bool
AllowRequest checks if a request is allowed through the circuit breaker
func (*CircuitBreaker) Execute ¶
func (cb *CircuitBreaker) Execute(operation func() error) error
Execute runs an operation through the circuit breaker
func (*CircuitBreaker) GetFailureCount ¶
func (cb *CircuitBreaker) GetFailureCount() int
GetFailureCount returns the current failure count
func (*CircuitBreaker) GetState ¶
func (cb *CircuitBreaker) GetState() CircuitState
GetState returns the current state of the circuit breaker
func (*CircuitBreaker) RecordFailure ¶
func (cb *CircuitBreaker) RecordFailure()
RecordFailure records a failed operation
func (*CircuitBreaker) RecordSuccess ¶
func (cb *CircuitBreaker) RecordSuccess()
RecordSuccess records a successful operation
func (*CircuitBreaker) Reset ¶
func (cb *CircuitBreaker) Reset()
Reset resets the circuit breaker to closed state
type CircuitState ¶
type CircuitState int
CircuitState represents the state of the circuit breaker
const ( // CircuitClosed allows requests through CircuitClosed CircuitState = iota // CircuitOpen blocks requests CircuitOpen // CircuitHalfOpen allows limited requests to test recovery CircuitHalfOpen )
func (CircuitState) String ¶
func (s CircuitState) String() string
String returns the string representation of the state
type RetryConfig ¶
type RetryConfig struct {
MaxAttempts int
InitialDelay time.Duration
MaxDelay time.Duration
BackoffFactor float64
JitterFactor float64
}
RetryConfig holds configuration for retry behavior
func DefaultRetryConfig ¶
func DefaultRetryConfig() RetryConfig
DefaultRetryConfig returns sensible default retry configuration
type RetryableOperation ¶
type RetryableOperation struct {
Name string
Operation func() error
Config RetryConfig
OnRetry func(attempt int, err error)
}
RetryableOperation wraps an operation with retry logic
type ServiceBreaker ¶ added in v0.1.4
type ServiceBreaker struct {
// contains filtered or unexported fields
}
ServiceBreaker wraps gobreaker with tako-cli defaults and provides a simpler API for common use cases.
func GetDeployBreaker ¶ added in v0.1.4
func GetDeployBreaker() *ServiceBreaker
GetDeployBreaker returns the shared deployment circuit breaker
func GetHTTPBreaker ¶ added in v0.1.4
func GetHTTPBreaker() *ServiceBreaker
GetHTTPBreaker returns the shared HTTP circuit breaker
func GetSSHBreaker ¶ added in v0.1.4
func GetSSHBreaker() *ServiceBreaker
GetSSHBreaker returns the shared SSH circuit breaker
func NewServiceBreaker ¶ added in v0.1.4
func NewServiceBreaker(name string, opts ...BreakerOption) *ServiceBreaker
NewServiceBreaker creates a new circuit breaker with sensible defaults for tako-cli. Default settings: - MaxRequests: 3 (requests allowed in half-open state) - Interval: 60s (stat collection window in closed state) - Timeout: 30s (how long to stay open before trying half-open) - FailureThreshold: 5 (consecutive failures to trip)
func (*ServiceBreaker) Counts ¶ added in v0.1.4
func (b *ServiceBreaker) Counts() gobreaker.Counts
Counts returns the current counts (requests, successes, failures, etc.)
func (*ServiceBreaker) Execute ¶ added in v0.1.4
func (b *ServiceBreaker) Execute(fn func() error) error
Execute runs an operation through the circuit breaker. Returns an error if the circuit is open or if the operation fails.
func (*ServiceBreaker) IsClosed ¶ added in v0.1.4
func (b *ServiceBreaker) IsClosed() bool
IsClosed returns true if the circuit is closed (allowing requests)
func (*ServiceBreaker) IsHalfOpen ¶ added in v0.1.4
func (b *ServiceBreaker) IsHalfOpen() bool
IsHalfOpen returns true if the circuit is half-open (testing recovery)
func (*ServiceBreaker) IsOpen ¶ added in v0.1.4
func (b *ServiceBreaker) IsOpen() bool
IsOpen returns true if the circuit is open (blocking requests)
func (*ServiceBreaker) Name ¶ added in v0.1.4
func (b *ServiceBreaker) Name() string
Name returns the name of the circuit breaker
func (*ServiceBreaker) State ¶ added in v0.1.4
func (b *ServiceBreaker) State() string
State returns the current state of the circuit breaker