Class CircuitBreakerConfig.Builder

  • Enclosing class:
    CircuitBreakerConfig

    public static class CircuitBreakerConfig.Builder
    extends java.lang.Object
    • Method Detail

      • 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:
        java.lang.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:
        java.lang.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​(java.time.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.
        Parameters:
        waitDurationInOpenState - the wait duration which specifies how long the CircuitBreaker should stay open
        Returns:
        the CircuitBreakerConfig.Builder
        Throws:
        java.lang.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.

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

        public CircuitBreakerConfig.Builder slowCallDurationThreshold​(java.time.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:
        java.lang.IllegalArgumentException - if slowCallDurationThreshold.toNanos() < 1
      • maxWaitDurationInHalfOpenState

        public CircuitBreakerConfig.Builder maxWaitDurationInHalfOpenState​(java.time.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:
        java.lang.IllegalArgumentException - if waitDurationInOpenState.toMillis() < 1000
      • 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:
        java.lang.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:
        java.lang.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:
        java.lang.IllegalArgumentException - if slidingWindowSize < 1
        See Also:
        slidingWindow(int, int, SlidingWindowType)
      • 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:
        java.lang.IllegalArgumentException - if minimumNumberOfCalls < 1
        See Also:
        slidingWindow(int, int, SlidingWindowType)
      • 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:
        slidingWindow(int, int, SlidingWindowType)
      • recordException

        public CircuitBreakerConfig.Builder recordException​(java.util.function.Predicate<java.lang.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​(java.util.function.Function<java.time.Clock,​java.lang.Long> currentTimestampFunction,
                                                                     java.util.concurrent.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
      • ignoreException

        public CircuitBreakerConfig.Builder ignoreException​(java.util.function.Predicate<java.lang.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
      • 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