Class CircuitBreakerConfig.Builder

java.lang.Object
io.github.resilience4j.circuitbreaker.CircuitBreakerConfig.Builder
Enclosing class:
CircuitBreakerConfig

public static class CircuitBreakerConfig.Builder extends Object
  • Constructor Details

  • Method Details

    • failureRateThreshold

      public CircuitBreakerConfig.Builder failureRateThreshold(float failureRateThreshold)
      Configures the failure rate threshold in percentage. If the failure rate is equal to or greater than the threshold, the CircuitBreaker transitions to open and starts short-circuiting calls.

      The threshold must be greater than 0 and not greater than 100. Default value is 50 percentage.

      Parameters:
      failureRateThreshold - the failure rate threshold in percentage
      Returns:
      the CircuitBreakerConfig.Builder
      Throws:
      IllegalArgumentException - if failureRateThreshold <= 0 || failureRateThreshold > 100
    • slowCallRateThreshold

      public CircuitBreakerConfig.Builder slowCallRateThreshold(float slowCallRateThreshold)
      Configures a threshold in percentage. The CircuitBreaker considers a call as slow when the call duration is greater than slowCallDurationThreshold(Duration). When the percentage of slow calls is equal to or greater than the threshold, the CircuitBreaker transitions to open and starts short-circuiting calls.

      The threshold must be greater than 0 and not greater than 100. Default value is 100 percentage which means that all recorded calls must be slower than slowCallDurationThreshold(Duration).

      Parameters:
      slowCallRateThreshold - the slow calls threshold in percentage
      Returns:
      the CircuitBreakerConfig.Builder
      Throws:
      IllegalArgumentException - if slowCallRateThreshold <= 0 || slowCallRateThreshold > 100
    • writableStackTraceEnabled

      public CircuitBreakerConfig.Builder writableStackTraceEnabled(boolean writableStackTraceEnabled)
      Enables writable stack traces. When set to false, Throwable.getStackTrace() returns a zero length array. This may be used to reduce log spam when the circuit breaker is open as the cause of the exceptions is already known (the circuit breaker is short-circuiting calls).
      Parameters:
      writableStackTraceEnabled - the flag to enable writable stack traces.
      Returns:
      the CircuitBreakerConfig.Builder
    • waitDurationInOpenState

      public CircuitBreakerConfig.Builder waitDurationInOpenState(Duration waitDurationInOpenState)
      Configures an interval function with a fixed wait duration which controls how long the CircuitBreaker should stay open, before it switches to half open. Default value is 60 seconds.

      Do not use with waitIntervalFunctionInOpenState(IntervalFunction)! Please, when using, make sure not to override the value set earlier from the waitIntervalFunctionInOpenState(IntervalFunction)

      Parameters:
      waitDurationInOpenState - the wait duration which specifies how long the CircuitBreaker should stay open
      Returns:
      the CircuitBreakerConfig.Builder
      Throws:
      IllegalArgumentException - if waitDurationInOpenState.toMillis() < 1
    • waitIntervalFunctionInOpenState

      public CircuitBreakerConfig.Builder waitIntervalFunctionInOpenState(io.github.resilience4j.core.IntervalFunction waitIntervalFunctionInOpenState)
      Configures an interval function which controls how long the CircuitBreaker should stay open, before it switches to half open. The default interval function returns a fixed wait duration of 60 seconds.

      A custom interval function is useful if you need an exponential backoff algorithm.

      Do not use with waitDurationInOpenState(Duration)! Please, when using, make sure not to override the value set earlier from the waitDurationInOpenState(Duration)

      Parameters:
      waitIntervalFunctionInOpenState - Interval function that returns wait time as a function of attempts
      Returns:
      the CircuitBreakerConfig.Builder
    • transitionOnResult

      public CircuitBreakerConfig.Builder transitionOnResult(Function<io.github.resilience4j.core.functions.Either<Object,Throwable>,CircuitBreakerConfig.TransitionCheckResult> transitionOnResult)
      Configures an function which can decide if the circuit breaker should transition to a different state base on the result of the protected function.
      Parameters:
      transitionOnResult - function which instructs the circuit breaker if it should transition to a different state
      Returns:
      the CircuitBreakerConfig.Builder
    • slowCallDurationThreshold

      public CircuitBreakerConfig.Builder slowCallDurationThreshold(Duration slowCallDurationThreshold)
      Configures the duration threshold above which calls are considered as slow and increase the slow calls percentage. Default value is 60 seconds.
      Parameters:
      slowCallDurationThreshold - the duration above which calls are considered as slow
      Returns:
      the CircuitBreakerConfig.Builder
      Throws:
      IllegalArgumentException - if slowCallDurationThreshold.toNanos() < 1
    • maxWaitDurationInHalfOpenState

      public CircuitBreakerConfig.Builder maxWaitDurationInHalfOpenState(Duration maxWaitDurationInHalfOpenState)
      Configures CircuitBreaker with a fixed wait duration which controls how long the CircuitBreaker should stay in Half Open state, before it switches to open. This is an optional parameter. By default CircuitBreaker will stay in Half Open state until minimumNumberOfCalls is completed with either success or failure.
      Parameters:
      maxWaitDurationInHalfOpenState - the wait duration which specifies how long the CircuitBreaker should stay in Half Open
      Returns:
      the CircuitBreakerConfig.Builder
      Throws:
      IllegalArgumentException - if maxWaitDurationInHalfOpenState.toMillis() < 0
    • permittedNumberOfCallsInHalfOpenState

      public CircuitBreakerConfig.Builder permittedNumberOfCallsInHalfOpenState(int permittedNumberOfCallsInHalfOpenState)
      Configures the number of permitted calls when the CircuitBreaker is half open.

      The size must be greater than 0. Default size is 10.

      Parameters:
      permittedNumberOfCallsInHalfOpenState - the permitted number of calls when the CircuitBreaker is half open
      Returns:
      the CircuitBreakerConfig.Builder
      Throws:
      IllegalArgumentException - if permittedNumberOfCallsInHalfOpenState < 1
    • slidingWindow

      public CircuitBreakerConfig.Builder slidingWindow(int slidingWindowSize, int minimumNumberOfCalls, CircuitBreakerConfig.SlidingWindowType slidingWindowType)
      Configures the sliding window which is used to record the outcome of calls when the CircuitBreaker is closed. slidingWindowSize configures the size of the sliding window. Sliding window can either be count-based or time-based, specified by slidingWindowType. minimumNumberOfCalls configures the minimum number of calls which are required (per sliding window period) before the CircuitBreaker can calculate the error rate. For example, if minimumNumberOfCalls is 10, then at least 10 calls must be recorded, before the failure rate can be calculated. If only 9 calls have been recorded, the CircuitBreaker will not transition to open, even if all 9 calls have failed.

      If slidingWindowSize is 100 and slidingWindowType is COUNT_BASED, the last 100 calls are recorded and aggregated. If slidingWindowSize is 10 and slidingWindowType is TIME_BASED, the calls of the last 10 seconds are recorded and aggregated.

      The slidingWindowSize must be greater than 0. The minimumNumberOfCalls must be greater than 0. If the slidingWindowType is COUNT_BASED, the minimumNumberOfCalls may not be greater than slidingWindowSize. If a greater value is provided, minimumNumberOfCalls will be equal to slidingWindowSize. If the slidingWindowType is TIME_BASED, the minimumNumberOfCalls may be any amount.

      Default slidingWindowSize is 100, minimumNumberOfCalls is 100 and slidingWindowType is COUNT_BASED.

      Parameters:
      slidingWindowSize - the size of the sliding window when the CircuitBreaker is closed.
      minimumNumberOfCalls - the minimum number of calls that must be recorded before the failure rate can be calculated.
      slidingWindowType - the type of the sliding window. Either COUNT_BASED or TIME_BASED.
      Returns:
      the CircuitBreakerConfig.Builder
      Throws:
      IllegalArgumentException - if slidingWindowSize < 1 || minimumNumberOfCalls < 1
    • slidingWindowSize

      public CircuitBreakerConfig.Builder slidingWindowSize(int slidingWindowSize)
      Configures the size of the sliding window which is used to record the outcome of calls when the CircuitBreaker is closed. slidingWindowSize configures the size of the sliding window.

      The slidingWindowSize must be greater than 0.

      Default slidingWindowSize is 100.

      Parameters:
      slidingWindowSize - the size of the sliding window when the CircuitBreaker is closed.
      Returns:
      the CircuitBreakerConfig.Builder
      Throws:
      IllegalArgumentException - if slidingWindowSize < 1
      See Also:
    • minimumNumberOfCalls

      public CircuitBreakerConfig.Builder minimumNumberOfCalls(int minimumNumberOfCalls)
      Configures the minimum number of calls which are required (per sliding window period) before the CircuitBreaker can calculate the error rate. For example, if minimumNumberOfCalls is 10, then at least 10 calls must be recorded, before the failure rate can be calculated. If only 9 calls have been recorded, the CircuitBreaker will not transition to open, even if all 9 calls have failed.

      Default minimumNumberOfCalls is 100

      Parameters:
      minimumNumberOfCalls - the minimum number of calls that must be recorded before the failure rate can be calculated.
      Returns:
      the CircuitBreakerConfig.Builder
      Throws:
      IllegalArgumentException - if minimumNumberOfCalls < 1
      See Also:
    • slidingWindowType

      public CircuitBreakerConfig.Builder slidingWindowType(CircuitBreakerConfig.SlidingWindowType slidingWindowType)
      Configures the type of the sliding window which is used to record the outcome of calls when the CircuitBreaker is closed. Sliding window can either be count-based or time-based.

      Default slidingWindowType is COUNT_BASED.

      Parameters:
      slidingWindowType - the type of the sliding window. Either COUNT_BASED or TIME_BASED.
      Returns:
      the CircuitBreakerConfig.Builder
      See Also:
    • recordException

      public CircuitBreakerConfig.Builder recordException(Predicate<Throwable> predicate)
      Configures a Predicate which evaluates if an exception should be recorded as a failure and thus increase the failure rate. The Predicate must return true if the exception should count as a failure. The Predicate must return false, if the exception should count as a success, unless the exception is explicitly ignored by ignoreExceptions(Class[]) or ignoreException(Predicate).
      Parameters:
      predicate - the Predicate which evaluates if an exception should count as a failure
      Returns:
      the CircuitBreakerConfig.Builder
    • currentTimestampFunction

      public CircuitBreakerConfig.Builder currentTimestampFunction(Function<Clock,Long> currentTimestampFunction, TimeUnit timeUnit)
      Configures a function that returns current timestamp for CircuitBreaker. Default implementation uses System.nanoTime() to compute current timestamp. Configure currentTimestampFunction to provide different implementation to compute current timestamp.

      Parameters:
      currentTimestampFunction - function that computes current timestamp.
      timeUnit - TimeUnit of timestamp returned by the function.
      Returns:
      the CircuitBreakerConfig.Builder
    • recordResult

      public CircuitBreakerConfig.Builder recordResult(Predicate<Object> predicate)
      Configures a Predicate which evaluates if the result of the protected function call should be recorded as a failure and thus increase the failure rate. The Predicate must return true if the result should count as a failure. The Predicate must return false, if the result should count as a success.
      Parameters:
      predicate - the Predicate which evaluates if a result should count as a failure
      Returns:
      the CircuitBreakerConfig.Builder
    • ignoreException

      public CircuitBreakerConfig.Builder ignoreException(Predicate<Throwable> predicate)
      Configures a Predicate which evaluates if an exception should be ignored and neither count as a failure nor success. The Predicate must return true if the exception should be ignored. The Predicate must return false, if the exception should count as a failure.
      Parameters:
      predicate - the Predicate which evaluates if an exception should count as a failure
      Returns:
      the CircuitBreakerConfig.Builder
    • recordExceptions

      @SafeVarargs public final CircuitBreakerConfig.Builder recordExceptions(@Nullable Class<? extends Throwable>... errorClasses)
      Configures a list of error classes that are recorded as a failure and thus increase the failure rate. Any exception matching or inheriting from one of the list should count as a failure, unless ignored via ignoreExceptions(Class[]) or ignoreException(Predicate).
      Parameters:
      errorClasses - the error classes that are recorded
      Returns:
      the CircuitBreakerConfig.Builder
      See Also:
    • ignoreExceptions

      @SafeVarargs public final CircuitBreakerConfig.Builder ignoreExceptions(@Nullable Class<? extends Throwable>... errorClasses)
      Configures a list of error classes that are ignored and thus neither count as a failure nor success. Any exception matching or inheriting from one of the list will not count as a failure nor success, even if marked via recordExceptions(Class[]) or recordException(Predicate).
      Parameters:
      errorClasses - the error classes that are ignored
      Returns:
      the CircuitBreakerConfig.Builder
      See Also:
    • enableAutomaticTransitionFromOpenToHalfOpen

      public CircuitBreakerConfig.Builder enableAutomaticTransitionFromOpenToHalfOpen()
      Enables automatic transition from OPEN to HALF_OPEN state once the waitDurationInOpenState has passed.
      Returns:
      the CircuitBreakerConfig.Builder
    • automaticTransitionFromOpenToHalfOpenEnabled

      public CircuitBreakerConfig.Builder automaticTransitionFromOpenToHalfOpenEnabled(boolean enableAutomaticTransitionFromOpenToHalfOpen)
      Enables automatic transition from OPEN to HALF_OPEN state once the waitDurationInOpenState has passed.
      Parameters:
      enableAutomaticTransitionFromOpenToHalfOpen - the flag to enable the automatic transitioning.
      Returns:
      the CircuitBreakerConfig.Builder
    • build

      public CircuitBreakerConfig build()
      Builds a CircuitBreakerConfig
      Returns:
      the CircuitBreakerConfig
      Throws:
      IllegalStateException - when the parameter is invalid