CircuitBreaker

object CircuitBreaker extends CircuitBreakerDocs
Companion
class
trait CircuitBreakerDocs
class Object
trait Matchable
class Any

Type members

Classlikes

final class Builders[F[_]](val F: Sync[F]) extends AnyVal with CircuitBreakerDocs

Builders specified for CircuitBreaker, using the Partially-Applied Type technique.

Builders specified for CircuitBreaker, using the Partially-Applied Type technique.

final case class Closed(failures: Int) extends State

The initial State of the CircuitBreaker. While in this state the circuit breaker allows tasks to be executed.

The initial State of the CircuitBreaker. While in this state the circuit breaker allows tasks to be executed.

Contract:

  • Exceptions increment the failures counter
  • Successes reset the failure count to zero
  • When the failures counter reaches the maxFailures count, the breaker is tripped into the Open state
Value Params
failures

is the current failures count

final class HalfOpen extends State

State of the CircuitBreaker in which the circuit breaker has already allowed a task to go through, as a reset attempt, in order to test the connection.

State of the CircuitBreaker in which the circuit breaker has already allowed a task to go through, as a reset attempt, in order to test the connection.

Contract:

  • The first task when Open has expired is allowed through without failing fast, just before the circuit breaker is evolved into the HalfOpen state
  • All tasks attempted in HalfOpen fail-fast with an exception just as in Open state
  • If that task attempt succeeds, the breaker is reset back to the Closed state, with the resetTimeout and the failures count also reset to initial values
  • If the first call fails, the breaker is tripped again into the Open state (the resetTimeout is multiplied by the exponential backoff factor)
Value Params
awaitClose

is a Deferred (pure promise) that will get completed when the CircuitBreaker will switch to the Closed state again; this reference is None in case the F[_] used does not implement cats.effect.Async, because only with Async data types we can wait for completion.

resetTimeout

is the current resetTimeout that was applied to the previous Open state, to be multiplied by the exponential backoff factor for the next transition to Open, in case the reset attempt fails

Companion
object
object HalfOpen
Companion
class
final class Open extends State

State of the CircuitBreaker in which the circuit breaker rejects all tasks with an ExecutionRejectedException.

State of the CircuitBreaker in which the circuit breaker rejects all tasks with an ExecutionRejectedException.

Contract:

  • all tasks fail fast with ExecutionRejectedException
  • after the configured resetTimeout, the circuit breaker enters a HalfOpen state, allowing one task to go through for testing the connection
Value Params
awaitClose

is a Deferred (pure promise) that will get completed when the CircuitBreaker will switch to the Closed state again; this reference is None in case the F[_] used does not implement cats.effect.Async, because only with Async data types we can wait for completion.

resetTimeout

is the current resetTimeout that is applied to this Open state, to be multiplied by the exponential backoff factor for the next transition from HalfOpen to Open, in case the reset attempt fails

startedAt

is the timestamp in milliseconds since the epoch when the transition to Open happened

Companion
object
object Open
Companion
class
sealed abstract class State

An enumeration that models the internal state of CircuitBreaker, kept in an Atomic for synchronization.

An enumeration that models the internal state of CircuitBreaker, kept in an Atomic for synchronization.

The initial state when initializing a CircuitBreaker is Closed. The available states:

  • Closed in case tasks are allowed to go through
  • Open in case the circuit breaker is active and rejects incoming tasks
  • HalfOpen in case a reset attempt was triggered and it is waiting for the result in order to evolve in Closed, or back to Open

Types

type Timestamp = Long

Type-alias to document timestamps specified in milliseconds, as returned by Scheduler.clockRealTime.

Type-alias to document timestamps specified in milliseconds, as returned by Scheduler.clockRealTime.

Value members

Concrete methods

def apply[F[_]](F: Sync[F]): Builders[F]

Builders specified for CircuitBreaker, using the Partially-Applied Type technique.

Builders specified for CircuitBreaker, using the Partially-Applied Type technique.

Example:

 import scala.concurrent.duration._
 import cats.effect.{IO, Clock}
 implicit val clock: Clock[IO] = Clock.create[IO]

 val cb = CircuitBreaker[IO].of(
   maxFailures = 10,
   resetTimeout = 3.second,
   exponentialBackoffFactor = 2
 )
def of[F[_]](maxFailures: Int, resetTimeout: FiniteDuration, exponentialBackoffFactor: Double, maxResetTimeout: Duration, padding: PaddingStrategy)(F: Sync[F], clock: Clock[F]): F[CircuitBreaker[F]]

Safe builder.

Safe builder.

Value Params
exponentialBackoffFactor

$exponentialBackoffFactorParam

maxFailures

$maxFailuresParam

maxResetTimeout

$maxResetTimeoutParam

padding

$paddingParam

resetTimeout

$resetTimeoutParam

See also

CircuitBreaker[F].of, the version that uses the "partially-applied type technique"

@UnsafeBecauseImpure
def unsafe[F[_]](maxFailures: Int, resetTimeout: FiniteDuration, exponentialBackoffFactor: Double, maxResetTimeout: Duration, padding: PaddingStrategy)(F: Sync[F], clock: Clock[F]): CircuitBreaker[F]

Unsafe builder (that violates referential transparency).

Unsafe builder (that violates referential transparency).

$unsafeWarning

Value Params
exponentialBackoffFactor

$exponentialBackoffFactorParam

maxFailures

$maxFailuresParam

maxResetTimeout

$maxResetTimeoutParam

padding

$paddingParam

resetTimeout

$resetTimeoutParam

See also

CircuitBreaker[F].unsafe, the version that uses the "partially-applied type technique"