R
- result typepublic interface CircuitBreaker<R> extends Policy<R>
Circuit breakers have three states: closed, open, and half-open. When a circuit breaker is in
the closed (initial) state, executions are allowed. If a configurable number
of failures occur, optionally over some time period
, the circuit breaker transitions to the open state. In the open state a circuit
breaker will fail executions with CircuitBreakerOpenException
. After a configurable delay
, the circuit breaker will transition to a
half-open state. In the
half-open state a configurable number
of trial
executions will be allowed, after which the circuit breaker will transition back to closed or open
depending on how many were successful.
A circuit breaker can be count based or time based:
A minimum number of executions must be performed in order for a state transition to occur. Time based circuit
breakers use a sliding window to aggregate execution results. The window is divided into 10
time slices,
each representing 1/10th of the failureThresholdingPeriod
.
As time progresses, statistics for old time slices are gradually discarded, which smoothes the calculation of
success and failure rates.
This class is threadsafe.
Modifier and Type | Interface and Description |
---|---|
static class |
CircuitBreaker.State
The state of the circuit.
|
Modifier and Type | Method and Description |
---|---|
default void |
acquirePermit()
Attempts to acquire a permit for the circuit breaker and throws
CircuitBreakerOpenException if a permit
could not be acquired. |
static <R> CircuitBreakerBuilder<R> |
builder()
Creates a CircuitBreakerBuilder that by default will build a count based circuit breaker that opens after a
single failure , closes after a single success , and has a 1 minute delay , unless configured otherwise. |
static <R> CircuitBreakerBuilder<R> |
builder(CircuitBreakerConfig<R> config)
Creates a new CircuitBreakerBuilder that will be based on the
config . |
void |
close()
Closes the circuit.
|
CircuitBreakerConfig<R> |
getConfig()
Returns the
CircuitBreakerConfig that the CircuitBreaker was built with. |
int |
getExecutionCount()
Returns the number of executions recorded in the current state when the state is CLOSED or HALF_OPEN.
|
long |
getFailureCount()
Returns the number of failures recorded in the current state when the state is CLOSED or HALF_OPEN.
|
int |
getFailureRate()
The percentage rate of failed executions, from 0 to 100, in the current state when the state is CLOSED or
HALF_OPEN.
|
Duration |
getRemainingDelay()
When in the OPEN state, returns the remaining delay until the circuit is half-opened and allows another execution,
else returns
Duration.ZERO . |
CircuitBreaker.State |
getState()
Gets the state of the circuit.
|
int |
getSuccessCount()
Returns the number of successes recorded in the current state when the state is CLOSED or HALF_OPEN.
|
int |
getSuccessRate()
The percentage rate of successful executions, from 0 to 100, in the current state when the state is CLOSED or
HALF_OPEN.
|
void |
halfOpen()
Half-opens the circuit.
|
boolean |
isClosed()
Returns whether the circuit is closed.
|
boolean |
isHalfOpen()
Returns whether the circuit is half open.
|
boolean |
isOpen()
Returns whether the circuit is open.
|
static <R> CircuitBreaker<R> |
ofDefaults()
Creates a count based CircuitBreaker that opens after a
single failure , closes after a single success , and has a 1
minute delay by default. |
void |
open()
Opens the circuit.
|
void |
recordException(Throwable exception)
Records an
exception as a success or failure based on the exception configuration. |
void |
recordFailure()
Records an execution failure.
|
void |
recordResult(R result)
Records an execution
result as a success or failure based on the failure configuration. |
void |
recordSuccess()
Records an execution success.
|
boolean |
tryAcquirePermit()
Tries to acquire a permit to use the circuit breaker and returns whether a permit was acquired.
|
toExecutor
static <R> CircuitBreakerBuilder<R> builder()
single failure
, closes after a single success
, and has a 1 minute delay
, unless configured otherwise.ofDefaults()
static <R> CircuitBreakerBuilder<R> builder(CircuitBreakerConfig<R> config)
config
.static <R> CircuitBreaker<R> ofDefaults()
single failure
, closes after a single success
, and has a 1
minute delay
by default. To configure additional options on a
CircuitBreaker, use builder()
instead.builder()
CircuitBreakerConfig<R> getConfig()
CircuitBreakerConfig
that the CircuitBreaker was built with.default void acquirePermit()
CircuitBreakerOpenException
if a permit
could not be acquired. Permission will be automatically released when a result or failure is recorded.CircuitBreakerOpenException
- if the circuit breaker is in a half-open state and no permits remain according
to the configured success or failure thresholding capacity.tryAcquirePermit()
,
recordResult(Object)
,
recordException(Throwable)
,
recordSuccess()
,
recordFailure()
boolean tryAcquirePermit()
void open()
void close()
void halfOpen()
CircuitBreaker.State getState()
int getExecutionCount()
For count based thresholding, the max number of executions is limited to the execution threshold. For time based thresholds, the number of executions may vary within the thresholding period.
Duration getRemainingDelay()
Duration.ZERO
.long getFailureCount()
For count based thresholds, the max number of failures is based on the failure threshold
. For time based thresholds, the number of failures
may vary within the failure thresholding period
.
int getFailureRate()
The rate is based on the configured failure
thresholding capacity
.
int getSuccessCount()
The max number of successes is based on the success threshold
.
int getSuccessRate()
The rate is based on the configured success
thresholding capacity
.
boolean isClosed()
boolean isHalfOpen()
boolean isOpen()
void recordFailure()
void recordException(Throwable exception)
exception
as a success or failure based on the exception configuration.void recordResult(R result)
result
as a success or failure based on the failure configuration.void recordSuccess()
Copyright © 2022. All rights reserved.