Skip to content

Instantly share code, notes, and snippets.

@krukow
Created November 23, 2009 06:17
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/240925 to your computer and use it in GitHub Desktop.
Save krukow/240925 to your computer and use it in GitHub Desktop.
(ns org.higher-order.integration.circuit-breaker.atomic
(:use org.higher-order.integration.circuit-breaker.states)
(:gen-class))
(def default-policy (TransitionPolicy 5 5000))
(def initial-state (ClosedState default-policy 0))
(def state (atom initial-state))
(def transition-by!)
(defn wrap [f]
(fn [& args]
(let [s (transition-by! on-before-call)]
(if (proceed s)
(try
(let [res (apply f args)]
(do (transition-by! on-success)
res))
(catch Exception e
(do
(transition-by! on-error)
(throw e))))
(throw (java.lang.RuntimeException. "OpenCircuit"))))))
(defn transition-by! [f]
(loop [s @state
t (f s)]
(cond
(identical? s t) s
(compare-and-set! state s t) t
:else (let [s1 @state
t1 (f s1)]
(recur s1 t1)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment