Package-level declarations

Types

Link copied to clipboard

A CircuitBreaker is used to protect resources or services from being overloaded When a service is being overloaded, interacting with it more will only worsen its overloaded state. Especially when combined with retry mechanisms such as Schedule, in some cases simply using a back-off retry policy might not be sufficient during peak traffics.

Link copied to clipboard
typealias Saga<A> = suspend SagaScope.() -> A

The saga design pattern is a way to manage data consistency across microservices in distributed transaction scenarios. A Saga is useful when you need to manage data in a consistent manner across services in distributed transaction scenarios. Or when you need to compose multiple actions with a compensation that needs to run in a transaction like style.

Link copied to clipboard

Marker object to protect SagaScope.saga from calling SagaScope.bind in its action step.

Link copied to clipboard
annotation class SagaDSLMarker

DSL Marker for the SagaEffect DSL

Link copied to clipboard
interface SagaScope

DSL that enables the Saga pattern in a suspend DSL.

Link copied to clipboard
fun interface Schedule<in Input, out Output>

A Schedule describes how a suspend fun should retry or repeat.

Link copied to clipboard

Functions

Link copied to clipboard
inline suspend fun <E : Throwable, A> Schedule<E, *>.retry(noinline action: suspend () -> A): A
suspend fun <E : Throwable, A> Schedule<E, *>.retry(exceptionClass: KClass<E>, action: suspend () -> A): A

Retries action using any E that occurred as the input to the Schedule. It will throw the last exception if the Schedule is exhausted, and ignores the output of the Schedule.

fun <A, B> Flow<A>.retry(schedule: Schedule<Throwable, B>): Flow<A>

Retries collection of the given flow when an exception occurs in the upstream flow based on a decision by the schedule. This operator is transparent to exceptions that occur in downstream flow and does not retry on exceptions that are thrown to cancel the flow.

inline suspend fun <Error, Result, Output> Raise<Error>.retry(schedule: Schedule<Error, Output>, action: Raise<Error>.() -> Result): Result

Retries action using any Error that occurred as the input to the Schedule. It will return the last Error if the Schedule is exhausted, and ignores the output of the Schedule.

Link copied to clipboard
inline suspend fun <Error, Result, Output> Schedule<Error, Output>.retryEither(action: () -> Either<Error, Result>): Either<Error, Result>

Retries action using any Error that occurred as the input to the Schedule. It will return the last Error if the Schedule is exhausted, and ignores the output of the Schedule.

Link copied to clipboard
inline suspend fun <E : Throwable, Input, Output> Schedule<E, Output>.retryOrElse(noinline action: suspend () -> Input, noinline orElse: suspend (Throwable, Output) -> Input): Input
suspend fun <E : Throwable, Input, Output> Schedule<E, Output>.retryOrElse(exceptionClass: KClass<E>, action: suspend () -> Input, orElse: suspend (E, Output) -> Input): Input

Retries action using any E that occurred as the input to the Schedule. If the Schedule is exhausted, it will invoke orElse with the last exception and the output of the Schedule to produce a fallback Input value.

Link copied to clipboard
inline suspend fun <E : Throwable, Input, Output, A> Schedule<E, Output>.retryOrElseEither(noinline action: suspend () -> Input, noinline orElse: suspend (E, Output) -> A): Either<A, Input>
suspend fun <E : Throwable, Input, Output, A> Schedule<E, Output>.retryOrElseEither(exceptionClass: KClass<E>, action: suspend () -> Input, orElse: suspend (E, Output) -> A): Either<A, Input>

Retries action using any E that occurred as the input to the Schedule. If the Schedule is exhausted, it will invoke orElse with the last exception and the output of the Schedule to produce a fallback value of A. Returns Either with the fallback value if the Schedule is exhausted, or the successful result of action.

Link copied to clipboard
inline suspend fun <Error, Result, Output> Schedule<Error, Output>.retryRaise(action: Raise<Error>.() -> Result): Either<Error, Result>

Retries action using any Error that occurred as the input to the Schedule. It will return the last Error if the Schedule is exhausted, and ignores the output of the Schedule.

Link copied to clipboard
inline fun <A> saga(noinline block: suspend SagaScope.() -> A): Saga<A>

The Saga builder which exposes the SagaScope.bind. The saga builder uses the suspension system to run actions, and automatically register their compensating actions.

fun <A> saga(action: suspend SagaActionStep.() -> A, compensation: suspend (A) -> Unit): Saga<A>

Create a lazy Saga that will only run when the Saga is invoked.

Link copied to clipboard
suspend fun <A> Saga<A>.transact(): A

Transact runs the Saga turning it into a suspend effect that results in A. If the saga fails then all compensating actions are guaranteed to run. When a compensating action failed it will be ignored, and the other compensating actions will continue to be run.