A bidi GraphStage that is joined with a Flow to add Circuit Breaker functionality.
Publishes the payload of the CircuitBreakerEvent when the event type of the CircuitBreakerEvent matches with the one used during subscription.
Exception thrown when Circuit Breaker is open.
Exception thrown when Circuit Breaker is open.
Defaults to "Circuit Breaker is open; calls are failing fast"
A container to hold circuit breaker settings.
A container to hold circuit breaker settings.
Circuit Breaker functionality requires each element to be uniquely identified, so it requires a Context, of any type defined by the application, to be carried along with the flow's input and output as a Tuple2 (Scala) or Pair (Java). The requirement is that either the Context itself or a mapping from Context should be able to uniquely identify an element. Here is the ways how a unique id can be retrieved:
uniqueIdMapper
the type of the elements pulled from the upstream along with the Context and pushed down to joined flow
the type that's contained in a Try and pushed downstream along with the Context
the type of the context that is carried around along with the elements. The context may be of any type that can be used to uniquely identify each element
Holds the state of the circuit breaker.
Holds the state of the circuit breaker. Transitions through three states:
Closed
: elements/requests/calls run through the main logic until the maxFailures
count is reached. If
maxFailures
is reached, it transitions to Open
state.
Open
: Short circuits. A scala.util.Failure or a fallback response is returned. After resetTimeout
, it
transitions to HalfOpen
state.
HalfOpen
: the first element/request/call will be allowed through the main logic, if it succeeds the circuit
breaker will reset to Closed
state. If it fails, the circuit breaker will re-open to Open
state. All
elements/requests/calls beyond the first that execute while the first is running will fail-fast with
scala.util.Failure or a fallback response.
An ActorRef can be subscribed to receive certain events, e.g., TransitionEvents to receive all state transition events or specific state transition event like Open.
It collects metrics for success, failure and short-circuit counts. Also, exposes the state as a gauge.
A bidi GraphStage that is joined with a Flow to add Circuit Breaker functionality. When the joined Flow is having trouble responding in time or is responding with failures, then based on the provided settings, it short circuits the Flow: Instead of pushing an element to the joined Flow, it directly pushes down a Failure or a fallback response to downstream, given that there is downstream demand.
CircuitBreakerSettings.circuitBreakerState that keeps circuit breaker state. The user can select a CircuitBreakerState implementation that is right for the use case. Please note, in many scenarios, a CircuitBreakerState might need to be shared across materializations or even different streams. In such scenarios, make sure to select a CircuitBreakerState implementation that can work across materializations concurrently.
A CircuitBreakerSettings.fallback function provides an alternative response when circuit is OPEN.
The joined Flow pushes down a Try. By default, any Failure is considered a problem and causes the circuit breaker failure count to be incremented. However, CircuitBreakerSettings.failureDecider can be used to decide on if an element passed by the joined Flow is actually considered a failure. For instance, if Circuit Breaker is joined with an Akka HTTP flow, a Success Http Response with status code 500 internal server error should be considered a failure.
Emits when an element is available from the joined Flow or a short circuited element is available
Backpressures when the downstream backpressures
Completes when upstream completes
Cancels when downstream cancels
Please note, if the upstream does not control the throughput, then the throughput of the stream might temporarily increase once the circuit is OPEN: The downstream demand will be addressed with short circuit/fallback messages, which might (or might not) take less time than it takes the joined Flow to process an element. To eliminate this problem, a throttle can be applied specifically for circuit breaker related messages.
the type of the elements pulled from the upstream along with the Context and pushed down to joined flow
the type that's contained in a Try and pushed downstream along with the Context
the type of the context that is carried around along with the elements. The context may be of any type that can be used to uniquely identify each element