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):
Fallback: Provides an alternative result if the entire pipeline fails.
Cache: Returns a cached result if available, bypassing the rest of the pipeline.
Coalescing: Deduplicates concurrent in-flight executions by key.
Timeout: Enforces a total time limit for the operation, including all retries.
Retry: Re-executes the operation upon specific failures.
Circuit Breaker: Halts execution temporarily if failure rates exceed a threshold.
Rate Limiter: Controls the frequency of executions.
Bulkhead: Limits the number of concurrent executions.
Hedging: Executes secondary operations in parallel if the primary is slow.
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.