asResilientScope

fun CoroutineScope.asResilientScope(): ResilientScope

Wraps this CoroutineScope as a ResilientScope that participates in the caller's coroutine hierarchy.

Lifecycle contract:

  • When the outer CoroutineScope is cancelled, the derived ResilientScope and all background jobs it owns (e.g. cache-cleanup sweepers, coalescing jobs) are cancelled automatically via structured concurrency.

  • Calling ResilientPolicy.close on a policy built from the returned scope cancels only the internal policy jobs — it does not cancel this CoroutineScope.

Important: Do not call this on a scope that has already been cancelled. The derived ResilientScope will still be created, but any internal jobs it tries to launch will fail immediately.

Return

A ResilientScope whose lifecycle is governed by this CoroutineScope.