From c7adae1d7e56066201ef7ef9d1631b2de5236b5c Mon Sep 17 00:00:00 2001 From: Paul Bergeron Date: Tue, 2 Apr 2013 11:00:17 -0700 Subject: [PATCH 1/2] Add `with-context` macro for conviently wrapping collapsers in thier own context Closes #132 --- .../main/clojure/com/netflix/hystrix/core.clj | 14 +++++++++-- .../clojure/com/netflix/hystrix/core_test.clj | 23 ++++++++----------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/hystrix-contrib/hystrix-clj/src/main/clojure/com/netflix/hystrix/core.clj b/hystrix-contrib/hystrix-clj/src/main/clojure/com/netflix/hystrix/core.clj index a43c1aa29..6cde1905c 100644 --- a/hystrix-contrib/hystrix-clj/src/main/clojure/com/netflix/hystrix/core.clj +++ b/hystrix-contrib/hystrix-clj/src/main/clojure/com/netflix/hystrix/core.clj @@ -152,7 +152,9 @@ HystrixCollapser HystrixCollapser$Scope HystrixCollapser$Setter - HystrixCollapser$CollapsedRequest])) + HystrixCollapser$CollapsedRequest] + [com.netflix.hystrix.strategy.concurrency + HystrixRequestContext])) (defmacro ^:private key-fn "Make a function that creates keys of the given class given one of: @@ -225,6 +227,15 @@ ;################################################################################ +(defmacro with-context + "Executes body within a new Hystrix Context" + [& body] + `(let [context# (HystrixRequestContext/initializeContext)] + (try + ~@body + (finally + (.shutdown context#))))) + (defn command "Helper function that takes a definition map for a HystrixCommand and returns a normalized version ready for use with execute and queue. @@ -599,4 +610,3 @@ (if (instance? Exception response) (.setException request ^Exception response) (.setResponse request response)))))))) - diff --git a/hystrix-contrib/hystrix-clj/src/test/clojure/com/netflix/hystrix/core_test.clj b/hystrix-contrib/hystrix-clj/src/test/clojure/com/netflix/hystrix/core_test.clj index 6776f3dc6..522c78237 100644 --- a/hystrix-contrib/hystrix-clj/src/test/clojure/com/netflix/hystrix/core_test.clj +++ b/hystrix-contrib/hystrix-clj/src/test/clojure/com/netflix/hystrix/core_test.clj @@ -104,19 +104,15 @@ (is (= (- 99 42) (.execute c))))) (testing "makes a HystrixCommand that implements getCacheKey" - - (let [context (com.netflix.hystrix.strategy.concurrency.HystrixRequestContext/initializeContext)] - (try - (let [call-count (atom 0) ; make sure run-fn is only called once - test-def (normalize (assoc base-def - :run-fn (fn [arg] (swap! call-count inc) (str arg "!")) - :cache-key-fn (fn [arg] arg))) - result1 (.execute (instantiate test-def "hi")) - result2 (.execute (instantiate test-def "hi"))] - (is (= "hi!" result1 result2)) - (is (= 1 @call-count))) - (finally - (.shutdown context)))))) + (with-context + (let [call-count (atom 0) ; make sure run-fn is only called once + test-def (normalize (assoc base-def + :run-fn (fn [arg] (swap! call-count inc) (str arg "!")) + :cache-key-fn (fn [arg] arg))) + result1 (.execute (instantiate test-def "hi")) + result2 (.execute (instantiate test-def "hi"))] + (is (= "hi!" result1 result2)) + (is (= 1 @call-count)))))) (testing "throws if :hystrix metadata isn't found in a var" (is (thrown? IllegalArgumentException (instantiate #'map)))) @@ -292,4 +288,3 @@ (is (= "V" single-call)) (is (= n (count expected-results) (count results))) (is (= expected-results results))))) - From 02bf38795b1255f4924b8836b16f819a16e948db Mon Sep 17 00:00:00 2001 From: Paul Bergeron Date: Wed, 3 Apr 2013 10:05:59 -0700 Subject: [PATCH 2/2] Rename macro to `with-request-context` --- .../hystrix-clj/src/main/clojure/com/netflix/hystrix/core.clj | 2 +- .../src/test/clojure/com/netflix/hystrix/core_test.clj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/hystrix-contrib/hystrix-clj/src/main/clojure/com/netflix/hystrix/core.clj b/hystrix-contrib/hystrix-clj/src/main/clojure/com/netflix/hystrix/core.clj index 6cde1905c..ef6eab9b9 100644 --- a/hystrix-contrib/hystrix-clj/src/main/clojure/com/netflix/hystrix/core.clj +++ b/hystrix-contrib/hystrix-clj/src/main/clojure/com/netflix/hystrix/core.clj @@ -227,7 +227,7 @@ ;################################################################################ -(defmacro with-context +(defmacro with-request-context "Executes body within a new Hystrix Context" [& body] `(let [context# (HystrixRequestContext/initializeContext)] diff --git a/hystrix-contrib/hystrix-clj/src/test/clojure/com/netflix/hystrix/core_test.clj b/hystrix-contrib/hystrix-clj/src/test/clojure/com/netflix/hystrix/core_test.clj index 522c78237..d3d8e0b5f 100644 --- a/hystrix-contrib/hystrix-clj/src/test/clojure/com/netflix/hystrix/core_test.clj +++ b/hystrix-contrib/hystrix-clj/src/test/clojure/com/netflix/hystrix/core_test.clj @@ -104,7 +104,7 @@ (is (= (- 99 42) (.execute c))))) (testing "makes a HystrixCommand that implements getCacheKey" - (with-context + (with-request-context (let [call-count (atom 0) ; make sure run-fn is only called once test-def (normalize (assoc base-def :run-fn (fn [arg] (swap! call-count inc) (str arg "!"))