Packages

class CircuitBreaker extends AbstractCircuitBreaker

Provides circuit breaker functionality for stability when working with "dangerous" operations, e.g. calls to remote systems.

Transitions through three states: - In *Closed* state, calls pass through until the maxFailures count is reached. This causes the circuit breaker to open. Both exceptions and calls exceeding callTimeout are considered failures. - In *Open* state, calls fail-fast with an exception. After resetTimeout, circuit breaker transitions to half-open state. - In *Half-Open* state, the first call will be allowed through, if it succeeds the circuit breaker will reset to closed state. If it fails, the circuit breaker will re-open to open state. All calls beyond the first that execute while the first is running will fail-fast with an exception.

Source
CircuitBreaker.scala
Linear Supertypes
AbstractCircuitBreaker, AnyRef, Any
Type Hierarchy
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. CircuitBreaker
  2. AbstractCircuitBreaker
  3. AnyRef
  4. Any
Implicitly
  1. by any2stringadd
  2. by StringFormat
  3. by Ensuring
  4. by ArrowAssoc
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Instance Constructors

  1. new CircuitBreaker(scheduler: Scheduler, maxFailures: Int, callTimeout: FiniteDuration, resetTimeout: FiniteDuration, maxResetTimeout: FiniteDuration, exponentialBackoffFactor: Double)(implicit executor: ExecutionContext)
  2. new CircuitBreaker(scheduler: Scheduler, maxFailures: Int, callTimeout: FiniteDuration, resetTimeout: FiniteDuration)(implicit executor: ExecutionContext)
  3. new CircuitBreaker(executor: ExecutionContext, scheduler: Scheduler, maxFailures: Int, callTimeout: Duration, resetTimeout: Duration)
  4. new CircuitBreaker(scheduler: Scheduler, maxFailures: Int, callTimeout: FiniteDuration, resetTimeout: FiniteDuration, maxResetTimeout: FiniteDuration, exponentialBackoffFactor: Double, randomFactor: Double)(implicit executor: ExecutionContext)
  5. new CircuitBreaker(scheduler: Scheduler, maxFailures: Int, callTimeout: FiniteDuration, resetTimeout: FiniteDuration, maxResetTimeout: FiniteDuration, exponentialBackoffFactor: Double, randomFactor: Double, allowExceptions: Set[String], telemetry: CircuitBreakerTelemetry)(implicit executor: ExecutionContext)

    scheduler

    Reference to Akka scheduler

    maxFailures

    Maximum number of failures before opening the circuit

    callTimeout

    scala.concurrent.duration.FiniteDuration of time after which to consider a call a failure

    resetTimeout

    scala.concurrent.duration.FiniteDuration of time after which to attempt to close the circuit

    randomFactor

    after calculation of the exponential back-off an additional random delay based on this factor is added, e.g. 0.2 adds up to 20% delay. randomFactor should be in range 0.0 (inclusive) and 1.0 (inclusive). In order to skip this additional delay pass in 0.

    executor

    scala.concurrent.ExecutionContext used for execution of state transition listeners

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. def +(other: String): String
    Implicit
    This member is added by an implicit conversion from CircuitBreaker toany2stringadd[CircuitBreaker] performed by method any2stringadd in scala.Predef.
    Definition Classes
    any2stringadd
  4. def ->[B](y: B): (CircuitBreaker, B)
    Implicit
    This member is added by an implicit conversion from CircuitBreaker toArrowAssoc[CircuitBreaker] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
    Annotations
    @inline()
  5. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  6. def addOnCallBreakerOpenListener(callback: Runnable): CircuitBreaker

    JavaAPI for onCallBreakerOpen.

    JavaAPI for onCallBreakerOpen.

    callback

    Handler to be invoked on call failed due to open breaker.

    returns

    CircuitBreaker for fluent usage

  7. def addOnCallFailureListener(callback: Consumer[Long]): CircuitBreaker

    JavaAPI for onCallFailure.

    JavaAPI for onCallFailure.

    callback

    Handler to be invoked on failed call, where passed value is elapsed time in nanoseconds.

    returns

    CircuitBreaker for fluent usage

  8. def addOnCallSuccessListener(callback: Consumer[Long]): CircuitBreaker

    JavaAPI for onCallSuccess

    JavaAPI for onCallSuccess

    callback

    Handler to be invoked on successful call, where passed value is elapsed time in nanoseconds.

    returns

    CircuitBreaker for fluent usage

  9. def addOnCallTimeoutListener(callback: Consumer[Long]): CircuitBreaker

    JavaAPI for onCallTimeout.

    JavaAPI for onCallTimeout.

    callback

    Handler to be invoked on call finished with timeout, where passed value is elapsed time in nanoseconds.

    returns

    CircuitBreaker for fluent usage

  10. def addOnCloseListener(callback: Runnable): CircuitBreaker

    JavaAPI for onClose

    JavaAPI for onClose

    callback

    Handler to be invoked on state change

    returns

    CircuitBreaker for fluent usage

  11. def addOnHalfOpenListener(callback: Runnable): CircuitBreaker

    JavaAPI for onHalfOpen

    JavaAPI for onHalfOpen

    callback

    Handler to be invoked on state change

    returns

    CircuitBreaker for fluent usage

  12. def addOnOpenListener(callback: Runnable): CircuitBreaker

    Java API for onOpen

    Java API for onOpen

    callback

    Handler to be invoked on state change

    returns

    CircuitBreaker for fluent usage

  13. val allowExceptions: Set[String]
  14. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  15. def callWithCircuitBreaker[T](body: Callable[Future[T]], defineFailureFn: BiFunction[Optional[T], Optional[Throwable], Boolean]): Future[T]

    Java API for #withCircuitBreaker.

    Java API for #withCircuitBreaker.

    body

    Call needing protected

    defineFailureFn

    function that define what should be consider failure and thus increase failure count

    returns

    scala.concurrent.Future containing the call result or a scala.concurrent.TimeoutException if the call timed out

  16. def callWithCircuitBreaker[T](body: Callable[Future[T]]): Future[T]

    Java API for #withCircuitBreaker.

    Java API for #withCircuitBreaker.

    body

    Call needing protected

    returns

    scala.concurrent.Future containing the call result or a scala.concurrent.TimeoutException if the call timed out

  17. def callWithCircuitBreakerCS[T](body: Callable[CompletionStage[T]], defineFailureFn: BiFunction[Optional[T], Optional[Throwable], Boolean]): CompletionStage[T]

    Java API (8) for #withCircuitBreaker.

    Java API (8) for #withCircuitBreaker.

    body

    Call needing protected

    defineFailureFn

    function that define what should be consider failure and thus increase failure count

    returns

    java.util.concurrent.CompletionStage containing the call result or a scala.concurrent.TimeoutException if the call timed out

  18. def callWithCircuitBreakerCS[T](body: Callable[CompletionStage[T]]): CompletionStage[T]

    Java API (8) for #withCircuitBreaker.

    Java API (8) for #withCircuitBreaker.

    body

    Call needing protected

    returns

    java.util.concurrent.CompletionStage containing the call result or a scala.concurrent.TimeoutException if the call timed out

  19. def callWithSyncCircuitBreaker[T](body: Callable[T], defineFailureFn: BiFunction[Optional[T], Optional[Throwable], Boolean]): T

    Java API for #withSyncCircuitBreaker.

    Java API for #withSyncCircuitBreaker. Throws java.util.concurrent.TimeoutException if the call timed out.

    body

    Call needing protected

    defineFailureFn

    function that define what should be consider failure and thus increase failure count

    returns

    The result of the call

  20. def callWithSyncCircuitBreaker[T](body: Callable[T]): T

    Java API for #withSyncCircuitBreaker.

    Java API for #withSyncCircuitBreaker. Throws java.util.concurrent.TimeoutException if the call timed out.

    body

    Call needing protected

    returns

    The result of the call

  21. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @HotSpotIntrinsicCandidate() @native()
  22. def ensuring(cond: (CircuitBreaker) => Boolean, msg: => Any): CircuitBreaker
    Implicit
    This member is added by an implicit conversion from CircuitBreaker toEnsuring[CircuitBreaker] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  23. def ensuring(cond: (CircuitBreaker) => Boolean): CircuitBreaker
    Implicit
    This member is added by an implicit conversion from CircuitBreaker toEnsuring[CircuitBreaker] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  24. def ensuring(cond: Boolean, msg: => Any): CircuitBreaker
    Implicit
    This member is added by an implicit conversion from CircuitBreaker toEnsuring[CircuitBreaker] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  25. def ensuring(cond: Boolean): CircuitBreaker
    Implicit
    This member is added by an implicit conversion from CircuitBreaker toEnsuring[CircuitBreaker] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  26. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  27. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  28. def fail(): Unit

    Mark a failed call through CircuitBreaker.

    Mark a failed call through CircuitBreaker. Sometimes the callee of CircuitBreaker sends back a message to the caller Actor. In such a case, it is convenient to mark a failed call instead of using Future via withCircuitBreaker

  29. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @HotSpotIntrinsicCandidate() @native()
  30. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @HotSpotIntrinsicCandidate() @native()
  31. def isClosed: Boolean

    Return true if the internal state is Closed.

    Return true if the internal state is Closed. WARNING: It is a "power API" call which you should use with care. Ordinal use cases of CircuitBreaker expects a remote call to return Future, as in withCircuitBreaker. So, if you check the state by yourself, and make a remote call outside CircuitBreaker, you should manage the state yourself.

  32. def isHalfOpen: Boolean

    Return true if the internal state is HalfOpen.

    Return true if the internal state is HalfOpen. WARNING: It is a "power API" call which you should use with care. Ordinal use cases of CircuitBreaker expects a remote call to return Future, as in withCircuitBreaker. So, if you check the state by yourself, and make a remote call outside CircuitBreaker, you should manage the state yourself.

  33. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  34. def isOpen: Boolean

    Return true if the internal state is Open.

    Return true if the internal state is Open. WARNING: It is a "power API" call which you should use with care. Ordinal use cases of CircuitBreaker expects a remote call to return Future, as in withCircuitBreaker. So, if you check the state by yourself, and make a remote call outside CircuitBreaker, you should manage the state yourself.

  35. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  36. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @HotSpotIntrinsicCandidate() @native()
  37. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @HotSpotIntrinsicCandidate() @native()
  38. def onCallBreakerOpen(callback: => Unit): CircuitBreaker

    Adds a callback to execute if call was failed due to open breaker.

    Adds a callback to execute if call was failed due to open breaker.

    The callback is run in the scala.concurrent.ExecutionContext supplied in the constructor.

    callback

    Handler to be invoked on call failed due to open breaker.

    returns

    CircuitBreaker for fluent usage

  39. def onCallFailure(callback: (Long) => Unit): CircuitBreaker

    Adds a callback to execute if the call finished with failure.

    Adds a callback to execute if the call finished with failure.

    The callback is run in the scala.concurrent.ExecutionContext supplied in the constructor.

    callback

    Handler to be invoked on failed call, where passed value is elapsed time in nanoseconds.

    returns

    CircuitBreaker for fluent usage

  40. def onCallSuccess(callback: (Long) => Unit): CircuitBreaker

    Adds a callback to execute when call finished with success.

    Adds a callback to execute when call finished with success.

    The callback is run in the scala.concurrent.ExecutionContext supplied in the constructor.

    callback

    Handler to be invoked on successful call, where passed value is elapsed time in nanoseconds.

    returns

    CircuitBreaker for fluent usage

  41. def onCallTimeout(callback: (Long) => Unit): CircuitBreaker

    Adds a callback to execute if a call finished with timeout.

    Adds a callback to execute if a call finished with timeout.

    The callback is run in the scala.concurrent.ExecutionContext supplied in the constructor.

    callback

    Handler to be invoked on call finished with timeout, where passed value is elapsed time in nanoseconds.

    returns

    CircuitBreaker for fluent usage

  42. def onClose(callback: => Unit): CircuitBreaker

    Adds a callback to execute when circuit breaker state closes

    Adds a callback to execute when circuit breaker state closes

    The callback is run in the scala.concurrent.ExecutionContext supplied in the constructor.

    callback

    Handler to be invoked on state change

    returns

    CircuitBreaker for fluent usage

  43. def onHalfOpen(callback: => Unit): CircuitBreaker

    Adds a callback to execute when circuit breaker transitions to half-open The callback is run in the scala.concurrent.ExecutionContext supplied in the constructor.

    Adds a callback to execute when circuit breaker transitions to half-open The callback is run in the scala.concurrent.ExecutionContext supplied in the constructor.

    callback

    Handler to be invoked on state change

    returns

    CircuitBreaker for fluent usage

  44. def onOpen(callback: => Unit): CircuitBreaker

    Adds a callback to execute when circuit breaker opens

    Adds a callback to execute when circuit breaker opens

    The callback is run in the scala.concurrent.ExecutionContext supplied in the constructor.

    callback

    Handler to be invoked on state change

    returns

    CircuitBreaker for fluent usage

  45. val resetTimeout: FiniteDuration
  46. def succeed(): Unit

    Mark a successful call through CircuitBreaker.

    Mark a successful call through CircuitBreaker. Sometimes the callee of CircuitBreaker sends back a message to the caller Actor. In such a case, it is convenient to mark a successful call instead of using Future via withCircuitBreaker

  47. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  48. val telemetry: CircuitBreakerTelemetry
  49. def toString(): String
    Definition Classes
    AnyRef → Any
  50. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  51. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  52. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  53. def withCircuitBreaker[T](body: => Future[T]): Future[T]

    Wraps invocations of asynchronous calls that need to be protected.

    Wraps invocations of asynchronous calls that need to be protected.

    body

    Call needing protected

    returns

    scala.concurrent.Future containing the call result or a scala.concurrent.TimeoutException if the call timed out

  54. def withCircuitBreaker[T](body: => Future[T], defineFailureFn: (Try[T]) => Boolean): Future[T]

    Wraps invocations of asynchronous calls that need to be protected.

    Wraps invocations of asynchronous calls that need to be protected.

    body

    Call needing protected

    defineFailureFn

    function that define what should be consider failure and thus increase failure count

    returns

    scala.concurrent.Future containing the call result or a scala.concurrent.TimeoutException if the call timed out

  55. def withExponentialBackoff(maxResetTimeout: Duration): CircuitBreaker

    The resetTimeout will be increased exponentially for each failed attempt to close the circuit.

    The resetTimeout will be increased exponentially for each failed attempt to close the circuit. The default exponential backoff factor is 2.

    maxResetTimeout

    the upper bound of resetTimeout

  56. def withExponentialBackoff(maxResetTimeout: FiniteDuration): CircuitBreaker

    The resetTimeout will be increased exponentially for each failed attempt to close the circuit.

    The resetTimeout will be increased exponentially for each failed attempt to close the circuit. The default exponential backoff factor is 2.

    maxResetTimeout

    the upper bound of resetTimeout

  57. def withRandomFactor(randomFactor: Double): CircuitBreaker

    Adds jitter to the delay.

    Adds jitter to the delay.

    randomFactor

    after calculation of the back-off an additional random delay based on this factor is added, e.g. 0.2 adds up to 20% delay. In order to skip this additional delay pass in 0.

  58. def withSyncCircuitBreaker[T](body: => T, defineFailureFn: (Try[T]) => Boolean): T

    Wraps invocations of synchronous calls that need to be protected.

    Wraps invocations of synchronous calls that need to be protected.

    Calls are run in caller's thread. Because of the synchronous nature of this call the scala.concurrent.TimeoutException will only be thrown after the body has completed.

    Throws java.util.concurrent.TimeoutException if the call timed out.

    body

    Call needing protected

    defineFailureFn

    function that define what should be consider failure and thus increase failure count

    returns

    The result of the call

  59. def withSyncCircuitBreaker[T](body: => T): T

    Wraps invocations of synchronous calls that need to be protected.

    Wraps invocations of synchronous calls that need to be protected.

    Calls are run in the caller's thread. Because of the synchronous nature of this call, the scala.concurrent.TimeoutException will only be thrown after the body has completed.

    Throws java.util.concurrent.TimeoutException if the call timed out.

    body

    Call needing protected

    returns

    The result of the call

