Class CircuitBreakerConfig

java.lang.Object
com.shri.restinpeace.CircuitBreakerConfig

public final class CircuitBreakerConfig extends Object
Per-client circuit breaker for RIP.getClient(Class, RipClientConfig) - stops even attempting calls to a downstream that's shown itself to be failing, for a cooldown period, once its failure rate crosses a threshold. See docs/design/circuit-breaker-bulkhead.md for the full design, personas, and the reasoning behind every default below.

The sliding window is count-based (the last getSlidingWindowSize() calls) by default, not time-based (the last N seconds) - a count-based window is deterministic to test and doesn't misbehave for a low-traffic client, where "the last 30 seconds" might contain zero or one call. The trip condition is always a failure rate (a percentage of the window), never a raw failure count, gated by getMinimumNumberOfCalls() so a rate is never evaluated off a statistically meaningless sample.

 UserApi api = RIP.getClient(UserApi.class, RipClientConfig.builder()
         .circuitBreaker(CircuitBreakerConfig.builder()
                 .slidingWindowSize(20)
                 .minimumNumberOfCalls(10)
                 .failureRateThreshold(50)
                 .waitDurationInOpenState(Duration.ofSeconds(30))
                 .permittedCallsInHalfOpenState(3)
                 .build())
         .build());
 

A CircuitBreakerConfig.SlidingWindowType.TIME_BASED window (the last N seconds, regardless of call volume) is also supported via CircuitBreakerConfig.Builder.slidingWindowSize(Duration) - unlike resilience4j's own bare int-always-means-seconds shape, this takes a real Duration, consistent with getWaitDurationInOpenStateMillis() already being duration-typed in this same config.

  • Method Details

    • builder

      public static CircuitBreakerConfig.Builder builder()
      Starts building a new config, defaulting to a count-based, 20-call window; a 50% failure-rate threshold, evaluated once at least 10 calls have been recorded; a 30-second open-state cooldown; 3 permitted half-open trial calls; and treating any 5xx response (or a transport failure) as a breaker failure.
      Returns:
      a new builder
    • getSlidingWindowType

      public CircuitBreakerConfig.SlidingWindowType getSlidingWindowType()
      Returns the sliding window's type.
      Returns:
      whether the window is count- or time-based
    • getSlidingWindowSize

      public int getSlidingWindowSize()
      Returns the count-based window's size.
      Returns:
      the number of most-recent calls the window tracks; meaningless if getSlidingWindowType() is CircuitBreakerConfig.SlidingWindowType.TIME_BASED
    • getSlidingWindowDurationMillis

      public long getSlidingWindowDurationMillis()
      Returns the time-based window's duration.
      Returns:
      how far back the window looks, in milliseconds; meaningless if getSlidingWindowType() is CircuitBreakerConfig.SlidingWindowType.COUNT_BASED
    • getMinimumNumberOfCalls

      public int getMinimumNumberOfCalls()
      Returns the minimum sample size before a failure rate is evaluated.
      Returns:
      the minimum number of calls that must be in the window before getFailureRateThreshold() is ever checked
    • getFailureRateThreshold

      public int getFailureRateThreshold()
      Returns the failure-rate trip threshold.
      Returns:
      the failure rate, as a percentage from 1 to 100, that trips the breaker from closed to open
    • getWaitDurationInOpenStateMillis

      public long getWaitDurationInOpenStateMillis()
      Returns how long the breaker stays open before trialing recovery.
      Returns:
      the open-state cooldown, in milliseconds
    • getPermittedCallsInHalfOpenState

      public int getPermittedCallsInHalfOpenState()
      Returns how many trial calls are permitted while half-open.
      Returns:
      the number of calls permitted through while half-open, before deciding whether to close or re-open
    • getRecordFailureForStatus

      public IntPredicate getRecordFailureForStatus()
      Returns the predicate deciding which HTTP status codes count as a breaker failure.
      Returns:
      the status-code failure predicate; a transport-level failure (no response at all) always counts as a failure regardless of this predicate, since there's no status code to test