|
38 | 38 | (-poll! [_]) |
39 | 39 | (-offer! [_ _] [_ _ _])) |
40 | 40 |
|
41 | | -(defprotocol IBulkhead |
42 | | - "Bulkhead main API" |
43 | | - (-invoke [_ f] "Call a function in bulkhead context and return a result")) |
| 41 | +;; (defprotocol IBulkhead |
| 42 | +;; "Bulkhead main API" |
| 43 | +;; (-invoke [_ f] "Call a function in bulkhead context and return a result")) |
44 | 44 |
|
45 | 45 | (extend-type BlockingQueue |
46 | 46 | IQueue |
|
82 | 82 | cp/Datafiable |
83 | 83 | (datafy [_] |
84 | 84 | {:permits (.availablePermits semaphore) |
| 85 | + :executor executor |
85 | 86 | :queue (.size queue) |
86 | 87 | :max-permits max-permits |
87 | 88 | :max-queue max-queue |
88 | 89 | :timeout timeout}) |
89 | 90 |
|
90 | | - IBulkhead |
91 | | - (-invoke [this f] |
92 | | - (px/await! (px/submit! this f))) |
| 91 | + ;; IBulkhead |
| 92 | + ;; (-invoke [this f] |
| 93 | + ;; (px/await! (px/submit! this f))) |
93 | 94 |
|
94 | 95 | Executor |
95 | 96 | (execute [this f] |
|
127 | 128 | (ns-unmap *ns* 'map->Bulkhead) |
128 | 129 | (ns-unmap *ns* '->Task) |
129 | 130 |
|
130 | | -(defrecord SemaphoreBulkhead [^Semaphore semaphore |
131 | | - ^AtomicInteger counter |
132 | | - max-permits |
133 | | - max-queue |
134 | | - timeout] |
135 | | - cp/Datafiable |
136 | | - (datafy [_] |
137 | | - {:permits (.availablePermits semaphore) |
138 | | - :queue (+ (long counter) (long max-permits)) |
139 | | - :max-permits max-permits |
140 | | - :max-queue max-queue}) |
141 | | - |
142 | | - IBulkhead |
143 | | - (-invoke [this f] |
144 | | - (let [nqueued (.incrementAndGet counter)] |
145 | | - (when (> (long nqueued) (long max-queue)) |
146 | | - (let [hint (str "bulkhead: queue max capacity reached (" max-queue ")") |
147 | | - props {:type :bulkhead-error |
148 | | - :code :capacity-limit-reached |
149 | | - :size max-queue}] |
150 | | - (.decrementAndGet counter) |
151 | | - (throw (ex-info hint props)))) |
152 | | - |
153 | | - (try |
154 | | - (if (psm/acquire! semaphore :permits 1 :timeout timeout) |
155 | | - (try |
156 | | - (f) |
157 | | - (finally |
158 | | - (psm/release! semaphore))) |
159 | | - (let [props {:type :bulkhead-error |
160 | | - :code :timeout |
161 | | - :timeout timeout}] |
162 | | - (throw (ex-info "bulkhead: timeout" props)))) |
163 | | - (finally |
164 | | - (.decrementAndGet counter)))))) |
165 | | - |
166 | | -(ns-unmap *ns* '->SemaphoreBulkhead) |
167 | | -(ns-unmap *ns* 'map->SemaphoreBulkhead) |
| 131 | +;; (defrecord SemaphoreBulkhead [^Semaphore semaphore |
| 132 | +;; ^AtomicInteger counter |
| 133 | +;; max-permits |
| 134 | +;; max-queue |
| 135 | +;; timeout] |
| 136 | +;; cp/Datafiable |
| 137 | +;; (datafy [_] |
| 138 | +;; {:permits (.availablePermits semaphore) |
| 139 | +;; :queue (+ (long counter) (long max-permits)) |
| 140 | +;; :max-permits max-permits |
| 141 | +;; :max-queue max-queue}) |
| 142 | + |
| 143 | +;; IBulkhead |
| 144 | +;; (-invoke [this f] |
| 145 | +;; (let [nqueued (.incrementAndGet counter)] |
| 146 | +;; (when (> (long nqueued) (long max-queue)) |
| 147 | +;; (let [hint (str "bulkhead: queue max capacity reached (" max-queue ")") |
| 148 | +;; props {:type :bulkhead-error |
| 149 | +;; :code :capacity-limit-reached |
| 150 | +;; :size max-queue}] |
| 151 | +;; (.decrementAndGet counter) |
| 152 | +;; (throw (ex-info hint props)))) |
| 153 | + |
| 154 | +;; (try |
| 155 | +;; (if (psm/acquire! semaphore :permits 1 :timeout timeout) |
| 156 | +;; (try |
| 157 | +;; (f) |
| 158 | +;; (finally |
| 159 | +;; (psm/release! semaphore))) |
| 160 | +;; (let [props {:type :bulkhead-error |
| 161 | +;; :code :timeout |
| 162 | +;; :timeout timeout}] |
| 163 | +;; (throw (ex-info "bulkhead: timeout" props)))) |
| 164 | +;; (finally |
| 165 | +;; (.decrementAndGet counter)))))) |
| 166 | + |
| 167 | +;; (ns-unmap *ns* '->SemaphoreBulkhead) |
| 168 | +;; (ns-unmap *ns* 'map->SemaphoreBulkhead) |
168 | 169 |
|
169 | 170 |
|
170 | 171 | ;; --- PUBLIC API |
171 | 172 |
|
172 | 173 | (defn create |
173 | | - [& {:keys [type permits queue timeout] :as params}] |
174 | | - (case type |
175 | | - (:async :executor) |
176 | | - (let [executor (px/resolve-executor (:executor params)) |
177 | | - max-queue (or queue Integer/MAX_VALUE) |
178 | | - max-permits (or permits 1) |
179 | | - queue (LinkedBlockingQueue. (int max-queue)) |
180 | | - semaphore (Semaphore. (int max-permits))] |
181 | | - (Bulkhead. executor |
182 | | - semaphore |
183 | | - queue |
184 | | - max-permits |
185 | | - max-queue |
186 | | - timeout)) |
187 | | - |
188 | | - (:sync :semaphore) |
189 | | - (let [max-queue (or queue Integer/MAX_VALUE) |
190 | | - max-permits (or permits 1) |
191 | | - counter (AtomicInteger. (int (- max-permits))) |
192 | | - semaphore (Semaphore. (int max-permits))] |
193 | | - (SemaphoreBulkhead. semaphore |
194 | | - counter |
195 | | - max-permits |
196 | | - max-queue |
197 | | - timeout)))) |
198 | | - |
199 | | -(defn invoke! |
200 | | - [instance f] |
201 | | - (-invoke instance f)) |
| 174 | + [& {:keys [permits queue timeout executor] :as params}] |
| 175 | + (let [executor (px/resolve-executor executor) |
| 176 | + max-queue (or queue Integer/MAX_VALUE) |
| 177 | + max-permits (or permits 1) |
| 178 | + queue (LinkedBlockingQueue. (int max-queue)) |
| 179 | + semaphore (Semaphore. (int max-permits))] |
| 180 | + (Bulkhead. executor |
| 181 | + semaphore |
| 182 | + queue |
| 183 | + max-permits |
| 184 | + max-queue |
| 185 | + timeout))) |
| 186 | + |
| 187 | +;; (defn create |
| 188 | +;; [& {:keys [type permits queue timeout executor] :as params}] |
| 189 | +;; (case type |
| 190 | +;; ;; (:async :executor) |
| 191 | +;; (let [executor (px/resolve-executor executor) |
| 192 | +;; max-queue (or queue Integer/MAX_VALUE) |
| 193 | +;; max-permits (or permits 1) |
| 194 | +;; queue (LinkedBlockingQueue. (int max-queue)) |
| 195 | +;; semaphore (Semaphore. (int max-permits))] |
| 196 | +;; (Bulkhead. executor |
| 197 | +;; semaphore |
| 198 | +;; queue |
| 199 | +;; max-permits |
| 200 | +;; max-queue |
| 201 | +;; timeout)) |
| 202 | + |
| 203 | +;; (:sync :semaphore) |
| 204 | +;; (let [max-queue (or queue Integer/MAX_VALUE) |
| 205 | +;; max-permits (or permits 1) |
| 206 | +;; counter (AtomicInteger. (int (- max-permits))) |
| 207 | +;; semaphore (Semaphore. (int max-permits))] |
| 208 | +;; (SemaphoreBulkhead. semaphore |
| 209 | +;; counter |
| 210 | +;; max-permits |
| 211 | +;; max-queue |
| 212 | +;; timeout)))) |
| 213 | + |
| 214 | +;; (defn invoke! |
| 215 | +;; [instance f] |
| 216 | +;; (-invoke instance f)) |
| 217 | + |
| 218 | +;; (defn submit |
| 219 | +;; [instance f] |
| 220 | +;; (-invoke instance f)) |
202 | 221 |
|
203 | 222 | (defn get-stats |
204 | 223 | [instance] |
|
207 | 226 | (defn bulkhead? |
208 | 227 | "Check if the provided object is instance of Bulkhead type." |
209 | 228 | [o] |
210 | | - (satisfies? IBulkhead o)) |
| 229 | + (satisfies? Bulkhead o)) |
0 commit comments