Skip to content

Commit bd481ea

Browse files
Merge pull request #10 from mtkp/middleware
support using clj-http-hystrix as middleware
2 parents 295ee89 + 096731b commit bd481ea

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
:license {:name "Eclipse Public License"
66
:url "http://www.eclipse.org/legal/epl-v10.html"}
77

8-
:dependencies [[clj-http "2.0.1"]
8+
:dependencies [[clj-http "2.3.0"]
99
[com.netflix.hystrix/hystrix-clj "1.4.23"]
1010
[org.clojure/clojure "1.8.0"]
1111
[org.clojure/tools.logging "0.3.1"]

src/clj_http_hystrix/core.clj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,22 @@
161161
"Deactivate clj-http-hystrix."
162162
[]
163163
(hooke/remove-hook #'http/request ::wrap-hystrix))
164+
165+
(defn wrap-hystrix
166+
"Middleware for adding hystrix to a clj-http client request.
167+
168+
Alternative to `add-hook`. Do not use both `wrap-hystrix` and `add-hook`.
169+
170+
;; add wrap-hystrix to the middleware chain
171+
(clj-http.client/with-additional-middleware [wrap-hystrix]
172+
(clj-http.client/get ...))
173+
174+
;; or if you want to provide your own defaults
175+
(clj-http.client/with-additional-middleware [(partial wrap-hystrix {:hystrix/timeout-ms 6000})]
176+
(clj-http.client/get ...))
177+
"
178+
([client] (wrap-hystrix {} client))
179+
([defaults client]
180+
(let [wrapper (hystrix-wrapper defaults)]
181+
(fn [req]
182+
(wrapper client req)))))

test/clj_http_hystrix/core_test.clj

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,25 @@
210210
(make-hystrix-call {:hystrix/fallback-fn (constantly "baz")}) => "baz")
211211
(remove-hook)
212212
(add-hook))
213+
214+
(fact "wrap-hystrix enables clj-http-hystrix to be incorporated as a middleware"
215+
(remove-hook)
216+
;verify hystrix is enabled by exceeding the default timeout (1000 ms)
217+
(http/with-additional-middleware [wrap-hystrix]
218+
(rest-driven
219+
[{:method :GET
220+
:url "/"}
221+
{:status 200
222+
:after 1500}]
223+
(make-hystrix-call {})
224+
=> (throws clojure.lang.ExceptionInfo "clj-http: status 503")))
225+
226+
;verify custom defaults are supported
227+
(http/with-additional-middleware
228+
[(partial wrap-hystrix {:hystrix/fallback-fn (constantly {:status 404})})]
229+
(rest-driven
230+
[{:method :GET
231+
:url "/"}
232+
{:status 500}]
233+
(make-hystrix-call {})
234+
=> (throws clojure.lang.ExceptionInfo "clj-http: status 404"))))

0 commit comments

Comments
 (0)