Documentation
¶
Index ¶
- Variables
- func IsPermanent(err error) bool
- func IsRetryable(err error) bool
- func New(fs absfs.FileSystem, options ...Option) absfs.FileSystem
- type CircuitBreaker
- type CircuitBreakerConfig
- type CircuitBreakerStats
- type Config
- type ErrorClass
- type ErrorClassifier
- type Logger
- type Metrics
- type NoopLogger
- type Operation
- type OperationMetrics
- type Option
- func WithCircuitBreaker(cb *CircuitBreaker) Option
- func WithConfig(config *Config) Option
- func WithErrorClassifier(classifier ErrorClassifier) Option
- func WithLogger(logger Logger) Option
- func WithOnRetry(onRetry func(op Operation, attempt int, err error)) Option
- func WithPerOperationCircuitBreaker(pocb *PerOperationCircuitBreaker) Option
- func WithPolicy(policy *Policy) Option
- type PerOperationCircuitBreaker
- func (pocb *PerOperationCircuitBreaker) Call(op Operation, fn func() error) error
- func (pocb *PerOperationCircuitBreaker) GetAllStates() map[Operation]State
- func (pocb *PerOperationCircuitBreaker) GetAllStats() map[Operation]CircuitBreakerStats
- func (pocb *PerOperationCircuitBreaker) GetCircuitBreaker(op Operation) *CircuitBreaker
- func (pocb *PerOperationCircuitBreaker) Reset()
- func (pocb *PerOperationCircuitBreaker) ResetOperation(op Operation)
- type Policy
- type PrometheusCollector
- type RetryFS
- func (rfs *RetryFS) Chdir(dir string) error
- func (rfs *RetryFS) Chmod(name string, mode os.FileMode) error
- func (rfs *RetryFS) ChmodContext(ctx context.Context, name string, mode os.FileMode) error
- func (rfs *RetryFS) Chown(name string, uid, gid int) error
- func (rfs *RetryFS) ChownContext(ctx context.Context, name string, uid, gid int) error
- func (rfs *RetryFS) Chtimes(name string, atime time.Time, mtime time.Time) error
- func (rfs *RetryFS) ChtimesContext(ctx context.Context, name string, atime, mtime time.Time) error
- func (rfs *RetryFS) Create(filename string) (absfs.File, error)
- func (rfs *RetryFS) CreateContext(ctx context.Context, filename string) (absfs.File, error)
- func (rfs *RetryFS) GetCircuitBreaker() *CircuitBreaker
- func (rfs *RetryFS) GetMetrics() *Metrics
- func (rfs *RetryFS) GetPerOperationCircuitBreaker() *PerOperationCircuitBreaker
- func (rfs *RetryFS) Getwd() (string, error)
- func (rfs *RetryFS) Lchown(name string, uid, gid int) error
- func (rfs *RetryFS) LchownContext(ctx context.Context, name string, uid, gid int) error
- func (rfs *RetryFS) Lstat(name string) (os.FileInfo, error)
- func (rfs *RetryFS) LstatContext(ctx context.Context, filename string) (os.FileInfo, error)
- func (rfs *RetryFS) Mkdir(name string, perm os.FileMode) error
- func (rfs *RetryFS) MkdirAll(filename string, perm os.FileMode) error
- func (rfs *RetryFS) MkdirAllContext(ctx context.Context, filename string, perm os.FileMode) error
- func (rfs *RetryFS) Open(filename string) (absfs.File, error)
- func (rfs *RetryFS) OpenContext(ctx context.Context, filename string) (absfs.File, error)
- func (rfs *RetryFS) OpenFile(filename string, flag int, perm os.FileMode) (absfs.File, error)
- func (rfs *RetryFS) OpenFileContext(ctx context.Context, filename string, flag int, perm os.FileMode) (absfs.File, error)
- func (rfs *RetryFS) ReadDir(name string) ([]fs.DirEntry, error)
- func (rfs *RetryFS) ReadFile(name string) ([]byte, error)
- func (rfs *RetryFS) Readlink(name string) (string, error)
- func (rfs *RetryFS) ReadlinkContext(ctx context.Context, link string) (string, error)
- func (rfs *RetryFS) Remove(filename string) error
- func (rfs *RetryFS) RemoveAll(path string) error
- func (rfs *RetryFS) RemoveContext(ctx context.Context, filename string) error
- func (rfs *RetryFS) Rename(oldpath, newpath string) error
- func (rfs *RetryFS) RenameContext(ctx context.Context, oldpath, newpath string) error
- func (rfs *RetryFS) Stat(filename string) (os.FileInfo, error)
- func (rfs *RetryFS) StatContext(ctx context.Context, filename string) (os.FileInfo, error)
- func (rfs *RetryFS) Sub(dir string) (fs.FS, error)
- func (rfs *RetryFS) Symlink(oldname, newname string) error
- func (rfs *RetryFS) SymlinkContext(ctx context.Context, target, link string) error
- func (rfs *RetryFS) TempDir() string
- func (rfs *RetryFS) Truncate(name string, size int64) error
- type SlogLogger
- type State
Constants ¶
This section is empty.
Variables ¶
var DefaultPolicy = &Policy{ MaxAttempts: 5, BaseDelay: 100 * time.Millisecond, MaxDelay: 30 * time.Second, Jitter: 0.25, Multiplier: 2.0, }
DefaultPolicy provides sensible defaults for retry behavior
var ( // ErrCircuitOpen is returned when the circuit breaker is open ErrCircuitOpen = errors.New("circuit breaker is open") )
Functions ¶
func IsPermanent ¶
IsPermanent is a helper function to determine if an error is permanent using the default classifier
func IsRetryable ¶
IsRetryable is a helper function to determine if an error is retryable using the default classifier
func New ¶
func New(fs absfs.FileSystem, options ...Option) absfs.FileSystem
New creates a new RetryFS wrapping the given filesystem
Types ¶
type CircuitBreaker ¶
type CircuitBreaker struct {
// FailureThreshold is the number of consecutive failures to open the circuit
FailureThreshold int
// SuccessThreshold is the number of consecutive successes to close the circuit from half-open
SuccessThreshold int
// Timeout is how long to wait in open state before trying half-open
Timeout time.Duration
// OnStateChange is called when the circuit breaker changes state
OnStateChange func(from, to State)
// contains filtered or unexported fields
}
CircuitBreaker implements the circuit breaker pattern
func NewCircuitBreaker ¶
func NewCircuitBreaker() *CircuitBreaker
NewCircuitBreaker creates a new CircuitBreaker with default settings
func (*CircuitBreaker) Call ¶
func (cb *CircuitBreaker) Call(fn func() error) error
Call executes the given function with circuit breaker protection
func (*CircuitBreaker) GetState ¶
func (cb *CircuitBreaker) GetState() State
GetState returns the current state of the circuit breaker
func (*CircuitBreaker) GetStats ¶
func (cb *CircuitBreaker) GetStats() CircuitBreakerStats
GetStats returns statistics about the circuit breaker
func (*CircuitBreaker) Reset ¶
func (cb *CircuitBreaker) Reset()
Reset resets the circuit breaker to closed state
type CircuitBreakerConfig ¶
type CircuitBreakerConfig struct {
FailureThreshold int
SuccessThreshold int
Timeout time.Duration
OnStateChange func(op Operation, from, to State)
}
CircuitBreakerConfig holds configuration for creating new circuit breakers
type CircuitBreakerStats ¶
type CircuitBreakerStats struct {
State State
ConsecutiveErrors int
ConsecutiveSuccess int
LastFailureTime time.Time
}
CircuitBreakerStats contains statistics about a circuit breaker
type Config ¶
type Config struct {
// DefaultPolicy is the policy used when no operation-specific policy exists
DefaultPolicy *Policy
// OperationPolicies maps operations to their specific retry policies
OperationPolicies map[Operation]*Policy
// ErrorClassifier classifies errors as retryable or permanent
ErrorClassifier ErrorClassifier
// OnRetry is called before each retry attempt
OnRetry func(op Operation, attempt int, err error)
}
Config holds the configuration for RetryFS
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a new Config with sensible defaults
type ErrorClass ¶
type ErrorClass int
ErrorClass represents the classification of an error
const ( // ErrorUnknown means we can't determine if the error is retryable ErrorUnknown ErrorClass = iota // ErrorRetryable means the error is transient and may succeed on retry ErrorRetryable // ErrorPermanent means the error is permanent and won't change on retry ErrorPermanent )
func (ErrorClass) String ¶
func (ec ErrorClass) String() string
String returns a string representation of the ErrorClass
type ErrorClassifier ¶
type ErrorClassifier func(err error) ErrorClass
ErrorClassifier is a function that classifies an error
type Logger ¶
type Logger interface {
// Debug logs a debug message with structured fields
Debug(msg string, keysAndValues ...interface{})
// Info logs an info message with structured fields
Info(msg string, keysAndValues ...interface{})
// Warn logs a warning message with structured fields
Warn(msg string, keysAndValues ...interface{})
// Error logs an error message with structured fields
Error(msg string, keysAndValues ...interface{})
}
Logger is the interface for structured logging
func NewSlogLogger ¶
NewSlogLogger creates a Logger from a slog.Logger
type Metrics ¶
type Metrics struct {
// TotalAttempts is the total number of attempts across all operations
TotalAttempts int64
// TotalRetries is the total number of retries (attempts - initial tries)
TotalRetries int64
// TotalSuccesses is the total number of successful operations
TotalSuccesses int64
// TotalFailures is the total number of failed operations (after all retries)
TotalFailures int64
// ErrorsByClass tracks errors by their classification
ErrorsByClass map[ErrorClass]int64
// OperationMetrics tracks metrics per operation type
OperationMetrics map[Operation]*OperationMetrics
}
Metrics holds statistics about retry behavior
func (*Metrics) RecordAttempt ¶
RecordAttempt records an attempt for an operation
func (*Metrics) RecordFailure ¶
func (m *Metrics) RecordFailure(op Operation, errClass ErrorClass)
RecordFailure records a failed operation
func (*Metrics) RecordSuccess ¶
RecordSuccess records a successful operation
type NoopLogger ¶
type NoopLogger struct{}
NoopLogger is a logger that does nothing
func (*NoopLogger) Debug ¶
func (n *NoopLogger) Debug(msg string, keysAndValues ...interface{})
func (*NoopLogger) Error ¶
func (n *NoopLogger) Error(msg string, keysAndValues ...interface{})
func (*NoopLogger) Info ¶
func (n *NoopLogger) Info(msg string, keysAndValues ...interface{})
func (*NoopLogger) Warn ¶
func (n *NoopLogger) Warn(msg string, keysAndValues ...interface{})
type Operation ¶
type Operation string
Operation represents a filesystem operation type
const ( OpOpen Operation = "open" OpCreate Operation = "create" OpOpenFile Operation = "openfile" OpStat Operation = "stat" OpLstat Operation = "lstat" OpMkdir Operation = "mkdir" OpMkdirAll Operation = "mkdirall" OpRemove Operation = "remove" OpRemoveAll Operation = "removeall" OpRename Operation = "rename" OpChmod Operation = "chmod" OpChown Operation = "chown" OpLchown Operation = "lchown" OpChtimes Operation = "chtimes" OpChdir Operation = "chdir" OpGetwd Operation = "getwd" OpTruncate Operation = "truncate" OpSymlink Operation = "symlink" OpReadlink Operation = "readlink" OpReadDir Operation = "readdir" OpReadFile Operation = "readfile" OpSub Operation = "sub" )
type OperationMetrics ¶
OperationMetrics holds metrics for a specific operation type
type Option ¶
type Option func(*RetryFS)
Option is a functional option for configuring RetryFS
func WithCircuitBreaker ¶
func WithCircuitBreaker(cb *CircuitBreaker) Option
WithCircuitBreaker enables circuit breaker protection
func WithErrorClassifier ¶
func WithErrorClassifier(classifier ErrorClassifier) Option
WithErrorClassifier sets a custom error classifier
func WithOnRetry ¶
WithOnRetry sets a callback for retry events
func WithPerOperationCircuitBreaker ¶
func WithPerOperationCircuitBreaker(pocb *PerOperationCircuitBreaker) Option
WithPerOperationCircuitBreaker enables per-operation circuit breaker protection
type PerOperationCircuitBreaker ¶
type PerOperationCircuitBreaker struct {
// contains filtered or unexported fields
}
PerOperationCircuitBreaker manages circuit breakers for individual operations
func NewPerOperationCircuitBreaker ¶
func NewPerOperationCircuitBreaker(config *CircuitBreakerConfig) *PerOperationCircuitBreaker
NewPerOperationCircuitBreaker creates a new per-operation circuit breaker manager
func (*PerOperationCircuitBreaker) Call ¶
func (pocb *PerOperationCircuitBreaker) Call(op Operation, fn func() error) error
Call executes a function with the circuit breaker for the given operation
func (*PerOperationCircuitBreaker) GetAllStates ¶
func (pocb *PerOperationCircuitBreaker) GetAllStates() map[Operation]State
GetAllStates returns the state of all circuit breakers
func (*PerOperationCircuitBreaker) GetAllStats ¶
func (pocb *PerOperationCircuitBreaker) GetAllStats() map[Operation]CircuitBreakerStats
GetAllStats returns statistics for all circuit breakers
func (*PerOperationCircuitBreaker) GetCircuitBreaker ¶
func (pocb *PerOperationCircuitBreaker) GetCircuitBreaker(op Operation) *CircuitBreaker
GetCircuitBreaker returns the circuit breaker for a specific operation Creates one if it doesn't exist
func (*PerOperationCircuitBreaker) Reset ¶
func (pocb *PerOperationCircuitBreaker) Reset()
Reset resets all circuit breakers
func (*PerOperationCircuitBreaker) ResetOperation ¶
func (pocb *PerOperationCircuitBreaker) ResetOperation(op Operation)
ResetOperation resets the circuit breaker for a specific operation
type Policy ¶
type Policy struct {
// MaxAttempts is the maximum number of attempts (including the first try)
MaxAttempts int
// BaseDelay is the initial delay before the first retry
BaseDelay time.Duration
// MaxDelay is the maximum delay between retries
MaxDelay time.Duration
// Jitter is the random variance factor (0.0 to 1.0) added to delay
// A jitter of 0.25 means ±25% randomness
Jitter float64
// Multiplier is the exponential backoff multiplier (usually 2.0)
Multiplier float64
}
Policy defines the retry behavior
type PrometheusCollector ¶
type PrometheusCollector struct {
// contains filtered or unexported fields
}
PrometheusCollector collects retryfs metrics for Prometheus
func NewPrometheusCollector ¶
func NewPrometheusCollector(rfs *RetryFS, namespace, subsystem string) *PrometheusCollector
NewPrometheusCollector creates a new Prometheus collector for the given RetryFS
func (*PrometheusCollector) Collect ¶
func (c *PrometheusCollector) Collect(ch chan<- prometheus.Metric)
Collect implements prometheus.Collector
func (*PrometheusCollector) Describe ¶
func (c *PrometheusCollector) Describe(ch chan<- *prometheus.Desc)
Describe implements prometheus.Collector
func (*PrometheusCollector) MustRegister ¶
func (c *PrometheusCollector) MustRegister(registry *prometheus.Registry)
MustRegister registers the collector with the provided Prometheus registry It panics if registration fails
func (*PrometheusCollector) Register ¶
func (c *PrometheusCollector) Register(registry *prometheus.Registry) error
Register registers the collector with the provided Prometheus registry Returns an error if registration fails
type RetryFS ¶
type RetryFS struct {
// contains filtered or unexported fields
}
RetryFS wraps an absfs.FileSystem with automatic retry logic
func (*RetryFS) ChmodContext ¶
ChmodContext changes file mode with context support
func (*RetryFS) ChownContext ¶
ChownContext changes file owner with context support
func (*RetryFS) ChtimesContext ¶
ChtimesContext changes file times with context support
func (*RetryFS) CreateContext ¶
CreateContext creates a file with context support
func (*RetryFS) GetCircuitBreaker ¶
func (rfs *RetryFS) GetCircuitBreaker() *CircuitBreaker
GetCircuitBreaker returns the circuit breaker if configured
func (*RetryFS) GetMetrics ¶
GetMetrics returns the current metrics
func (*RetryFS) GetPerOperationCircuitBreaker ¶
func (rfs *RetryFS) GetPerOperationCircuitBreaker() *PerOperationCircuitBreaker
GetPerOperationCircuitBreaker returns the per-operation circuit breaker if configured
func (*RetryFS) LchownContext ¶
LchownContext changes file owner without following symlinks with context support
func (*RetryFS) LstatContext ¶
LstatContext returns file info without following symlinks with context support
func (*RetryFS) MkdirAllContext ¶
MkdirAllContext creates a directory tree with context support
func (*RetryFS) OpenContext ¶
OpenContext opens a file with context support
func (*RetryFS) OpenFileContext ¶
func (rfs *RetryFS) OpenFileContext(ctx context.Context, filename string, flag int, perm os.FileMode) (absfs.File, error)
OpenFileContext opens a file with flags and permissions with context support
func (*RetryFS) ReadlinkContext ¶
ReadlinkContext reads a symbolic link with context support
func (*RetryFS) RemoveContext ¶
RemoveContext removes a file with context support
func (*RetryFS) RenameContext ¶
RenameContext renames a file with context support
func (*RetryFS) StatContext ¶
StatContext returns file info with context support
func (*RetryFS) SymlinkContext ¶
SymlinkContext creates a symbolic link with context support
type SlogLogger ¶
type SlogLogger struct {
// contains filtered or unexported fields
}
SlogLogger wraps slog.Logger to implement our Logger interface
func (*SlogLogger) Debug ¶
func (s *SlogLogger) Debug(msg string, keysAndValues ...interface{})
func (*SlogLogger) Error ¶
func (s *SlogLogger) Error(msg string, keysAndValues ...interface{})
func (*SlogLogger) Info ¶
func (s *SlogLogger) Info(msg string, keysAndValues ...interface{})
func (*SlogLogger) Warn ¶
func (s *SlogLogger) Warn(msg string, keysAndValues ...interface{})