Documentation
¶
Overview ¶
Package stability ensures service robustness with mechanisms like circuit breakers, retries for transient failures, throttling for rate limiting, debounce for execution control, and timeouts for enforcing execution limits.
Index ¶
- Variables
- func Breaker[IN, OUT any](fn service.Function[IN, OUT], threshold int) service.Function[IN, OUT]
- func Debounce[IN, OUT any](fn service.Function[IN, OUT], duration time.Duration) service.Function[IN, OUT]
- func Retry[IN, OUT any](fn service.Function[IN, OUT], maxRetries int, delay time.Duration) service.Function[IN, OUT]
- func Throttle[IN, OUT any](fn service.Function[IN, OUT], maxTokens, refill uint, duration time.Duration) service.Function[IN, OUT]
- func Timeout[IN, OUT any](fn service.Function[IN, OUT], duration time.Duration) service.Function[IN, OUT]
Constants ¶
This section is empty.
Variables ¶
var (
)
var (
ErrorThrottleTooManyCalls = errors.New("too many calls")
)
Functions ¶
func Breaker ¶
Breaker wraps a service function with a circuit breaker mechanism. It tracks failures and prevents further calls when a failure threshold is reached.
func Debounce ¶
func Debounce[IN, OUT any](fn service.Function[IN, OUT], duration time.Duration) service.Function[IN, OUT]
Debounce ensures that fn(...) is ultimately called only once if multiple calls arrive within the given duration. Each call waits until that one call finishes, returning the same result/error.
func Retry ¶
func Retry[IN, OUT any](fn service.Function[IN, OUT], maxRetries int, delay time.Duration) service.Function[IN, OUT]
Retry wraps a given function (`fn`) to retry its execution upon failure. The function will be retried up to `maxRetries` times with a delay of `delay` between retries. If the context is canceled during retries, it stops immediately and returns the context error.
func Throttle ¶
func Throttle[IN, OUT any](fn service.Function[IN, OUT], maxTokens, refill uint, duration time.Duration) service.Function[IN, OUT]
Throttle adds rate-limiting behavior to the provided function (`fn`). The function can only be called up to `maxTokens` times initially, and then tokens are refilled by `refill` every `duration`. If the limit is exceeded, the function returns `ErrorThrottleTooManyCalls`.
func Timeout ¶
func Timeout[IN, OUT any](fn service.Function[IN, OUT], duration time.Duration) service.Function[IN, OUT]
Timeout wraps a service.Function and enforces a timeout on its execution. If the function does not complete within the specified duration, the context is canceled, and the function returns an error indicating a timeout.
Types ¶
This section is empty.