/
closeable_map.clj
594 lines (504 loc) · 21.9 KB
/
closeable_map.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
(ns piotr-yuxuan.closeable-map
;; Manually keep this description in sync with relevant parts of the README.
"In your project, require:
``` clojure
(require '[piotr-yuxuan.closeable-map :as closeable-map :refer [close-with with-tag]])
```
Define an application that can be started, and closed.
``` clojure
(defn start
\"Return a map describing a running application, and which values may
be closed.\"
[config]
(closeable-map/closeable-map
{;; Kafka producers/consumers are `java.io.Closeable`.
:producer (kafka-producer config)
:consumer (kafka-consumer config)}))
```
You can start/stop the app in the repl with:
``` clojure
(comment
(def config (load-config))
(def system (start config))
;; Stop/close all processes/resources with:
(.close system)
)
```
It can be used in conjunction with `with-open` in test file to create
well-contained, independent tests:
``` clojure
(with-open [{:keys [consumer] :as app} (start config)]
(testing \"unit test with isolated, repeatable context\"
(is (= :yay/🚀 (some-business/function consumer)))))
```
You could also use thi library while live-coding to stop and restart
your application whenever a file is changed.
## More details
``` clojure
(defn start
\"Return a map describing a running application, and which values may
be closed.\"
[config]
(closeable-map/closeable-map
{;; Kafka producers/consumers are `java.io.Closeable`.
:producer (kafka-producer config)
:consumer (kafka-consumer config)
;; File streams are `java.io.Closeable` too:
:logfile (io/output-stream (io/file \"/tmp/log.txt\"))
;; Closeable maps can be nested. Nested maps will be closed before the outer map.
:backend/api {:response-executor (close-with (memfn ^ExecutorService .shutdown)
(flow/utilization-executor (:executor config)))
:connection-pool (close-with (memfn ^IPool .shutdown)
(http/connection-pool {:pool-opts config}))
;; These functions receive their map as argument.
::closeable-map/before-close (fn [m] (backend/give-up-leadership config m))
::closeable-map/after-close (fn [m] (backend/close-connection config m))}
;; Any exception when closing this nested map will be swallowed
;; and not bubbled up.
:db ^::closeable-map/swallow {;; Connection are `java.io.Closeable`, too:
:db-conn (jdbc/get-connection (:db config))}
;; Some libs return a zero-argument function which when called
;; stops the server, like:
:server (with-tag ::closeable-map/fn (http/start-server (api config) (:server config)))
;; Gotcha: Clojure meta data can only be attached on 'concrete'
;; objects; they are lost on literal forms (see above).
:forensic ^::closeable-map/fn #(metrics/report-death!)
::closeable-map/ex-handler
(fn [ex]
;; Will be called for all exceptions thrown when closing this
;; map and nested items.
(println (ex-message ex)))}))
```
When `(.close system)` is executed, it will:
- Recursively close all instances of `java.io.Closeable` and
`java.lang.AutoCloseable`;
- Recursively call all stop zero-argument functions tagged with
`^::closeable-map/fn`;
- Skip all nested `Closeable` under a `^::closeable-map/ignore`;
- Silently swallow any exception with `^::closeable-map/swallow`;
- Exceptions to optional `::closeable-map/ex-handler` in key or
metadata;
- If keys (or metadata) `::closeable-map/before-close` or
`::closeable-map/after-close` are present, they will be assumed as
a function which takes one argument (the map itself) and used run
additional closing logic:
``` clojure
(closeable-map
{;; This function will be executed before the auto close.
::closeable-map/before-close (fn [this-map] (flush!))
;; Kafka producers/consumers are java.io.Closeable
:producer (kafka-producer config)
:consumer (kafka-consumer config)
;; This function will be executed after the auto close.
::closeable-map/after-close (fn [this-map] (garbage/collect!))})
```
Some classes do not implement `java.lang.AutoCloseable` but present
some similar method. For example instances of
`java.util.concurrent.ExecutorService` can't be closed but they can be
`.shutdown`:
``` clojure
{:response-executor (close-with (memfn ^ExecutorService .shutdown)
(flow/utilization-executor (:executor config)))
:connection-pool (close-with (memfn ^IPool .shutdown)
(http/connection-pool {:pool-opts config}))}
```
You may also extend this library by giving new dispatch values to
multimethod [[piotr-yuxuan.closeable-map/close!]]. Once evaluated,
this will work accross all your code. The multimethod is dispatched on
the concrete class of its argument:
``` clojure
(import '(java.util.concurrent ExecutorService))
(defmethod closeable-map/close! ExecutorService
[x]
(.shutdown ^ExecutorService x))
(import '(io.aleph.dirigiste IPool))
(defmethod closeable-map/close! IPool
[x]
(.shutdown ^IPool x))
```
## All or nothing
### No half-broken closeable map
You may also avoid partially open state when an exception is thrown
when creating a `CloseableMap`. This is where `closeable-map*` comes
handy. It outcome in one of the following:
- Either everything went right, and all inner forms wrapped by
`closeable` correctly return a value; you get an open instance of `CloseableMap`.
- Either some inner form wrapped by `closeable` didn't return a
closeable object but threw an exception instead. Then all
`closeable` forms are closed, and finally the exception is
bubbled up.
``` clojure
(closeable-map*
{:server (closeable* (http/start-server (api config)))
:kafka {:consumer (closeable* (kafka-consumer config))
:producer (closeable* (kafka-producer config))
:schema.registry.url \"https://localhost\"}})
```
### No half-broken state in general code
In some circumstances you may need to handle exception on the creation
of a closeable map. If an exception happens during the creation of the
map, values already evaluated will be closed. No closeable objects
will be left open with no references to them.
For instance, this form would throw an exception:
``` clojure
(closeable-map/closeable-map {:server (http/start-server (api config))
:kafka {:consumer (kafka-consumer config)
:producer (throw (ex-info \"Exception\" {}))}})
;; => (ex-info \"Exception\" {})
```
`with-closeable*` prevents that kind of broken, partially open states for its bindings:
``` clojure
(with-closeable* [server (http/start-server (api config))
consumer (kafka-consumer config)
producer (throw (ex-info \"Exception\" {}))]
;; Your code goes here.
)
;; Close consumer,
;; close server,
;; finally throw `(ex-info \"Exception\" {})`.
```
You now have the guarantee that your code will only be executed if
all these closeable are open. In the latter example an exception is
thrown when `producer` is evaluated, so `consumer` is closed, then
`server` is closed, and finally the exception is bubbled up. Your
code is not evaluated. In the next example the body is evaluated,
but throws an exception: all bindings are closed.
``` clojure
(with-closeable* [server (http/start-server (api config))
consumer (kafka-consumer config)
producer (kafka-producer config)]
;; Your code goes here.
(throw (ex-info \"Exception\" {})))
;; Close producer,
;; close consumer,
;; close server,
;; finally throw `(ex-info \"Exception\" {})`.
```
When no exception is thrown, leave bindings open and return like a
normal `let` form. If you prefer to close bindings, use `with-open` as
usual.
``` clojure
(with-closeable* [server (http/start-server (api config))
consumer (kafka-consumer config)
producer (kafka-producer config)]
;; Your code goes here.
)
;; All closeable in bindings stay open.
;; => result
```"
(:require [clojure.data]
[clojure.walk :as walk]
[potemkin :refer [def-map-type]])
(:import (java.io Closeable)
(java.lang AutoCloseable)
(java.util Map)))
(def ^:dynamic *swallow?*
"Dynamic var. If bound to a logically true value in closing thread,
will swallow any `java.lang.Throwable`, the apex class for all
exceptions in Java and Clojure. You may change its value is some
nested maps with meta `{::swallow false}`.
Because `clojure.walk/walk` is used for map traversal, it is not
possible to pass down any argument. Also, as we iteratively walk
through nested data structures, some of them do not support
metadata. As a result, a binding on this dynamic var in the
execution thread enables a simple way to remember the parent value
as we visit the children."
false)
(def ^:dynamic *?ex-handler*
"Dynamic var. If non-nil, will be invoked with the exception as
argument A one-argument function is excepted.
Because `clojure.walk/walk` is used for map traversal, it is not
possible to pas s down any argument. Also, as we iteratively walk
through nested data structures, some of them do not support
metadata. As a result, a binding on this dynamic var in the
execution thread enables a simple way to remember the parent value
as we visit the children."
nil)
(def ^:dynamic *ignore?*
"Dynamic var. If bound to a logically true value in closing thread,
will ignore any closeable items. You may change its value is some
nested maps with meta `{::ignore false}`.
Because `clojure.walk/walk` is used for map traversal, it is not
possible to pass down any argument. Also, as we iteratively walk
through nested data structures, some of them do not support
metadata. As a result, a binding on this dynamic var in the
execution thread enables a simple way to remember the parent value
as we visit the children."
false)
(def ^:dynamic *closeables*
"FIXME cljdoc"
nil)
(defmulti close!
"Perform a side effect of the form `x` passed as argument and attempts
to close it. If it doesn't know how to close it, does
nothing. Functions tagged with `^::fn` are considered closeable as
`java.io.Closeable` and `java.lang.AutoCloseable`.
This multimethod is dispatched on the concrete class of its
argument. You can extend this method like any other multimethod.
``` clojure
(import '(java.util.concurrent ExecutorService))
(defmethod closeable-map/close! ExecutorService
[x]
(.shutdown ^ExecutorService x))
(import '(io.aleph.dirigiste IPool))
(defmethod closeable-map/close! IPool
[x]
(.shutdown ^IPool x))
```"
class)
(defmethod close! :default
[x]
(let [fn-tag (::fn (meta x))]
;; AutoCloseable is a superinterface of Closeable.
(cond (instance? AutoCloseable x) (.close ^AutoCloseable x)
(true? fn-tag) (x)
fn-tag (fn-tag x))))
(def visitor
"Take a form `x` as one argument and traverse it while trying to
[[piotr-yuxuan.closeable-map/close!]] inner items.
Map keys `::before-close` and `::after-close` will be invoked before
and after other keys of the map. Will ignore items marked with
`^::ignore`. Exceptions when closing some item will be passed to an
optional `::ex-handler`. With `^::swallow` they will not be raised
higher and will stay silently swallowed. Functions tagged with
`^::fn` will be considered closeable and will thus be called on
`.close`.
For advance usage, no checks on closing functions, the minimal
requirement is they be invokable. Map keys can also be tagged. Maps
values `::before-close` and `::after-close` are not expected to be
tagged with `^::fn` for they would be executed twice."
(letfn [(before-close [x f] (when-let [close! (or (and (instance? Map x)
(::before-close x))
(::before-close (meta x)))]
(f x close!)))
(after-close [x f] (when-let [close! (or (and (instance? Map x)
(::after-close x))
(::after-close (meta x)))]
(f x close!)))
(?ex-handler [x] (cond (contains? (meta x) ::ex-handler) (::ex-handler (meta x))
(and (instance? Map x) (contains? x ::ex-handler)) (::ex-handler x)
:else *?ex-handler*))
(swallow? [x] (cond (contains? (meta x) ::swallow) (::swallow (meta x))
(and (instance? Map x) (contains? x ::swallow?)) (::swallow? x)
:else *swallow?*))
(swallow [x f] (if (or (swallow? x)
(swallow? f))
(try (f x)
(catch Throwable th
(when (?ex-handler x)
(*?ex-handler* th)))
(finally x))
(doto x f)))
(ignore? [x] (cond (contains? (meta x) ::ignore) (::ignore (meta x))
(and (instance? Map x) (contains? x ::ignore?)) (::ignore? x)
:else *ignore?*))]
(fn visitor
[x]
(binding [*swallow?* (swallow? x)
*ignore?* (ignore? x)
*?ex-handler* (?ex-handler x)]
(walk/walk visitor
#(do (when-not (ignore? %)
(swallow % close!)
(after-close % swallow))
%)
(do (when-not (ignore? x)
(before-close x swallow))
x))))))
(def-map-type CloseableMap [m mta]
(get [_ k default-value] (get m k default-value))
(assoc [_ k v] (CloseableMap. (assoc m k v) mta))
(dissoc [_ k] (CloseableMap. (dissoc m k) mta))
(keys [_] (keys m))
(meta [_] mta)
(with-meta [_ mta] (CloseableMap. m mta))
Closeable ;; Closeable is a subinterface of AutoCloseable.
(^void close [this] (visitor m)))
(defn ^CloseableMap closeable-map
"Take any object that implements a map interface and return a new map
that can later be closed. You may use this map like any other map
for example with `update` or `assoc`. When you call `(.close m)`,
inner closeable items will be closed.
Map keys `::before-close` and `::after-close` will be evaluated
before and after other keys of the map. Will ignore items marked
with `^::ignore`, and exceptions thrown under `^::swallow` will be
silently swallowed. Functions tagged with `^::fn` will be considered
closeable. No checks on closing functions, the minimal requirement
is they be invokable."
[m]
{:pre [(instance? Map m)]}
(CloseableMap. m (meta m)))
(def ^:constant ^CloseableMap empty-map
"An empty, immutable closeable map that you may use like any other
map. When you call `(.close m)` on it, inner closeable items will be
closed."
(closeable-map {}))
(def ^:constant ^CloseableMap swallowed
"An empty, immutable closeable map that swallows all exceptions. A
nested closeable item may throw an exception by setting its metadata
to `{::swallow false}`. You may use it like any other map."
(closeable-map ^::swallow {}))
(def ^:constant ^CloseableMap ignored
"An empty, immutable closeable map that ignores all closeable. A
nested closeable may be no longer ignored if its metadata contain
`{::ignore false}`. You may use it like any other map."
(closeable-map ^::ignore {}))
(defmacro with-tag-
"The code is the docstring:
``` clojure
(defmacro -with-tag
\"The code is the docstring: [truncated]\"
[x tag]
`(vary-meta ~x assoc ~tag true))
```"
[x tag]
`(vary-meta ~x assoc ~tag true))
(defn with-tag
"By design, the Clojure shortcut notation `^::closeable-map/fn {}`
works only on direct objects, not on bindings, or literal forms. use
this function to circumvent this limitation.
``` clojure
(meta
(let [a {}]
^::closeable-map/fn a))
;; => nil
(meta
(let [a {}]
(with-tag ::closeable-map/fn a)))
;; => #:piotr-yuxuan.closeable-map{:fn true}
```"
[tag x]
(with-tag- x tag))
(defmacro close-with
"Take a procedure `proc`, an object `x`, return `x`. When the map is
closed `(proc x)` will be called. Will have no effect out of a
closeable map.
Some classes do not implement `java.lang.AutoCloseable` but present
some similar method. For example instances of
`java.util.concurrent.ExecutorService` can't be closed but they can
be shut down, which achieves a similar outcome. This convenience
macro allows you to express it this way:
``` clojure
(closeable-map/close-with (memfn ^ExecutorService .shutdown) my-service)
;; => my-service
```"
[proc x]
`(vary-meta ~x assoc ::fn ~proc))
(defmacro closeable*
"Use it within `closeable-map*` or `with-closeable*` to avoid
partially open state when an exception is thrown on evaluating
closeable forms."
[x]
`(do (assert *closeables* "`closeable` should only be used within `closeable-map*` or `with-closeable*`.")
(let [ret# ~x]
(swap! *closeables* conj ret#)
ret#)))
(defmacro with-closeable*
"Take two arguments, a `bindings` vector and a `body`. Like a `let`,
support destructuring. Avoids partially open state when an exception
is thrown on evaluating closeable forms. Evaluate bindings
sequentially then return `body` and leave bindings open. When an
exception is thrown in a later binding or in the `body`, close
bindings already open in reverse order and finally bubble up the
exception. Do nothing for non-closeable bindings.
Use it if you need exception handling on the creation of a closeable
map, so no closeable objects are left open but with no references
because of an exception.
For instance, this form would throw an exception and leave the
server open and the port locked:
``` clojure
(closeable-map {:server (http/start-server (api config))
:kafka {:consumer (kafka-consumer config)
:producer (throw (ex-info \"Exception\" {}))}})
;; `consumer` and `server` stay open but with no reference. Kafka
;; messages are consumed and the port is locked.
;; => (ex-info \"Exception\" {})
```
`with-closeable*` prevents that kind of broken, partially open
states for its bindings:
``` clojure
(with-closeable* [server (http/start-server (api config))
consumer (kafka-consumer config)
producer (throw (ex-info \"Exception\" {}))]
;; Your code goes here.
)
;; Close consumer,
;; close server,
;; finally throw `(ex-info \"Exception\" {})`.
```
You now have the guarantee that your code will only be executed if
all these closeable are open. In the latter example an exception is
thrown when `producer` is evaluated, so `consumer` is closed, then
`server` is closed, and finally the exception is bubbled up. Your
code is not evaluated. In the next example the body is evaluated,
but throws an exception: all bindings are closed.
``` clojure
(with-closeable* [server (http/start-server (api config))
consumer (kafka-consumer config)
producer (kafka-producer config)]
;; Your code goes here.
(throw (ex-info \"Exception\" {})))
;; Close producer,
;; close consumer,
;; close server,
;; finally throw `(ex-info \"Exception\" {})`.
```
When no exception is thrown, leave bindings open and return like a
normal `let` form.
``` clojure
(with-closeable* [server (http/start-server (api config))
consumer (kafka-consumer config)
producer (kafka-producer config)]
;; Your code goes here.
)
;; All closeable in bindings stay open.
;; => result
```"
[bindings & body]
(assert (even? (count bindings)) "Expecting an even number of forms in `bindings`.")
(if (zero? (count bindings))
`(binding [*closeables* (or *closeables* (atom ()))]
~@body)
(let [v (gensym "value")]
`(binding [*closeables* (or *closeables* (atom ()))]
(try
(let [~(nth bindings 0) ~(nth bindings 1) ; unsplice?
~v ~(nth bindings 0)]
(swap! *closeables* conj ~v)
(with-closeable* ~(subvec bindings 2) ~@body))
(catch Throwable th#
(doseq [c# @*closeables*]
(try (close! c#)
(catch Throwable _#)
(finally (swap! *closeables* rest))))
(throw th#)))))))
(defmacro ^CloseableMap closeable-map*
"Avoid partially open state when an exception is thrown on evaluating
inner closeable forms wrapped by `closeable`.
``` clojure
(closeable-map*
{:server (closeable* (http/start-server (api config)))
:kafka {:consumer (closeable* (kafka-consumer config))
:producer (closeable* (kafka-producer config))
:schema.registry.url \"https://localhost\"}})
```
The outcome of this macro `closeable-map*` is one of the following:
- Either everything went right, and all inner forms wrapped by
`closeable` correctly return a value, then this macro returns an
open instance of `CloseableMap`.
- Either some inner form wrapped by `closeable` didn't return a
closeable object but threw an exception instead. Then all
`closeable` forms are closed, and finally the exception is
bubbled up.
Known (minor) issue: type hint is not acknowledged, you have to tag
it yourself with `^CloseableMap` (most precise),
`^java.io.Closeable`, or `^java.lang.AutoCloseable` (most generic)."
[m]
`(binding [*closeables* (or *closeables* (atom ()))]
(try (vary-meta (closeable-map ~m) assoc :tag `CloseableMap)
(catch Throwable th#
(doseq [c# @*closeables*]
(try (close! c#)
(catch Throwable _#)
(finally (swap! *closeables* rest))))
(throw th#)))))