1

I´m struggling to make the Circuit breaker of Monix works async with some request that I´m receiving in my service, and that are failing, so it should activate the Circuit breaker, and even when is open it must render the request.

Here my code

 @GET
  def XXXX(@Suspended asyncResponse: AsyncResponse): Unit = {
    val circuitBreaker = breaker.doOnOpen(eval.Task {
      logger.error(null, "Circuit breaker change state to open")
      asyncResponse.resume(Response.status(Response.Status.SERVICE_UNAVAILABLE.getStatusCode))
    })
      .doOnHalfOpen(eval.Task(logger.error(null, "Circuit breaker change state to half-open")))
      .doOnClosed(eval.Task(logger.error(null, "Circuit breaker change state to close")))
    val staticToggleProgram: ZIO[Any, Throwable, Boolean] =
      (for {
        staticToggle <- toggleService.getStaticToggle()
        state <- renderResponse(asyncResponse, staticToggle)
      } yield state).catchAll(t => {
        logger.error(null, s"Toggle Error handler. Unhandled effect $t")
        asyncResponse.resume(Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode))
        ZIO.fail(t)
      })
    val task = eval.Task(main.unsafeRun(staticToggleProgram))
    circuitBreaker.protect(task).runAsync
  }

It´s seems when is async the AsyncResponse which is used in the Circuit breaker callback when is open, is override by the next request that arrive, and then my service get stuck and no more request can arrive.

So far the only solution is to run it Sync, but that's not an option since it must be NIO

circuitBreaker.protect(task).runSyncMaybe
paul
  • 12,873
  • 23
  • 91
  • 153

0 Answers0