From a5720be19b3d5de3a048588cea1b30f52f5d4b58 Mon Sep 17 00:00:00 2001 From: Alejandro Serrano Mena Date: Thu, 2 Nov 2023 10:32:47 +0100 Subject: [PATCH] Fix problem with KMP 1.9.20 --- .../arrow/fx/coroutines/CircuitBreaker.kt | 51 +++++++++---------- .../kotlin/arrow/resilience/CircuitBreaker.kt | 26 ++++++---- 2 files changed, 38 insertions(+), 39 deletions(-) diff --git a/arrow-libs/fx/arrow-fx-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/CircuitBreaker.kt b/arrow-libs/fx/arrow-fx-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/CircuitBreaker.kt index 3395f32f401..ec07859b4a4 100644 --- a/arrow-libs/fx/arrow-fx-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/CircuitBreaker.kt +++ b/arrow-libs/fx/arrow-fx-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/CircuitBreaker.kt @@ -621,26 +621,31 @@ private constructor( onClosed: suspend () -> Unit = { }, onHalfOpen: suspend () -> Unit = { }, onOpen: suspend () -> Unit = { }, - ): CircuitBreaker = - CircuitBreaker( + ): CircuitBreaker { + require(maxFailures >= 0) { + "maxFailures expected to be greater than or equal to 0, but was $maxFailures" + } + require(resetTimeoutNanos > 0) { + "resetTimeout expected to be greater than 0, but was $resetTimeoutNanos" + } + require(exponentialBackoffFactor > 0) { + "exponentialBackoffFactor expected to be greater than 0, but was $exponentialBackoffFactor" + } + require(maxResetTimeoutNanos > 0) { + "maxResetTimeout expected to be greater than 0, but was $maxResetTimeoutNanos" + } + return CircuitBreaker( state = AtomicRef(Closed(0)), - maxFailures = maxFailures - .takeIf { it >= 0 } - .let { requireNotNull(it) { "maxFailures expected to be greater than or equal to 0, but was $maxFailures" } }, - resetTimeoutNanos = resetTimeoutNanos - .takeIf { it > 0 } - .let { requireNotNull(it) { "resetTimeout expected to be greater than 0, but was $resetTimeoutNanos" } }, - exponentialBackoffFactor = exponentialBackoffFactor - .takeIf { it > 0 } - .let { requireNotNull(it) { "exponentialBackoffFactor expected to be greater than 0, but was $exponentialBackoffFactor" } }, - maxResetTimeoutNanos = maxResetTimeoutNanos - .takeIf { it > 0 } - .let { requireNotNull(it) { "maxResetTimeout expected to be greater than 0, but was $maxResetTimeoutNanos" } }, + maxFailures = maxFailures, + resetTimeoutNanos = resetTimeoutNanos, + exponentialBackoffFactor = exponentialBackoffFactor, + maxResetTimeoutNanos = maxResetTimeoutNanos, onRejected = onRejected, onClosed = onClosed, onHalfOpen = onHalfOpen, onOpen = onOpen ) + } /** * Attempts to create a [CircuitBreaker]. @@ -682,20 +687,10 @@ private constructor( onOpen: suspend () -> Unit = suspend { }, ): CircuitBreaker = of( - maxFailures = maxFailures - .takeIf { it >= 0 } - .let { requireNotNull(it) { "maxFailures expected to be greater than or equal to 0, but was $maxFailures" } }, - resetTimeoutNanos = resetTimeout - .takeIf { it.isPositive() && it != Duration.ZERO } - .let { requireNotNull(it) { "resetTimeout expected to be greater than ${Duration.ZERO}, but was $resetTimeout" } } - .toDouble(DurationUnit.NANOSECONDS), - exponentialBackoffFactor = exponentialBackoffFactor - .takeIf { it > 0 } - .let { requireNotNull(it) { "exponentialBackoffFactor expected to be greater than 0, but was $exponentialBackoffFactor" } }, - maxResetTimeoutNanos = maxResetTimeout - .takeIf { it.isPositive() && it != Duration.ZERO } - .let { requireNotNull(it) { "maxResetTimeout expected to be greater than ${Duration.ZERO}, but was $maxResetTimeout" } } - .toDouble(DurationUnit.NANOSECONDS), + maxFailures = maxFailures, + resetTimeoutNanos = resetTimeout.toDouble(DurationUnit.NANOSECONDS), + exponentialBackoffFactor = exponentialBackoffFactor, + maxResetTimeoutNanos = maxResetTimeout.toDouble(DurationUnit.NANOSECONDS), onRejected = onRejected, onClosed = onClosed, onHalfOpen = onHalfOpen, diff --git a/arrow-libs/resilience/arrow-resilience/src/commonMain/kotlin/arrow/resilience/CircuitBreaker.kt b/arrow-libs/resilience/arrow-resilience/src/commonMain/kotlin/arrow/resilience/CircuitBreaker.kt index e56372e3285..e348f127c4c 100644 --- a/arrow-libs/resilience/arrow-resilience/src/commonMain/kotlin/arrow/resilience/CircuitBreaker.kt +++ b/arrow-libs/resilience/arrow-resilience/src/commonMain/kotlin/arrow/resilience/CircuitBreaker.kt @@ -520,24 +520,28 @@ private constructor( onClosed: suspend () -> Unit = suspend { }, onHalfOpen: suspend () -> Unit = suspend { }, onOpen: suspend () -> Unit = suspend { } - ): CircuitBreaker = - CircuitBreaker( + ): CircuitBreaker { + require(resetTimeout > Duration.ZERO) { + "resetTimeout expected to be greater than ${Duration.ZERO}, but was $resetTimeout" + } + require(exponentialBackoffFactor > 0) { + "exponentialBackoffFactor expected to be greater than 0, but was $exponentialBackoffFactor" + } + require(maxResetTimeout > Duration.ZERO) { + "maxResetTimeout expected to be greater than ${Duration.ZERO}, but was $maxResetTimeout" + } + return CircuitBreaker( state = AtomicRef(Closed(openingStrategy)), - resetTimeout = resetTimeout - .takeIf { it.isPositive() && it != Duration.ZERO } - .let { requireNotNull(it) { "resetTimeout expected to be greater than ${Duration.ZERO}, but was $resetTimeout" } }, - exponentialBackoffFactor = exponentialBackoffFactor - .takeIf { it > 0 } - .let { requireNotNull(it) { "exponentialBackoffFactor expected to be greater than 0, but was $exponentialBackoffFactor" } }, - maxResetTimeout = maxResetTimeout - .takeIf { it.isPositive() && it != Duration.ZERO } - .let { requireNotNull(it) { "maxResetTimeout expected to be greater than ${Duration.ZERO}, but was $maxResetTimeout" } }, + resetTimeout = resetTimeout, + exponentialBackoffFactor = exponentialBackoffFactor, + maxResetTimeout = maxResetTimeout, timeSource = timeSource, onRejected = onRejected, onClosed = onClosed, onHalfOpen = onHalfOpen, onOpen = onOpen ) + } } public sealed class OpeningStrategy {