Skip to content

Instantly share code, notes, and snippets.

@krukow
Created May 5, 2010 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krukow/390529 to your computer and use it in GitHub Desktop.
Save krukow/390529 to your computer and use it in GitHub Desktop.
(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