Skip to content

Commit 0780287

Browse files
committed
consolidate defaults into a single var
1 parent 6d4cc86 commit 0780287

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Whenever you make an http request, add one or more of the hystrix-clj options to
2424
:hystrix/breaker-request-volume 20
2525
:hystrix/breaker-error-percent 50
2626
:hystrix/breaker-sleep-window-ms 5000
27-
:hystrix/bad-request-pred client-error?}}
27+
:hystrix/bad-request-pred client-error?})
2828
```
2929
Any values not supplied will be set to their default values as above. Requests with no Hystrix-related keys won't use Hystrix.
3030

src/clj_http_hystrix/core.clj

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,17 @@
1313
[com.netflix.hystrix.exception HystrixBadRequestException]
1414
[org.slf4j MDC]))
1515

16-
(def ^:private ^:const hystrix-keys
17-
#{:hystrix/fallback-fn
18-
:hystrix/group-key
19-
:hystrix/command-key
20-
:hystrix/threads
21-
:hystrix/queue-size
22-
:hystrix/timeout-ms
23-
:hystrix/breaker-request-volume
24-
:hystrix/breaker-error-percent
25-
:hystrix/breaker-sleep-window-ms
26-
:hystrix/bad-request-pred})
27-
2816
(defn default-fallback [req resp]
2917
(if (:status resp)
3018
resp
3119
{:status 503}))
3220

21+
(defn client-error?
22+
"Returns true when the response has one of the 4xx family of status
23+
codes"
24+
[req resp]
25+
(http/client-error? resp))
26+
3327
(defn ^:private handle-exception
3428
[f req]
3529
(let [^Exception raw-response (try (f) (catch Exception e e))
@@ -40,6 +34,21 @@
4034
((http/wrap-exceptions (constantly resp)) req))
4135
resp))
4236

37+
(def ^:private ^:const hystrix-defaults
38+
{:hystrix/command-key :default
39+
:hystrix/fallback-fn default-fallback
40+
:hystrix/group-key :default
41+
:hystrix/threads 10
42+
:hystrix/queue-size 5
43+
:hystrix/timeout-ms 1000
44+
:hystrix/breaker-request-volume 20
45+
:hystrix/breaker-error-percent 50
46+
:hystrix/breaker-sleep-window-ms 5000
47+
:hystrix/bad-request-pred client-error?})
48+
49+
(def ^:private hystrix-keys
50+
(keys hystrix-defaults))
51+
4352
(defn ^:private group-key [s]
4453
(HystrixCommandGroupKey$Factory/asKey (name s)))
4554

@@ -49,13 +58,15 @@
4958
(defn ^:private configurator
5059
"Create a configurator that can configure the hystrix according to the
5160
declarative config (or some sensible defaults)"
52-
[config]
53-
(let [timeout (:hystrix/timeout-ms config 1000)
54-
group (:hystrix/group-key config :default)
55-
threads (:hystrix/threads config 10)
56-
request-volume (:hystrix/breaker-request-volume config 20)
57-
error-percent (:hystrix/breaker-error-percent config 50)
58-
sleep-window (:hystrix/breaker-sleep-window-ms config 5000)
61+
^HystrixCommand$Setter [config]
62+
(let [{group :hystrix/group-key
63+
command :hystrix/command-key
64+
timeout :hystrix/timeout-ms
65+
threads :hystrix/threads
66+
queue-size :hystrix/queue-size
67+
sleep-window :hystrix/breaker-sleep-window-ms
68+
error-percent :hystrix/breaker-error-percent
69+
request-volume :hystrix/breaker-request-volume} config
5970
command-configurator (doto (HystrixCommandProperties/Setter)
6071
(.withExecutionIsolationThreadTimeoutInMilliseconds timeout)
6172
(.withCircuitBreakerRequestVolumeThreshold request-volume)
@@ -64,10 +75,10 @@
6475
(.withMetricsRollingPercentileEnabled false))
6576
thread-pool-configurator (doto (HystrixThreadPoolProperties/Setter)
6677
(.withCoreSize threads)
67-
(.withMaxQueueSize (:hystrix/queue-size config 5))
68-
(.withQueueSizeRejectionThreshold (:hystrix/queue-size config 5)))]
78+
(.withMaxQueueSize queue-size)
79+
(.withQueueSizeRejectionThreshold queue-size))]
6980
(doto (HystrixCommand$Setter/withGroupKey (group-key group))
70-
(.andCommandKey (command-key (:hystrix/command-key config :default)))
81+
(.andCommandKey (command-key command))
7182
(.andCommandPropertiesDefaults command-configurator)
7283
(.andThreadPoolPropertiesDefaults thread-pool-configurator))))
7384

@@ -85,27 +96,22 @@
8596
(fn [req resp]
8697
(contains? status-codes (:status resp)))))
8798

88-
(defn client-error?
89-
"Returns true when the response has one of the 4xx family of status
90-
codes"
91-
[req resp]
92-
(http/client-error? resp))
93-
9499
(defn wrap-hystrix
95100
"Wrap a clj-http client request with hystrix features (but only if a
96101
command-key is present in the options map)."
97102
[f req]
98103
(if (not-empty (select-keys req hystrix-keys))
99-
(let [bad-request-pred (:hystrix/bad-request-pred req client-error?)
100-
fallback (:hystrix/fallback-fn req default-fallback)
104+
(let [req (merge hystrix-defaults req)
105+
bad-request-pred (:hystrix/bad-request-pred req)
106+
fallback (:hystrix/fallback-fn req)
101107
wrap-bad-request (fn [resp] (if (bad-request-pred req resp)
102108
(throw (HystrixBadRequestException. "Ignored bad request"
103109
(wrap {:object resp
104110
:message "Bad request pred"
105111
:stack-trace (stack-trace)})))
106112
resp))
107113
wrap-exception-response (fn [resp] ((http/wrap-exceptions (constantly resp)) (assoc req :throw-exceptions true)))
108-
^HystrixCommand$Setter configurator (configurator req)
114+
configurator (configurator req)
109115
logging-context (or (MDC/getCopyOfContextMap) {})
110116
command (proxy [HystrixCommand] [configurator]
111117
(getFallback []

0 commit comments

Comments
 (0)