-
Notifications
You must be signed in to change notification settings - Fork 468
kyay10/saga-as-resource #3794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
kyay10/saga-as-resource #3794
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,10 @@ package arrow.resilience | |
| import arrow.atomic.Atomic | ||
| import arrow.atomic.update | ||
| import arrow.core.nonFatalOrThrow | ||
| import kotlinx.coroutines.NonCancellable | ||
| import kotlinx.coroutines.withContext | ||
| import kotlin.coroutines.cancellation.CancellationException | ||
| import arrow.core.prependTo | ||
| import arrow.fx.coroutines.ExitCase | ||
| import arrow.fx.coroutines.ResourceScope | ||
| import arrow.fx.coroutines.resourceScope | ||
|
|
||
|
|
||
| /** | ||
|
|
@@ -98,14 +99,8 @@ public fun <A> 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. | ||
| */ | ||
| public suspend fun <A> Saga<A>.transact(): A { | ||
| val builder = SagaBuilder() | ||
| return guaranteeCase({ invoke(builder) }) { res -> | ||
| when (res) { | ||
| null -> builder.totalCompensation() | ||
| else -> Unit | ||
| } | ||
| } | ||
| public suspend fun <A> Saga<A>.transact(): A = resourceScope { | ||
| invoke(SagaResourceScope(this)) | ||
| } | ||
|
|
||
| /** DSL Marker for the SagaEffect DSL */ | ||
|
|
@@ -114,10 +109,19 @@ public suspend fun <A> Saga<A>.transact(): A { | |
| /** | ||
| * Marker object to protect [SagaScope.saga] from calling [SagaScope.bind] in its `action` step. | ||
| */ | ||
| @SagaDSLMarker | ||
| public object SagaActionStep | ||
|
|
||
| // Internal implementation of the `saga { }` builder. | ||
| private class SagaResourceScope(private val scope: ResourceScope) : SagaScope { | ||
| override suspend fun <A> saga( | ||
| action: suspend SagaActionStep.() -> A, | ||
| compensation: suspend (A) -> Unit | ||
| ): A = action(SagaActionStep).also { a -> | ||
| scope.onRelease { if (it !is ExitCase.Completed) compensation(a) } | ||
| } | ||
| } | ||
|
|
||
| @Deprecated("Binary compatibility", level = DeprecationLevel.HIDDEN) | ||
| @PublishedApi | ||
| internal class SagaBuilder( | ||
| private val stack: Atomic<List<suspend () -> Unit>> = Atomic(emptyList()) | ||
|
|
@@ -127,19 +131,11 @@ internal class SagaBuilder( | |
| override suspend fun <A> saga( | ||
| action: suspend SagaActionStep.() -> A, | ||
| compensation: suspend (A) -> Unit | ||
| ): A = | ||
| guaranteeCase({ action(SagaActionStep) }) { res -> | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic of this change is:
Hence, guaranteeCase's code can be simplified to the following (assumptions: private suspend fun <A> guaranteeCase(
fa: suspend () -> A,
finalizer: suspend (value: A?) -> Unit
): A {
val res = fa()
finalizer(res)
return res
}which is just |
||
| // This action failed, so we have no compensate to push on the stack | ||
| // the compensation stack will run in the `transact` stage, this is just the builder | ||
| when (res) { | ||
| null -> Unit | ||
| else -> stack.update( | ||
| function = { listOf(suspend { compensation(res) }) + it }, | ||
| transform = { _, new -> new } | ||
| ) | ||
| } | ||
| } | ||
| ): A = action(SagaActionStep).also { res -> | ||
| stack.update(suspend { compensation(res) }::prependTo) | ||
| } | ||
|
|
||
| @Deprecated("Binary compatibility", level = DeprecationLevel.HIDDEN) | ||
| @PublishedApi | ||
| internal suspend fun totalCompensation() { | ||
| stack | ||
|
|
@@ -156,28 +152,3 @@ internal class SagaBuilder( | |
| ?.let { throw it } | ||
| } | ||
| } | ||
|
|
||
| private suspend fun <A> guaranteeCase( | ||
| fa: suspend () -> A, | ||
| finalizer: suspend (value: A?) -> Unit | ||
| ): A { | ||
| val res = | ||
| try { | ||
| fa() | ||
| } catch (e: CancellationException) { | ||
| runReleaseAndRethrow(e) { finalizer(null) } | ||
| } catch (t: Throwable) { | ||
| runReleaseAndRethrow(t) { finalizer(null) } | ||
| } | ||
| withContext(NonCancellable) { finalizer(res) } | ||
| return res | ||
| } | ||
|
|
||
| private suspend fun runReleaseAndRethrow(original: Throwable, f: suspend () -> Unit): Nothing { | ||
| try { | ||
| withContext(NonCancellable) { f() } | ||
| } catch (e: Throwable) { | ||
| original.addSuppressed(e.nonFatalOrThrow()) | ||
| } | ||
| throw original | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.