onCompleteWithBreaker
Signature
Description
Evaluates its parameter of type Future[T]
protecting it with the specified CircuitBreaker
.
Refer to Akka Circuit Breaker for a detailed description of this pattern.
If the CircuitBreaker
is open, the request is rejected with a CircuitBreakerOpenRejection
.
Note that in this case the request's entity databytes stream is cancelled, and the connection is closed
as a consequence.
Otherwise, the same behaviour provided by onComplete is to be expected.
Example
def divide(a: Int, b: Int): Future[Int] = Future {
a / b
}
val resetTimeout = 1.second
val breaker = new CircuitBreaker(system.scheduler,
maxFailures = 1,
callTimeout = 5.seconds,
resetTimeout
)
val route =
path("divide" / IntNumber / IntNumber) { (a, b) =>
onCompleteWithBreaker(breaker)(divide(a, b)) {
case Success(value) => complete(s"The result was $value")
case Failure(ex) => complete((InternalServerError, s"An error occurred: ${ex.getMessage}"))
}
}
// tests:
Get("/divide/10/2") ~> route ~> check {
responseAs[String] shouldEqual "The result was 5"
}
Get("/divide/10/0") ~> Route.seal(route) ~> check {
status shouldEqual InternalServerError
responseAs[String] shouldEqual "An error occurred: / by zero"
} // opens the circuit breaker
Get("/divide/10/2") ~> route ~> check {
rejection shouldBe a[CircuitBreakerOpenRejection]
}
Thread.sleep(resetTimeout.toMillis + 200)
Get("/divide/10/2") ~> route ~> check {
responseAs[String] shouldEqual "The result was 5"
}
Contents