closeable-map
<scherz>
Think about Zelda: when hens are free to propagate
everywhere, they attack you and it becomes a mess. Your application
state is like your hens: it's safe when it is securely contained in a
coop with automated doors to prevent run-aways.</scherz>
This small Clojure library tries to get as close as possible to the
bare essential complexity of state management in code. It defines a
new type of Clojure map that you may (.close m)
. <scherz>
See it in
action above</scherz>
.
This library defines a new type of map that you may use like any other
map. This map may contain stateful Java objects like a server, a Kafka
producer, a file output stream. When you want to clean your state, you
just .close
the map and all nested stateful objects will be closed
recursively.
It is a tiny alternative to more capable projects:
with-open
:
jarohen/with-openIn your project, require:
(require '[piotr-yuxuan.closeable-map :as closeable-map :refer [close-with with-tag close!]])
Define an application that can be started, and closed.
(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:
(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:
(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.
(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:
(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
:
{: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}))}
A Java object does not implement interface clojure.lang.IObj
so it
is unable to carry Clojure metadata. As such, you can't give a it a
tag like ::closeable-map/fn
. You may also extend this library by
giving new dispatch values to multimethod close!
. Once evaluated,
this will work accross all your code. The multimethod is dispatched on
the concrete class of its argument:
(import '(java.util.concurrent ExecutorService))
(defmethod close! ExecutorService
[x]
(.shutdown ^ExecutorService x))
(import '(io.aleph.dirigiste IPool))
(defmethod close! IPool
[x]
(.shutdown ^IPool x))
(import '(clojure.lang Atom))
(defmethod close! Atom
[x]
(reset! x nil))
(import '(clojure.core.async.impl.protocols Channel))
(defmethod close! Channel
[x]
(async/close! x))
A Java object may be wrapped as a closeable*
.
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.
(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"}})
Closeable objects not directly within the map may still be closed if
wrapped in closeable*
:
(closeable-map*
(closeable* (kafka-consumer config))
(closeable* (kafka-producer config))
{:server (closeable* (http/start-server (api config)))
:schema.registry.url "https://localhost"})
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:
(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:
(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.
(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.
(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
Some Clojure datastructures implement IFn
:
({:a 1} :a) ;; => 1
(remove #{:a} [:a :b :c]) ;; => '(:b :c)
([:a :b :c] 1) ;; => :b
Clojure maps (IPersistentMap
) implement IFn
, for invoke()
of one
argument (a key) with an optional second argument (a default value),
i.e. maps are functions of their keys. nil
keys and values are fine.
This library defines a new data strucure, CloseableMap. It is exposed
as an instance of java.io.Closeable
which is a subinterface of
java.lang.AutoCloseable
. When trying to close its values, it looks
for instances of the latter. As such, it tries to be most general.
(require '[clojure.data])
(clojure.data/diff
(ancestors (class {}))
(ancestors CloseableMap))
;; =>
[;; Ancestors of Clojure map only but not CloseableMap.
#{clojure.lang.AFn ; Concrete type, but see below for IFn.
clojure.lang.APersistentMap
clojure.lang.IEditableCollection
clojure.lang.IKVReduce
clojure.lang.IMapIterable
java.io.Serializable}
;; Ancestors of CloseableMap only.
#{clojure.lang.IType
java.io.Closeable
java.lang.AutoCloseable
java.util.Iterator
potemkin.collections.PotemkinMap
potemkin.types.PotemkinType}
;; Ancestors common to both types.
#{clojure.lang.Associative
clojure.lang.Counted
clojure.lang.IFn
clojure.lang.IHashEq
clojure.lang.ILookup
clojure.lang.IMeta
clojure.lang.IObj
clojure.lang.IPersistentCollection
clojure.lang.IPersistentMap
clojure.lang.MapEquivalence
clojure.lang.Seqable
java.lang.Iterable
java.lang.Object
java.lang.Runnable
java.util.Map
java.util.concurrent.Callable}]
# `closeable-map` [![Build status](https://img.shields.io/github/workflow/status/piotr-yuxuan/closeable-map/Walter%20CD)](https://github.com/piotr-yuxuan/closeable-map/actions/workflows/walter-cd.yml) [![Clojars badge](https://img.shields.io/clojars/v/piotr-yuxuan/closeable-map.svg)](https://clojars.org/piotr-yuxuan/closeable-map) [![Clojars downloads](https://img.shields.io/clojars/dt/piotr-yuxuan/closeable-map)](https://clojars.org/piotr-yuxuan/closeable-map) [![cljdoc badge](https://cljdoc.org/badge/piotr-yuxuan/closeable-map)](https://cljdoc.org/d/piotr-yuxuan/closeable-map/CURRENT) [![GitHub license](https://img.shields.io/github/license/piotr-yuxuan/closeable-map)](https://github.com/piotr-yuxuan/closeable-map/blob/main/LICENSE) [![GitHub issues](https://img.shields.io/github/issues/piotr-yuxuan/closeable-map)](https://github.com/piotr-yuxuan/closeable-map/issues) `<scherz>`Think about Zelda: when hens are free to propagate everywhere, they attack you and it becomes a mess. Your application state is like your hens: it's safe when it is securely contained in a coop with automated doors to prevent run-aways.`</scherz>` ![](./doc/automatische-huehnerklappe.jpg) This small Clojure library tries to get as close as possible to the bare essential complexity of state management in code. It defines a new type of Clojure map that you may `(.close m)`. `<scherz>`See it in action above`</scherz>`. This library defines a new type of map that you may use like any other map. This map may contain stateful Java objects like a server, a Kafka producer, a file output stream. When you want to clean your state, you just `.close` the map and all nested stateful objects will be closed recursively. It is a tiny alternative to more capable projects: - Application state management in a map: [juxt/clip](https://github.com/juxt/clip) - Simple library to initialise an app stack: [sathyavijayan/upit](https://github.com/sathyavijayan/upit) - Application state management: [stuartsierra/component](https://github.com/stuartsierra/component), [weavejester/integrant](weavejester/integrant), [tolitius/mount](https://github.com/tolitius/mount), _et al_. - Extension of `with-open`: [jarohen/with-open](https://github.com/jarohen/with-open) - Representing state in a map: [robertluo/fun-map](https://github.com/robertluo/fun-map) ## Usage In your project, require: ``` clojure (require '[piotr-yuxuan.closeable-map :as closeable-map :refer [close-with with-tag close!]]) ``` 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}))} ``` ## Java objects A Java object does not implement interface `clojure.lang.IObj` so it is unable to carry Clojure metadata. As such, you can't give a it a tag like `::closeable-map/fn`. You may also extend this library by giving new dispatch values to multimethod `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 close! ExecutorService [x] (.shutdown ^ExecutorService x)) (import '(io.aleph.dirigiste IPool)) (defmethod close! IPool [x] (.shutdown ^IPool x)) (import '(clojure.lang Atom)) (defmethod close! Atom [x] (reset! x nil)) (import '(clojure.core.async.impl.protocols Channel)) (defmethod close! Channel [x] (async/close! x)) ``` A Java object may be wrapped as a `closeable*`. ## 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"}}) ``` Closeable objects not directly within the map may still be closed if wrapped in `closeable*`: ``` clojure (closeable-map* (closeable* (kafka-consumer config)) (closeable* (kafka-producer config)) {:server (closeable* (http/start-server (api 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 ``` ## Technicalities Some Clojure datastructures implement `IFn`: ``` clojure ({:a 1} :a) ;; => 1 (remove #{:a} [:a :b :c]) ;; => '(:b :c) ([:a :b :c] 1) ;; => :b ``` Clojure maps (`IPersistentMap`) implement `IFn`, for `invoke()` of one argument (a key) with an optional second argument (a default value), i.e. maps are functions of their keys. `nil` keys and values are fine. This library defines a new data strucure, CloseableMap. It is exposed as an instance of `java.io.Closeable` which is a subinterface of `java.lang.AutoCloseable`. When trying to close its values, it looks for instances of the latter. As such, it tries to be most general. ``` clojure (require '[clojure.data]) (clojure.data/diff (ancestors (class {})) (ancestors CloseableMap)) ;; => [;; Ancestors of Clojure map only but not CloseableMap. #{clojure.lang.AFn ; Concrete type, but see below for IFn. clojure.lang.APersistentMap clojure.lang.IEditableCollection clojure.lang.IKVReduce clojure.lang.IMapIterable java.io.Serializable} ;; Ancestors of CloseableMap only. #{clojure.lang.IType java.io.Closeable java.lang.AutoCloseable java.util.Iterator potemkin.collections.PotemkinMap potemkin.types.PotemkinType} ;; Ancestors common to both types. #{clojure.lang.Associative clojure.lang.Counted clojure.lang.IFn clojure.lang.IHashEq clojure.lang.ILookup clojure.lang.IMeta clojure.lang.IObj clojure.lang.IPersistentCollection clojure.lang.IPersistentMap clojure.lang.MapEquivalence clojure.lang.Seqable java.lang.Iterable java.lang.Object java.lang.Runnable java.util.Map java.util.concurrent.Callable}] ```
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.
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.
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.
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.
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.
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.
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.
(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))
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)) ```
(close-with proc x)
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:
(closeable-map/close-with (memfn ^ExecutorService .shutdown) my-service)
;; => my-service
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 ```
(closeable* x)
Use it within closeable-map*
or with-closeable*
to avoid
partially open state when an exception is thrown on evaluating
closeable forms.
When x
is a Java object, store as a object that may be closed. You
may then extend the multimethod close!
to provide a standard way
to close this class of objects.
Use it within `closeable-map*` or `with-closeable*` to avoid partially open state when an exception is thrown on evaluating closeable forms. When `x` is a Java object, store as a object that may be closed. You may then extend the multimethod `close!` to provide a standard way to close this class of objects.
(closeable-map m)
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.
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.
(closeable-map* & body)
Avoid partially open state when an exception is thrown on evaluating
inner closeable forms wrapped by closeable
. Inner forms must
return a map.
(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).
Avoid partially open state when an exception is thrown on evaluating inner closeable forms wrapped by `closeable`. Inner forms must return a map. ``` 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).
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.
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.
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.
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.
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.
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.
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.
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.
(with-closeable* bindings & body)
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:
(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:
(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.
(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.
(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
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 ```
(with-tag tag x)
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.
(meta
(let [a {}]
^::closeable-map/fn a))
;; => nil
(meta
(let [a {}]
(with-tag ::closeable-map/fn a)))
;; => #:piotr-yuxuan.closeable-map{:fn true}
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} ```
(with-tag- x tag)
The code is the docstring:
(defmacro -with-tag
"The code is the docstring: [truncated]"
[x tag]
`(vary-meta ~x assoc ~tag true))
The code is the docstring: ``` clojure (defmacro -with-tag "The code is the docstring: [truncated]" [x tag] `(vary-meta ~x assoc ~tag true)) ```
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close