Created
November 23, 2009 06:17
-
-
Save krukow/240925 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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