Non-suspending variant of Resilience #3433
Replies: 2 comments 3 replies
-
@vladimirfx, I am absolutely open for it, I expect Loom to also play a important role in Kotlin/JVM (I expect 99% of Spring users to use Loom, not coroutines). However, I am not entirely sure yet where it would belong. Almost everything in Fx could be ported to Loom. So I am including to build it outside of Arrow, or in a standalone repository (as we're doing for all new projects). A lot of loom stuff hasn't been explored for Kotlin, but I wrote a What do you think? |
Beta Was this translation helpful? Give feedback.
-
The crucial problem is So the question is not how to use Resilience in blocking projects - it is not so hard. The question is about first-class support of blocking style code in Resilience. Currently, we use such simple utility function for blocking projects: fun <T> retryOn(
attempts: Int = Int.MAX_VALUE,
initialDelay: Duration = 100.milliseconds,
maxDelay: Duration = 10.seconds,
timeout: Duration = 60.seconds,
factor: Double = 2.0,
errorType: KClass<out Exception> = Exception::class,
block: () -> T,
): T {
var currentDelay = initialDelay
val startedAt = markNow()
repeat(attempts - 1) { attempt ->
try {
return block()
} catch (ie: InterruptedException) {
throw ie
} catch (t: Exception) {
if (errorType.isAssignableWithCause(t) && startedAt.elapsedNow() < timeout) {
log.info(t) { "Retry $attempt, elapsed time: ${startedAt.elapsedNow()}" }
} else {
throw t
}
}
LockSupport.parkNanos(currentDelay.inWholeNanoseconds)
currentDelay = (currentDelay.inWholeMilliseconds * factor).toLong().coerceAtMost(maxDelay.inWholeMilliseconds).milliseconds
}
return block()
} Key differences are:
It works but this implementation is much more primitive and inflexible than Resilience. I don't know how such first-class support may be packaged... |
Beta Was this translation helpful? Give feedback.
-
The main use case is JVM with virtual threads:
Resilience is a handy tool for interaction with external systems. It works quite well in Android and Coroutines-based JVM backend projects. But most of the backend projects are blocking in nature. Most of the usual
magic
like AOP is not working well with suspend functions.Until JDK 21, blocking threads for tasks as retry calls is considered bad practice because of the platform nature of threads. Now we have virtual threads that in nature, are very similar to coroutines and can be safely blocked with resilience operations.
What do you think about the non-suspending variant of Arrow Resilience?
Beta Was this translation helpful? Give feedback.
All reactions