Documentation
¶
Overview ¶
Package resilium provides composable resilience policies — retry, circuit breaker, timeout, and rate limiting — behind a single, type-safe execution API.
Policies are built with New and the With* option functions, then executed through Execute. Middleware order matters; see docs/policy-order.md.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCircuitOpen = errors.New("resilium: circuit breaker is open")
ErrCircuitOpen is returned when a call is rejected because its circuit breaker is in the open state. Use errors.Is to test for it; the underlying circuitbreaker.ErrCircuitOpen may also be present in the error chain when using the subpackage directly.
var ErrMaxAttemptsExceeded = errors.New("resilium: max retry attempts exceeded")
ErrMaxAttemptsExceeded is returned when retry attempts are exhausted without a successful result. The last underlying error is wrapped; errors.Is(err, retry.ErrMaxAttemptsExceeded) may also be true.
var ErrRateLimited = errors.New("resilium: rate limit exceeded")
ErrRateLimited is returned when a call is rejected by a rate-limit policy because no token was available. WithRateLimit never blocks waiting for a token.
var ErrTimeout = errors.New("resilium: operation timed out")
ErrTimeout is returned when an operation exceeds its configured timeout before completing. errors.Is(err, context.DeadlineExceeded) also returns true for timeout errors wrapped by WithTimeout.
Functions ¶
func Execute ¶
Execute runs op through every middleware configured on the policy and returns the typed result. It respects ctx cancellation throughout the middleware chain. Errors from middlewares are returned as-is (often wrapped sentinels such as ErrTimeout or ErrCircuitOpen); use errors.Is to inspect them.
Types ¶
type Hooks ¶
type Hooks struct {
// OnRetry is called when a failed attempt will be retried. attempt is
// 1-indexed (1 = first failure that triggers a retry). It is not
// called on the final failed attempt when no retry follows.
OnRetry func(attempt int, err error)
// OnCircuitOpen is called when a circuit breaker transitions to open.
// name is circuitbreaker.Config.Name when set via WithCircuitBreaker,
// otherwise "".
OnCircuitOpen func(name string)
// OnCircuitClose is called when a circuit breaker transitions to closed
// (typically after a successful half-open trial).
OnCircuitClose func(name string)
// OnTimeout is called when WithTimeout detects a deadline exceeded.
OnTimeout func()
// OnRateLimited is called when WithRateLimit rejects a call because
// no token was available.
OnRateLimited func()
}
Hooks lets callers observe policy events without wiring a full logger or metrics backend. Callbacks are invoked from the middleware that triggers them; they must not block for long. A Policy is safe for concurrent Execute calls; hook implementations should be thread-safe if they mutate shared state.
type Middleware ¶
type Middleware func(next OperationFunc) OperationFunc
Middleware wraps an OperationFunc with additional behavior (retry, circuit breaking, timeout, etc.). Middlewares compose in the order given to New: the first With* option is outermost.
type Operation ¶
Operation is the unit of work resilium executes. It is generic over the result type T so callers get their real type back, not interface{}.
type OperationFunc ¶
OperationFunc is the untyped form of Operation used internally so that middlewares can be composed without needing to know the result type.
type Option ¶
type Option func(*Policy)
Option configures a Policy when passed to New.
func WithCircuitBreaker ¶
func WithCircuitBreaker(cfg circuitbreaker.Config) Option
WithCircuitBreaker adds circuit-breaking behavior to the policy. Each Policy holds one CircuitBreaker instance shared across Execute calls on that policy; use separate policies (or circuitbreaker.Do with a shared breaker) for different dependencies. Set cfg.Name to identify the breaker in OnCircuitOpen, OnCircuitClose, and logger output. Open calls return ErrCircuitOpen.
func WithHooks ¶
WithHooks attaches the given hooks to the policy, chaining with any hooks already registered (e.g. from WithLogger). Later registrations run after earlier ones for the same event.
func WithLogger ¶
WithLogger attaches structured logging to policy events (retries, circuit state transitions, timeouts, rate-limit rejections). A nil logger defaults to slog.Default(). Logging is implemented via Hooks merged with any hooks from WithHooks.
func WithRateLimit ¶
WithRateLimit bounds how often the wrapped operation may run using a token-bucket limiter. requestsPerSecond is the sustained refill rate; burst is the maximum number of tokens that can accumulate (allowing short bursts without rejecting). A typical starting point is burst equal to requestsPerSecond (rounded up) or a small fixed value such as 5–10. Rejected calls return ErrRateLimited immediately without blocking.
func WithRetry ¶
WithRetry adds retry behavior to the policy using the given config. When RetryIf is nil, retries stop immediately on ErrCircuitOpen so an open circuit breaker is not hammered through backoff cycles. Exhausted retries return ErrMaxAttemptsExceeded wrapping the last error.
func WithTimeout ¶
WithTimeout bounds execution time of the wrapped operation using context.WithTimeout. When the deadline is exceeded, returns ErrTimeout wrapping context.DeadlineExceeded. Parent context cancellation returns context.Canceled and is not mapped to ErrTimeout.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package circuitbreaker implements the circuit breaker pattern as a resilium middleware, usable standalone as well.
|
Package circuitbreaker implements the circuit breaker pattern as a resilium middleware, usable standalone as well. |
|
examples
|
|
|
basic
command
Command basic demonstrates using resilium's retry, timeout, and rate limiting policies to call a flaky operation.
|
Command basic demonstrates using resilium's retry, timeout, and rate limiting policies to call a flaky operation. |
|
internal
|
|
|
ratelimit
Package ratelimit provides a minimal token-bucket rate limiter for internal use by resilium policies.
|
Package ratelimit provides a minimal token-bucket rate limiter for internal use by resilium policies. |
|
otel
module
|
|
|
Package retry provides retry policies with configurable backoff strategies, used as a resilium middleware but also usable standalone.
|
Package retry provides retry policies with configurable backoff strategies, used as a resilium middleware but also usable standalone. |