Class CircuitBreakerStateMachine

  • All Implemented Interfaces:
    CircuitBreaker

    public final class CircuitBreakerStateMachine
    extends java.lang.Object
    implements CircuitBreaker
    A CircuitBreaker finite state machine.
    • Constructor Detail

      • CircuitBreakerStateMachine

        public CircuitBreakerStateMachine​(java.lang.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​(java.lang.String name,
                                          CircuitBreakerConfig circuitBreakerConfig,
                                          java.time.Clock clock)
        Creates a circuitBreaker.
        Parameters:
        name - the name of the CircuitBreaker
        circuitBreakerConfig - The CircuitBreaker configuration.
      • CircuitBreakerStateMachine

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

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

        public CircuitBreakerStateMachine​(java.lang.String name,
                                          CircuitBreakerConfig circuitBreakerConfig,
                                          io.vavr.collection.Map<java.lang.String,​java.lang.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​(java.lang.String name)
        Creates a circuitBreaker with default config.
        Parameters:
        name - the name of the CircuitBreaker
      • CircuitBreakerStateMachine

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

        public CircuitBreakerStateMachine​(java.lang.String name,
                                          java.util.function.Supplier<CircuitBreakerConfig> circuitBreakerConfig,
                                          io.vavr.collection.Map<java.lang.String,​java.lang.String> tags)
        Creates a circuitBreaker.
        Parameters:
        name - the name of the CircuitBreaker
        circuitBreakerConfig - The CircuitBreaker configuration supplier.
    • Method Detail

      • 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 java.util.concurrent.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
      • 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,
                            java.util.concurrent.TimeUnit durationUnit,
                            java.lang.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,
                              java.util.concurrent.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,
                             java.util.concurrent.TimeUnit durationUnit,
                             @Nullable
                             java.lang.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
      • getName

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

        public io.vavr.collection.Map<java.lang.String,​java.lang.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 java.lang.String toString()
        Overrides:
        toString in class java.lang.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 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
      • 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