ResilientPolicy

A comprehensive resilience policy that orchestrates multiple strategies to safeguard suspendable operations.

This policy composes strategies like timeout, retry, circuit breaker, rate limiter, bulkhead, and others, applying them in a predefined order to enhance the robustness and reliability of the executed code. It acts as a single entry point for executing an operation with a full suite of resilience measures.

The execution pipeline follows this sequence (from outermost to innermost):

  1. Fallback: Provides an alternative result if the entire pipeline fails.

  2. Cache: Returns a cached result if available, bypassing the rest of the pipeline.

  3. Coalescing: Deduplicates concurrent in-flight executions by key.

  4. Timeout: Enforces a total time limit for the operation, including all retries.

  5. Retry: Re-executes the operation upon specific failures.

  6. Circuit Breaker: Halts execution temporarily if failure rates exceed a threshold.

  7. Rate Limiter: Controls the frequency of executions.

  8. Bulkhead: Limits the number of concurrent executions.

  9. Hedging: Executes secondary operations in parallel if the primary is slow.

  10. User's Code Block: The actual suspendable operation to be executed.

Telemetry events are emitted throughout the execution, providing insights into the behavior of each strategy.

Lifecycle: This policy implements AutoCloseable. Call close when the policy is no longer needed to release internal resources (e.g. cache cleanup job when using com.santimattius.resilient.cache.CacheConfig.cleanupInterval). If you use a com.santimattius.resilient.composition.ResilientScope, closing that scope cancels its jobs (including cache cleanup); calling close on the policy additionally stops the cache sweeper without closing the scope.

Properties

Link copied to clipboard

Optional handle for cache invalidation when the policy was built with cache enabled.

Link copied to clipboard
abstract val events: SharedFlow<ResilientEvent>

A stream of telemetry events emitted by the policy's resilience strategies during execution.

Functions

Link copied to clipboard
expect abstract fun close()
Link copied to clipboard
abstract suspend fun <T> execute(block: suspend () -> T): T

Executes the provided suspendable block of code under the control of the composed resilience policies.

Link copied to clipboard

Returns a point-in-time snapshot of policy state for health/readiness and metrics.