Class CircuitBreaker

java.lang.Object
com.cockroachlabs.crdb.circuit.CircuitBreaker

public final class CircuitBreaker extends Object
A thread-safe circuit breaker.

In State.CLOSED it counts consecutive failures; once the count reaches the threshold it trips State.OPEN and rejects calls with CrdbException(CircuitOpen) until the reset timeout elapses, after which a single State.HALF_OPEN trial probes recovery.

  • Constructor Details

    • CircuitBreaker

      public CircuitBreaker(int failureThreshold, long resetTimeoutMillis)
    • CircuitBreaker

      public CircuitBreaker(int failureThreshold, long resetTimeoutMillis, LongSupplier clock)
      Parameters:
      clock - supplies the current time in millis (inject for tests)
  • Method Details

    • state

      public State state()
      Current breaker state (snapshot).
    • execute

      public <T> T execute(Callable<T> work)
      Execute work through the breaker.
      Throws:
      CrdbException - with a CircuitOpen anomaly when the breaker is open (call is not attempted)