Create a gist now

Instantly share code, notes, and snippets.

(def default-policy (TransitionPolicy. 5 5000))
(def initial-state (ClosedState. default-policy 0))
(defn make-circuit-breaker
"Creates a circuit-breaker instance. If called with no arguments
a circuit-breaker in the initial closed state with the default policy is
created. If called with one argument supporting the CircuitBreakerTransitions
protocol, a circuit-breaker is created using that as state."
([] (atom initial-state))
([#^CircuitBreakerTransitions s] (atom s)))
(defn wrap-with [f state]
(fn [& args]
(let [s (transition-by! on-before-call state)]
(if (proceed s)
(try
(let [res (apply f args)]
(do (transition-by! on-success state)
res))
(catch Exception e
(do
(transition-by! on-error state)
(throw e))))
(throw (RuntimeException. "OpenCircuit"))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment