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>
ACircuitBreakerimplementation.- 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>configprotected java.util.concurrent.atomic.AtomicReference<CircuitState<R>>stateWrites 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 voidclose()Closes the circuit.CircuitBreakerConfig<R>getConfig()Returns theCircuitBreakerConfigthat the CircuitBreaker was built with.intgetExecutionCount()Returns the number of executions recorded in the current state when the state is CLOSED or HALF_OPEN.longgetFailureCount()Returns the number of failures recorded in the current state when the state is CLOSED or HALF_OPEN.intgetFailureRate()The percentage rate of failed executions, from 0 to 100, in the current state when the state is CLOSED or HALF_OPEN.java.time.DurationgetRemainingDelay()When in the OPEN state, returns the remaining delay until the circuit is half-opened and allows another execution, else returnsDuration.ZERO.CircuitBreaker.StategetState()Gets the state of the circuit.intgetSuccessCount()Returns the number of successes recorded in the current state when the state is CLOSED or HALF_OPEN.intgetSuccessRate()The percentage rate of successful executions, from 0 to 100, in the current state when the state is CLOSED or HALF_OPEN.voidhalfOpen()Half-opens the circuit.booleanisClosed()Returns whether the circuit is closed.booleanisHalfOpen()Returns whether the circuit is half open.booleanisOpen()Returns whether the circuit is open.voidopen()Opens the circuit.protected voidopen(ExecutionContext<R> context)Opens the circuit breaker and considers thecontextwhen computing the delay before the circuit breaker will transition to half open.voidrecordException(java.lang.Throwable exception)Records anexceptionas a success or failure based on the failure configuration.protected voidrecordExecutionFailure(ExecutionContext<R> context)Records an execution failure.voidrecordFailure()Records an execution failure.voidrecordResult(R result)Records an executionresultas a success or failure based on the failure configuration.protected voidrecordResult(R result, java.lang.Throwable exception)voidrecordSuccess()Records an execution success.PolicyExecutor<R>toExecutor(int policyIndex)Returns aPolicyExecutorcapable of handling an execution for the Policy.java.lang.StringtoString()protected voidtransitionTo(CircuitBreaker.State newState, EventListener<CircuitBreakerStateChangedEvent> listener, ExecutionContext<R> context)Transitions to thenewStateif not already in that state and calls any associated event listener.booleantryAcquirePermit()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:CircuitBreakerReturns theCircuitBreakerConfigthat the CircuitBreaker was built with.- Specified by:
getConfigin interfaceCircuitBreaker<R>- Specified by:
getConfigin interfaceDelayablePolicy<R>- Specified by:
getConfigin interfaceFailurePolicy<R>- Specified by:
getConfigin interfacePolicy<R>
-
tryAcquirePermit
public boolean tryAcquirePermit()
Description copied from interface:CircuitBreakerTries 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:
tryAcquirePermitin interfaceCircuitBreaker<R>- See Also:
CircuitBreaker.recordResult(Object),CircuitBreaker.recordException(Throwable),CircuitBreaker.recordSuccess(),CircuitBreaker.recordFailure()
-
close
public void close()
Description copied from interface:CircuitBreakerCloses the circuit.- Specified by:
closein interfaceCircuitBreaker<R>
-
getState
public CircuitBreaker.State getState()
Description copied from interface:CircuitBreakerGets the state of the circuit.- Specified by:
getStatein interfaceCircuitBreaker<R>
-
getExecutionCount
public int getExecutionCount()
Description copied from interface:CircuitBreakerReturns 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:
getExecutionCountin interfaceCircuitBreaker<R>
-
getRemainingDelay
public java.time.Duration getRemainingDelay()
Description copied from interface:CircuitBreakerWhen in the OPEN state, returns the remaining delay until the circuit is half-opened and allows another execution, else returnsDuration.ZERO.- Specified by:
getRemainingDelayin interfaceCircuitBreaker<R>
-
getFailureCount
public long getFailureCount()
Description copied from interface:CircuitBreakerReturns 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:
getFailureCountin interfaceCircuitBreaker<R>
-
getFailureRate
public int getFailureRate()
Description copied from interface:CircuitBreakerThe 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:
getFailureRatein interfaceCircuitBreaker<R>
-
getSuccessCount
public int getSuccessCount()
Description copied from interface:CircuitBreakerReturns 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:
getSuccessCountin interfaceCircuitBreaker<R>
-
getSuccessRate
public int getSuccessRate()
Description copied from interface:CircuitBreakerThe 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:
getSuccessRatein interfaceCircuitBreaker<R>
-
halfOpen
public void halfOpen()
Description copied from interface:CircuitBreakerHalf-opens the circuit.- Specified by:
halfOpenin interfaceCircuitBreaker<R>
-
isClosed
public boolean isClosed()
Description copied from interface:CircuitBreakerReturns whether the circuit is closed.- Specified by:
isClosedin interfaceCircuitBreaker<R>
-
isHalfOpen
public boolean isHalfOpen()
Description copied from interface:CircuitBreakerReturns whether the circuit is half open.- Specified by:
isHalfOpenin interfaceCircuitBreaker<R>
-
isOpen
public boolean isOpen()
Description copied from interface:CircuitBreakerReturns whether the circuit is open.- Specified by:
isOpenin interfaceCircuitBreaker<R>
-
open
public void open()
Description copied from interface:CircuitBreakerOpens the circuit.- Specified by:
openin interfaceCircuitBreaker<R>
-
recordFailure
public void recordFailure()
Description copied from interface:CircuitBreakerRecords an execution failure.- Specified by:
recordFailurein interfaceCircuitBreaker<R>
-
recordException
public void recordException(java.lang.Throwable exception)
Description copied from interface:CircuitBreakerRecords anexceptionas a success or failure based on the failure configuration.- Specified by:
recordExceptionin interfaceCircuitBreaker<R>
-
recordResult
public void recordResult(R result)
Description copied from interface:CircuitBreakerRecords an executionresultas a success or failure based on the failure configuration.- Specified by:
recordResultin interfaceCircuitBreaker<R>
-
recordSuccess
public void recordSuccess()
Description copied from interface:CircuitBreakerRecords an execution success.- Specified by:
recordSuccessin interfaceCircuitBreaker<R>
-
toString
public java.lang.String toString()
- Overrides:
toStringin 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 thenewStateif 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 thecontextwhen computing the delay before the circuit breaker will transition to half open.
-
toExecutor
public PolicyExecutor<R> toExecutor(int policyIndex)
Description copied from interface:PolicyReturns aPolicyExecutorcapable of handling an execution for the Policy.- Specified by:
toExecutorin interfacePolicy<R>
-
-