【发布时间】:2018-11-10 20:46:49
【问题描述】:
我试图基于 Reactor 额外包的功能在 Kotlin 和 Reactor 中实现重试逻辑。我想要做的是传递一个持续时间列表,并且在每个context.iteration 上,我得到列表的第 (iteration-1) 个元素。它部分工作,我总是在最后一次迭代中得到一个IndexOutOfBoundsException,这比我想要的要多,尽管我提供了最大重试次数 - 列表的大小。虽然重试在给定的持续时间和“正确”的次数内运行(肯定是因为IndexOutOfBoundsException 阻止了更多),但只有这个异常(也是根本原因)困扰着我。
这是我自定义的 BackOff 界面:
interface MyCustomBackoff : Backoff {
companion object {
fun getBackoffDelay(backoffList: List<Duration>): (IterationContext<*>) -> BackoffDelay {
return { context -> BackoffDelay(backoffList[(context.iteration() - 1).toInt()]) }
}
}
}
我的 Kotlin 扩展是:
fun <T> Mono<T>.retryCustomBackoffs(backoffList: List<Duration>, doOnRetry: ((RetryContext<T>) -> Unit)? = null): Mono<T> {
val retry = Retry.any<T>().retryMax(backoffList.size.toLong()).backoff(MyCustomBackoff.getBackoffDelay(backoffList))
return if (doOnRetry == null) {
this.retryWhen(retry)
}
else {
this.retryWhen(retry.doOnRetry(doOnRetry))
}
}
我在这里错过了什么?
【问题讨论】:
标签: kotlin spring-webflux project-reactor retry-logic