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 java.lang.Object implements CircuitBreaker<R>, FailurePolicy<R>, DelayablePolicy<R>
ACircuitBreaker
implementation.- See Also:
CircuitBreakerBuilder
,CircuitBreakerOpenException
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface dev.failsafe.CircuitBreaker
CircuitBreaker.State
-
-
Field Summary
Fields Modifier and Type Field Description private CircuitBreakerConfig<R>
config
protected java.util.concurrent.atomic.AtomicReference<CircuitState<R>>
state
Writes guarded by "this"
-
Constructor Summary
Constructors Constructor Description CircuitBreakerImpl(CircuitBreakerConfig<R> config)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes the circuit.CircuitBreakerConfig<R>
getConfig()
Returns theCircuitBreakerConfig
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.java.time.Duration
getRemainingDelay()
When in the OPEN state, returns the remaining delay until the circuit is half-opened and allows another execution, else returnsDuration.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.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(java.lang.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
recordFailure()
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, java.lang.Throwable exception)
void
recordSuccess()
Records an execution success.PolicyExecutor<R>
toExecutor(int policyIndex)
Returns aPolicyExecutor
capable of handling an execution for the Policy.java.lang.String
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
tryAcquirePermit()
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 Detail
-
config
private final CircuitBreakerConfig<R> config
-
state
protected final java.util.concurrent.atomic.AtomicReference<CircuitState<R>> state
Writes guarded by "this"
-
-
Constructor Detail
-
CircuitBreakerImpl
public CircuitBreakerImpl(CircuitBreakerConfig<R> config)
-
-
Method Detail
-
getConfig
public CircuitBreakerConfig<R> 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:
CircuitBreaker.recordResult(Object)
,CircuitBreaker.recordException(Throwable)
,CircuitBreaker.recordSuccess()
,CircuitBreaker.recordFailure()
-
close
public void close()
Description copied from interface:CircuitBreaker
Closes the circuit.- Specified by:
close
in interfaceCircuitBreaker<R>
-
getState
public CircuitBreaker.State 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
public java.time.Duration 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
public void recordException(java.lang.Throwable exception)
Description copied from interface:CircuitBreaker
Records anexception
as a success or failure based on the failure configuration.- Specified by:
recordException
in interfaceCircuitBreaker<R>
-
recordResult
public void recordResult(R result)
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
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
recordResult
protected void recordResult(R result, java.lang.Throwable exception)
-
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
protected void recordExecutionFailure(ExecutionContext<R> context)
Records an execution failure.
-
open
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.
-
toExecutor
public PolicyExecutor<R> toExecutor(int policyIndex)
Description copied from interface:Policy
Returns aPolicyExecutor
capable of handling an execution for the Policy.- Specified by:
toExecutor
in interfacePolicy<R>
-
-