You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
packagecom.hoc081098.kotlin_playground.arrowktimportarrow.core.Eitherimportarrow.resilience.CircuitBreakerimportkotlin.time.Duration.Companion.secondsimportkotlin.time.ExperimentalTimeimportkotlinx.coroutines.delay
@ExperimentalTime
suspendfunmain(): Unit {
val circuitBreaker =CircuitBreaker(
openingStrategy =CircuitBreaker.OpeningStrategy.Count(2),
resetTimeout =2.seconds,
exponentialBackoffFactor =1.2,
maxResetTimeout =60.seconds,
)
// normal operation
circuitBreaker.protectOrThrow { "I am in Closed: ${circuitBreaker.state()}" }.also(::println)
// simulate service getting overloadedEither.catch {
circuitBreaker.protectOrThrow { throwRuntimeException("Service overloaded") }
}.also(::println)
Either.catch {
circuitBreaker.protectOrThrow { throwRuntimeException("Service overloaded") }
}.also(::println)
circuitBreaker.protectEither { }
.also { println("I am Open and short-circuit with ${it}. ${circuitBreaker.state()}") }
println(">>> state should be Open: ${circuitBreaker.state()}")
// simulate reset timeoutprintln("Service recovering . . .").also { delay(2500) }
println(">>> state should be HalfOpen: ${circuitBreaker.state()}")
// simulate test request success
circuitBreaker.protectOrThrow {
"I am running test-request in HalfOpen: ${circuitBreaker.state()}"
}.also(::println)
println("I am back to normal state closed ${circuitBreaker.state()}")
}
Console
I am in Closed: arrow.resilience.CircuitBreaker$State$Closed@5ccd43c2
Either.Left(java.lang.RuntimeException: Service overloaded)
Either.Left(java.lang.RuntimeException: Service overloaded)
I am Open and short-circuit with Either.Right(kotlin.Unit). arrow.resilience.CircuitBreaker$State$Closed@27ddd392
>>> state should be Open: arrow.resilience.CircuitBreaker$State$Closed@27ddd392
Service recovering . . .
>>> state should be HalfOpen: arrow.resilience.CircuitBreaker$State$Closed@27ddd392
I am running test-request in HalfOpen: arrow.resilience.CircuitBreaker$State$Closed@27ddd392
I am back to normal state closed arrow.resilience.CircuitBreaker$State$Closed@27ddd392
Process finished with exit code 0
We can see the line >>> state should be Open: arrow.resilience.CircuitBreaker$State$Closed@27ddd392
I think that CircuitBreaker.state() returns the wrong state.
The text was updated successfully, but these errors were encountered:
I have just noticed that the example in docs seems to be wrong compared to the actual library source code.
Docs
🔀Closed
This is the state in which the circuit breaker starts.
Requests are made normally in this state:
When an exception occurs, it increments the failure counter.
When the failure counter reaches the given maxFailures threshold, the breaker moves to the Open state.
A successful request will reset the failure counter to zero.
Version
2.0.0
Reproduce
I've copied the sample code from https://arrow-kt.io/learn/resilience/circuitbreaker/#arrows-circuitbreaker, and added 2 lines to print the state of the CircuitBreaker.
Github link: https://github.com/hoc081098/kotlin_playground/blob/master/src/main/kotlin/com/hoc081098/kotlin_playground/arrowkt/circuit_breaker.kt
Console
We can see the line
>>> state should be Open: arrow.resilience.CircuitBreaker$State$Closed@27ddd392
I think that
CircuitBreaker.state()
returns the wrong state.The text was updated successfully, but these errors were encountered: