Skip to content

Instantly share code, notes, and snippets.

Created May 5, 2010 08:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
(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)
(let [res (apply f args)]
(do (transition-by! on-success state)
(catch Exception e
(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