Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
For Jan
;;; First we'll define a new kind of (degenerate) connection pool to encapsulate the behavior you want:
(defrecord SingletonPool [conn] ; Keeps a single connection atom
(get-conn [_ spec]
(or (when-let [c @conn] (when (conn-alive? c) c)) ; Aliveness check may not be necessary
(println "making a new conn!")
(reset! conn (make-new-connection spec))))
(release-conn [_ _] nil)
(release-conn [this _ _] nil)
(close [this] (when-let [c @conn] (reset! conn (close-conn c)))))
;;; And a helper:
(defmacro with-singleton-pool
[conn-name spec & body]
`(with-open [singleton-pool# (->SingletonPool (atom nil))]
(let [~conn-name {:pool singleton-pool# :spec ~spec}]
;;; Now we can code to [almost] the same API as if we had a real pool:
(with-singleton-pool c1 {}
(let [reply1 (wcar c1 (echo "foo"))
reply2 (wcar c1 (echo "bar"))]
(wcar c1 (set "my-key" (str reply1 reply2)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.