Package dev.failsafe.internal
Class CircuitBreakerImpl<R>
java.lang.Object
dev.failsafe.internal.CircuitBreakerImpl<R>
- Type Parameters:
R
- result type
- All Implemented Interfaces:
CircuitBreaker<R>
,Policy<R>
,DelayablePolicy<R>
,FailurePolicy<R>
public class CircuitBreakerImpl<R>
extends Object
implements CircuitBreaker<R>, FailurePolicy<R>, DelayablePolicy<R>
A
CircuitBreaker
implementation.- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface dev.failsafe.CircuitBreaker
CircuitBreaker.State
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final CircuitBreakerConfig
<R> protected final AtomicReference
<CircuitState<R>> Writes guarded by "this" -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the circuit.Returns theCircuitBreakerConfig
that the CircuitBreaker was built with.int
Returns the number of executions recorded in the current state when the state is CLOSED or HALF_OPEN.long
Returns the number of failures recorded in the current state when the state is CLOSED or HALF_OPEN.int
The percentage rate of failed executions, from 0 to 100, in the current state when the state is CLOSED or HALF_OPEN.When in the OPEN state, returns the remaining delay until the circuit is half-opened and allows another execution, else returnsDuration.ZERO
.getState()
Gets the state of the circuit.int
Returns the number of successes recorded in the current state when the state is CLOSED or HALF_OPEN.int
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
Returns whether the circuit is half open.boolean
isOpen()
Returns whether the circuit is open.void
open()
Opens the circuit.protected void
open
(ExecutionContext<R> context) Opens the circuit breaker and considers thecontext
when computing the delay before the circuit breaker will transition to half open.void
recordException
(Throwable exception) Records anexception
as a success or failure based on the failure configuration.protected void
recordExecutionFailure
(ExecutionContext<R> context) Records an execution failure.void
Records an execution failure.void
recordResult
(R result) Records an executionresult
as a success or failure based on the failure configuration.protected void
recordResult
(R result, Throwable exception) void
Records an execution success.toExecutor
(int policyIndex) Returns aPolicyExecutor
capable of handling an execution for the Policy.toString()
protected void
transitionTo
(CircuitBreaker.State newState, EventListener<CircuitBreakerStateChangedEvent> listener, ExecutionContext<R> context) Transitions to thenewState
if not already in that state and calls any associated event listener.boolean
Tries to acquire a permit to use the circuit breaker and returns whether a permit was acquired.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface dev.failsafe.CircuitBreaker
acquirePermit
Methods inherited from interface dev.failsafe.spi.DelayablePolicy
computeDelay
Methods inherited from interface dev.failsafe.spi.FailurePolicy
isFailure
-
Field Details
-
config
-
state
Writes guarded by "this"
-
-
Constructor Details
-
CircuitBreakerImpl
-
-
Method Details
-
getConfig
Description copied from interface:CircuitBreaker
Returns theCircuitBreakerConfig
that the CircuitBreaker was built with.- Specified by:
getConfig
in interfaceCircuitBreaker<R>
- Specified by:
getConfig
in interfaceDelayablePolicy<R>
- Specified by:
getConfig
in interfaceFailurePolicy<R>
- Specified by:
getConfig
in interfacePolicy<R>
-
tryAcquirePermit
public boolean tryAcquirePermit()Description copied from interface:CircuitBreaker
Tries to acquire a permit to use the circuit breaker and returns whether a permit was acquired. Permission will be automatically released when a result or failure is recorded.- Specified by:
tryAcquirePermit
in interfaceCircuitBreaker<R>
- See Also:
-
close
public void close()Description copied from interface:CircuitBreaker
Closes the circuit.- Specified by:
close
in interfaceCircuitBreaker<R>
-
getState
Description copied from interface:CircuitBreaker
Gets the state of the circuit.- Specified by:
getState
in interfaceCircuitBreaker<R>
-
getExecutionCount
public int getExecutionCount()Description copied from interface:CircuitBreaker
Returns the number of executions recorded in the current state when the state is CLOSED or HALF_OPEN. When the state is OPEN, returns the executions recorded during the previous CLOSED state.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.
- Specified by:
getExecutionCount
in interfaceCircuitBreaker<R>
-
getRemainingDelay
Description copied from interface:CircuitBreaker
When in the OPEN state, returns the remaining delay until the circuit is half-opened and allows another execution, else returnsDuration.ZERO
.- Specified by:
getRemainingDelay
in interfaceCircuitBreaker<R>
-
getFailureCount
public long getFailureCount()Description copied from interface:CircuitBreaker
Returns the number of failures recorded in the current state when the state is CLOSED or HALF_OPEN. When the state is OPEN, returns the failures recorded during the previous CLOSED state.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 thefailure thresholding period
.- Specified by:
getFailureCount
in interfaceCircuitBreaker<R>
-
getFailureRate
public int getFailureRate()Description copied from interface:CircuitBreaker
The percentage rate of failed executions, from 0 to 100, in the current state when the state is CLOSED or HALF_OPEN. When the state is OPEN, returns the rate recorded during the previous CLOSED state.The rate is based on the configured
failure thresholding capacity
.- Specified by:
getFailureRate
in interfaceCircuitBreaker<R>
-
getSuccessCount
public int getSuccessCount()Description copied from interface:CircuitBreaker
Returns the number of successes recorded in the current state when the state is CLOSED or HALF_OPEN. When the state is OPEN, returns the successes recorded during the previous CLOSED state.The max number of successes is based on the
success threshold
.- Specified by:
getSuccessCount
in interfaceCircuitBreaker<R>
-
getSuccessRate
public int getSuccessRate()Description copied from interface:CircuitBreaker
The percentage rate of successful executions, from 0 to 100, in the current state when the state is CLOSED or HALF_OPEN. When the state is OPEN, returns the rate recorded during the previous CLOSED state.The rate is based on the configured
success thresholding capacity
.- Specified by:
getSuccessRate
in interfaceCircuitBreaker<R>
-
halfOpen
public void halfOpen()Description copied from interface:CircuitBreaker
Half-opens the circuit.- Specified by:
halfOpen
in interfaceCircuitBreaker<R>
-
isClosed
public boolean isClosed()Description copied from interface:CircuitBreaker
Returns whether the circuit is closed.- Specified by:
isClosed
in interfaceCircuitBreaker<R>
-
isHalfOpen
public boolean isHalfOpen()Description copied from interface:CircuitBreaker
Returns whether the circuit is half open.- Specified by:
isHalfOpen
in interfaceCircuitBreaker<R>
-
isOpen
public boolean isOpen()Description copied from interface:CircuitBreaker
Returns whether the circuit is open.- Specified by:
isOpen
in interfaceCircuitBreaker<R>
-
open
public void open()Description copied from interface:CircuitBreaker
Opens the circuit.- Specified by:
open
in interfaceCircuitBreaker<R>
-
recordFailure
public void recordFailure()Description copied from interface:CircuitBreaker
Records an execution failure.- Specified by:
recordFailure
in interfaceCircuitBreaker<R>
-
recordException
Description copied from interface:CircuitBreaker
Records anexception
as a success or failure based on the failure configuration.- Specified by:
recordException
in interfaceCircuitBreaker<R>
-
recordResult
Description copied from interface:CircuitBreaker
Records an executionresult
as a success or failure based on the failure configuration.- Specified by:
recordResult
in interfaceCircuitBreaker<R>
-
recordSuccess
public void recordSuccess()Description copied from interface:CircuitBreaker
Records an execution success.- Specified by:
recordSuccess
in interfaceCircuitBreaker<R>
-
toString
-
recordResult
-
transitionTo
protected void transitionTo(CircuitBreaker.State newState, EventListener<CircuitBreakerStateChangedEvent> listener, ExecutionContext<R> context) Transitions to thenewState
if not already in that state and calls any associated event listener. -
recordExecutionFailure
Records an execution failure. -
open
Opens the circuit breaker and considers thecontext
when computing the delay before the circuit breaker will transition to half open. -
toExecutor
Description copied from interface:Policy
Returns aPolicyExecutor
capable of handling an execution for the Policy.- Specified by:
toExecutor
in interfacePolicy<R>
-