Deprecated Value Members

  1. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable]) @Deprecated
    Deprecated

    (Since version 9)

  2. def formatted(fmtstr: String): String
    Implicit
    This member is added by an implicit conversion from CircuitBreaker toStringFormat[CircuitBreaker] performed by method StringFormat in scala.Predef.
    Definition Classes
    StringFormat
    Annotations
    @deprecated @inline()
    Deprecated

    (Since version 2.12.16) Use formatString.format(value) instead of value.formatted(formatString), or use the f"" string interpolator. In Java 15 and later, formatted resolves to the new method in String which has reversed parameters.

  3. def [B](y: B): (CircuitBreaker, B)
    Implicit
    This member is added by an implicit conversion from CircuitBreaker toArrowAssoc[CircuitBreaker] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
    Annotations
    @deprecated
    Deprecated

    (Since version 2.13.0) Use -> instead. If you still wish to display it as one character, consider using a font with programming ligatures such as Fira Code.

Inherited from AbstractCircuitBreaker

Inherited from AnyRef

Inherited from Any

Inherited by implicit conversion any2stringadd fromCircuitBreaker to any2stringadd[CircuitBreaker]

Inherited by implicit conversion StringFormat fromCircuitBreaker to StringFormat[CircuitBreaker]

Inherited by implicit conversion Ensuring fromCircuitBreaker to Ensuring[CircuitBreaker]

Inherited by implicit conversion ArrowAssoc fromCircuitBreaker to ArrowAssoc[CircuitBreaker]

Ungrouped