Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
(extend
::ClosedState CircuitBreakerTransitions
(merge abs-transitions
{:proceed (constantly true)
:on-success (fn [s] (if (zero? (:fail-count s)) s (to-closed-state-from s)))
:on-error (fn [s]
(let [p (:policy s)
f (:fail-count s)]
(if (= f (:fail-count p))
(OpenState p (System/currentTimeMillis))
(assoc s :fail-count (inc f)))))}))
(extend
::OpenState CircuitBreakerTransitions
(merge abs-transitions
{:on-success (fn [s] (InitialHalfOpenState (:policy s)))
:on-before-call (fn [s]
(let [p (:policy s)
delta (- (System/currentTimeMillis) (:time-stamp s))]
(if (> delta (:timeout p))
(InitialHalfOpenState p)
s)))}))
(extend
::InitialHalfOpenState CircuitBreakerTransitions
(merge abs-transitions
{:proceed (constantly true)
:on-before-call (fn [s] (PendingHalfOpenState (:policy s)))
:on-success to-closed-state-from
:on-error (fn [s] (OpenState (:policy s) (System/currentTimeMillis)))}))
(extend
::PendingHalfOpenState
CircuitBreakerTransitions
(merge abs-transitions
{:on-success to-closed-state-from
:on-error (fn [s] (OpenState (:policy s) (System/currentTimeMillis)))}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment