CircuitBreaker

class CircuitBreaker(val name: String, failureThreshold: Int = DEFAULT_FAILURE_THRESHOLD, windowMs: Long = DEFAULT_WINDOW_MS, cooldownMs: Long = DEFAULT_COOLDOWN_MS, clock: () -> Long = System::currentTimeMillis)(source)

Thread-safe circuit breaker that automatically disables calls after repeated failures.

States:

  • CLOSED (normal): calls pass through. Failures are counted within a sliding window.

  • OPEN (tripped): calls are rejected immediately. After cooldownMs elapses, the breaker transitions to HALF_OPEN.

  • HALF_OPEN (probing): one call is allowed through. If it succeeds the breaker resets to CLOSED; if it fails the breaker re-opens.

Parameters

name

Human-readable label used in log messages

failureThreshold

Number of failures within windowMs that trip the breaker

windowMs

Sliding time window for counting failures (milliseconds)

cooldownMs

How long the breaker stays OPEN before transitioning to HALF_OPEN

Constructors

Link copied to clipboard
constructor(name: String, failureThreshold: Int = DEFAULT_FAILURE_THRESHOLD, windowMs: Long = DEFAULT_WINDOW_MS, cooldownMs: Long = DEFAULT_COOLDOWN_MS, clock: () -> Long = System::currentTimeMillis)

Types

Link copied to clipboard

The three possible breaker states.

Properties

Link copied to clipboard

Functions

Link copied to clipboard

Returns true if a call should be allowed through.

Link copied to clipboard

Number of failures recorded in the current window.

Link copied to clipboard

Records a failed call. In HALF_OPEN, immediately re-opens the breaker. In CLOSED, increments the failure counter and trips the breaker if the threshold is reached.

Link copied to clipboard

Records a successful call. Resets the breaker to CLOSED if it was HALF_OPEN.

Link copied to clipboard
fun reset()

Manually resets the breaker to CLOSED.

Link copied to clipboard

Current breaker state.