ResilientScope

class ResilientScope(dispatcher: CoroutineDispatcher = Dispatchers.Default) : AutoCloseable

Manages a CoroutineScope for internal background tasks within resilience policies, such as cache cleanup (when com.santimattius.resilient.cache.CacheConfig.cleanupInterval is set).

The user's execution block passed to ResilientPolicy.execute runs in the caller's coroutine context, not in this scope. This scope is only used for policy-internal work (e.g. periodic sweeper jobs).

The internal scope uses a SupervisorJob, ensuring that the failure of one child coroutine does not affect others. Dispatchers.Default is used by default for CPU/background work.

As this class implements AutoCloseable, it can be used with use blocks for automatic resource management. Call close when the scope is no longer needed to cancel all running tasks and release resources.

Parameters

dispatcher

The CoroutineDispatcher for launching internal policy coroutines. Defaults to Dispatchers.Default.

Constructors

Link copied to clipboard
constructor(dispatcher: CoroutineDispatcher = Dispatchers.Default)

Functions

Link copied to clipboard
fun <T> async(context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T): Deferred<T>

Launches a coroutine in this scope for internal policy work and returns its Deferred.

Link copied to clipboard
open override fun close()

Cancels this scope and all its child coroutines. Call when the resilience policy is no longer needed.

Link copied to clipboard
fun launch(context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit): Job

Launches a coroutine in this scope for internal policy work (e.g. cache cleanup).