-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
bulkhead.clj
28 lines (24 loc) · 941 Bytes
/
bulkhead.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(ns diehard.bulkhead
(:import [java.util.concurrent Semaphore TimeUnit]))
(def ^{:const true :no-doc true}
allowed-bulkhead-option-keys
#{:concurrency})
(defprotocol IBulkhead
(acquire! [_] [this timeout-ms])
(release! [_]))
(defrecord Bulkhead [semaphore]
IBulkhead
(acquire! [_] (.acquire ^Semaphore semaphore))
(acquire! [_ timeout-ms]
(when-not (.tryAcquire ^Semaphore semaphore timeout-ms TimeUnit/MILLISECONDS)
(throw (ex-info "Failed to acquire semaphore" {:bulkhead true
:max-wait-ms timeout-ms}))))
(release! [_]
(.release ^Semaphore semaphore)))
(defn bulkhead
"Create bulkhead with given configuration:
* `concurrency`: the max number of concurrent executions"
[opts]
(if-let [concurrency (:concurrency opts)]
(Bulkhead. (Semaphore. concurrency))
(throw (IllegalArgumentException. ":concurrency is required for bulkhead"))))