Class CircuitBreakerStateMachine

java.lang.Object
io.github.resilience4j.circuitbreaker.internal.CircuitBreakerStateMachine
All Implemented Interfaces:
CircuitBreaker

public final class CircuitBreakerStateMachine extends Object implements CircuitBreaker
A CircuitBreaker finite state machine.
  • Constructor Details

    • CircuitBreakerStateMachine

      public CircuitBreakerStateMachine(String name, CircuitBreakerConfig circuitBreakerConfig, SchedulerFactory schedulerFactory)
      Creates a circuitBreaker.
      Parameters:
      name - the name of the CircuitBreaker
      circuitBreakerConfig - The CircuitBreaker configuration.
      schedulerFactory - A SchedulerFactory which can be mocked in tests.
    • CircuitBreakerStateMachine

      public CircuitBreakerStateMachine(String name, CircuitBreakerConfig circuitBreakerConfig, Clock clock)
      Creates a circuitBreaker.
      Parameters:
      name - the name of the CircuitBreaker
      circuitBreakerConfig - The CircuitBreaker configuration.
    • CircuitBreakerStateMachine

      public CircuitBreakerStateMachine(String name, CircuitBreakerConfig circuitBreakerConfig, Clock clock, Map<String,String> tags)
      Creates a circuitBreaker.
      Parameters:
      name - the name of the CircuitBreaker
      circuitBreakerConfig - The CircuitBreaker configuration.
    • CircuitBreakerStateMachine

      public CircuitBreakerStateMachine(String name, CircuitBreakerConfig circuitBreakerConfig)
      Creates a circuitBreaker.
      Parameters:
      name - the name of the CircuitBreaker
      circuitBreakerConfig - The CircuitBreaker configuration.
    • CircuitBreakerStateMachine

      public CircuitBreakerStateMachine(String name, CircuitBreakerConfig circuitBreakerConfig, Map<String,String> tags)
      Creates a circuitBreaker.
      Parameters:
      name - the name of the CircuitBreaker
      circuitBreakerConfig - The CircuitBreaker configuration.
      tags - Tags to add to the CircuitBreaker.
    • CircuitBreakerStateMachine

      public CircuitBreakerStateMachine(String name)
      Creates a circuitBreaker with default config.
      Parameters:
      name - the name of the CircuitBreaker
    • CircuitBreakerStateMachine

      public CircuitBreakerStateMachine(String name, Supplier<CircuitBreakerConfig> circuitBreakerConfig)
      Creates a circuitBreaker.
      Parameters:
      name - the name of the CircuitBreaker
      circuitBreakerConfig - The CircuitBreaker configuration supplier.
    • CircuitBreakerStateMachine

      public CircuitBreakerStateMachine(String name, Supplier<CircuitBreakerConfig> circuitBreakerConfig, Map<String,String> tags)
      Creates a circuitBreaker.
      Parameters:
      name - the name of the CircuitBreaker
      circuitBreakerConfig - The CircuitBreaker configuration supplier.
  • Method Details

    • getCurrentTimestamp

      public long getCurrentTimestamp()
      Description copied from interface: CircuitBreaker
      Returns the current time with respect to the CircuitBreaker currentTimeFunction. Returns System.nanoTime() by default.
      Specified by:
      getCurrentTimestamp in interface CircuitBreaker
      Returns:
      current timestamp
    • getTimestampUnit

      public TimeUnit getTimestampUnit()
      Description copied from interface: CircuitBreaker
      Returns the timeUnit of current timestamp. Default is TimeUnit.NANOSECONDS.
      Specified by:
      getTimestampUnit in interface CircuitBreaker
      Returns:
      the timeUnit of current timestamp
    • tryAcquirePermission

      public boolean tryAcquirePermission()
      Description copied from interface: CircuitBreaker
      Acquires a permission to execute a call, only if one is available at the time of invocation. If a call is not permitted, the number of not permitted calls is increased.

      Returns false when the state is OPEN or FORCED_OPEN. Returns true when the state is CLOSED or DISABLED. Returns true when the state is HALF_OPEN and further test calls are allowed. Returns false when the state is HALF_OPEN and the number of test calls has been reached. If the state is HALF_OPEN, the number of allowed test calls is decreased. Important: Make sure to call onSuccess or onError after the call is finished. If the call is cancelled before it is invoked, you have to release the permission again.

      Specified by:
      tryAcquirePermission in interface CircuitBreaker
      Returns:
      true if a permission was acquired and false otherwise
    • releasePermission

      public void releasePermission()
      Description copied from interface: CircuitBreaker
      Releases a permission.

      Should only be used when a permission was acquired but not used. Otherwise use CircuitBreaker.onSuccess(long, TimeUnit) or CircuitBreaker.onError(long, TimeUnit, Throwable) to signal a completed or failed call.

      If the state is HALF_OPEN, the number of allowed test calls is increased by one.

      Specified by:
      releasePermission in interface CircuitBreaker
    • acquirePermission

      public void acquirePermission()
      Description copied from interface: CircuitBreaker
      Try to obtain a permission to execute a call. If a call is not permitted, the number of not permitted calls is increased.

      Throws a CallNotPermittedException when the state is OPEN or FORCED_OPEN. Returns when the state is CLOSED or DISABLED. Returns when the state is HALF_OPEN and further test calls are allowed. Throws a CallNotPermittedException when the state is HALF_OPEN and the number of test calls has been reached. If the state is HALF_OPEN, the number of allowed test calls is decreased. Important: Make sure to call onSuccess or onError after the call is finished. If the call is cancelled before it is invoked, you have to release the permission again.

      Specified by:
      acquirePermission in interface CircuitBreaker
    • onError

      public void onError(long duration, TimeUnit durationUnit, Throwable throwable)
      Description copied from interface: CircuitBreaker
      Records a failed call. This method must be invoked when a call failed.
      Specified by:
      onError in interface CircuitBreaker
      Parameters:
      duration - The elapsed time duration of the call
      durationUnit - The duration unit
      throwable - The throwable which must be recorded
    • onSuccess

      public void onSuccess(long duration, TimeUnit durationUnit)
      Description copied from interface: CircuitBreaker
      Records a successful call. This method must be invoked when a call was successful.
      Specified by:
      onSuccess in interface CircuitBreaker
      Parameters:
      duration - The elapsed time duration of the call
      durationUnit - The duration unit
    • onResult

      public void onResult(long duration, TimeUnit durationUnit, @Nullable Object result)
      Description copied from interface: CircuitBreaker
      This method must be invoked when a call returned a result and the result predicate should decide if the call was successful or not.
      Specified by:
      onResult in interface CircuitBreaker
      Parameters:
      duration - The elapsed time duration of the call
      durationUnit - The duration unit
      result - The result of the protected function
    • getState

      public CircuitBreaker.State getState()
      Get the state of this CircuitBreaker.
      Specified by:
      getState in interface CircuitBreaker
      Returns:
      the state of this CircuitBreaker
    • getName

      public String getName()
      Get the name of this CircuitBreaker.
      Specified by:
      getName in interface CircuitBreaker
      Returns:
      the name of this CircuitBreaker
    • getCircuitBreakerConfig

      public CircuitBreakerConfig getCircuitBreakerConfig()
      Get the config of this CircuitBreaker.
      Specified by:
      getCircuitBreakerConfig in interface CircuitBreaker
      Returns:
      the config of this CircuitBreaker
    • getMetrics

      public CircuitBreaker.Metrics getMetrics()
      Description copied from interface: CircuitBreaker
      Returns the Metrics of this CircuitBreaker.
      Specified by:
      getMetrics in interface CircuitBreaker
      Returns:
      the Metrics of this CircuitBreaker
    • getTags

      public Map<String,String> getTags()
      Description copied from interface: CircuitBreaker
      Returns an unmodifiable map with tags assigned to this Retry.
      Specified by:
      getTags in interface CircuitBreaker
      Returns:
      the tags assigned to this Retry in an unmodifiable map
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • reset

      public void reset()
      Description copied from interface: CircuitBreaker
      Returns the circuit breaker to its original closed state, losing statistics.

      Should only be used, when you want to fully reset the circuit breaker without creating a new one.

      Specified by:
      reset in interface CircuitBreaker
    • transitionToDisabledState

      public void transitionToDisabledState()
      Description copied from interface: CircuitBreaker
      Transitions the state machine to a DISABLED state, stopping state transition, metrics and event publishing. This call is idempotent and will not have any effect if the state machine is already in DISABLED state.

      Should only be used, when you want to disable the circuit breaker allowing all calls to pass. To recover from this state you must force a new state transition

      Specified by:
      transitionToDisabledState in interface CircuitBreaker
    • transitionToMetricsOnlyState

      public void transitionToMetricsOnlyState()
      Description copied from interface: CircuitBreaker
      Transitions the state machine to METRICS_ONLY state, stopping all state transitions but continues to capture metrics and publish events. This call is idempotent and will not have any effect if the state machine is already in METRICS_ONLY state.

      Should only be used when you want to collect metrics while keeping the circuit breaker disabled, allowing all calls to pass. To recover from this state you must force a new state transition.

      Specified by:
      transitionToMetricsOnlyState in interface CircuitBreaker
    • transitionToForcedOpenState

      public void transitionToForcedOpenState()
      Description copied from interface: CircuitBreaker
      Transitions the state machine to a FORCED_OPEN state, stopping state transition, metrics and event publishing. This call is idempotent and will not have any effect if the state machine is already in FORCED_OPEN state.

      Should only be used, when you want to disable the circuit breaker allowing no call to pass. To recover from this state you must force a new state transition

      Specified by:
      transitionToForcedOpenState in interface CircuitBreaker
    • transitionToClosedState

      public void transitionToClosedState()
      Description copied from interface: CircuitBreaker
      Transitions the state machine to CLOSED state. This call is idempotent and will not have any effect if the state machine is already in CLOSED state.

      Should only be used, when you want to force a state transition. State transition are normally done internally.

      Specified by:
      transitionToClosedState in interface CircuitBreaker
    • transitionToOpenState

      public void transitionToOpenState()
      Description copied from interface: CircuitBreaker
      Transitions the state machine to OPEN state. This call is idempotent and will not have any effect if the state machine is already in OPEN state.

      Should only be used, when you want to force a state transition. State transition are normally done internally.

      Specified by:
      transitionToOpenState in interface CircuitBreaker
    • transitionToOpenStateFor

      public void transitionToOpenStateFor(Duration waitDuration)
      Description copied from interface: CircuitBreaker
      Same as CircuitBreaker.transitionToOpenState() but waits in open state for the given amount of time instead of relaying on configurations to determine it.
      Specified by:
      transitionToOpenStateFor in interface CircuitBreaker
      Parameters:
      waitDuration - how long should we wait in open state
    • transitionToOpenStateUntil

      public void transitionToOpenStateUntil(Instant waitUntil)
      Description copied from interface: CircuitBreaker
      Same as CircuitBreaker.transitionToOpenState() but waits in open state until the given in time instead of relaying on configurations to determine it.
      Specified by:
      transitionToOpenStateUntil in interface CircuitBreaker
      Parameters:
      waitUntil - how long should we wait in open state
    • transitionToHalfOpenState

      public void transitionToHalfOpenState()
      Description copied from interface: CircuitBreaker
      Transitions the state machine to HALF_OPEN state. This call is idempotent and will not have any effect if the state machine is already in HALF_OPEN state.

      Should only be used, when you want to force a state transition. State transition are normally done internally.

      Specified by:
      transitionToHalfOpenState in interface CircuitBreaker
    • getEventPublisher

      public CircuitBreaker.EventPublisher getEventPublisher()
      Description copied from interface: CircuitBreaker
      Returns an EventPublisher which can be used to register event consumers.
      Specified by:
      getEventPublisher in interface CircuitBreaker
      Returns:
      an EventPublisher