Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callback functions for instrumentation #231

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/taoensso/carmine.clj
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,16 @@
(wcar {} (echo 1) (println (with-replies (ping))) (echo 2))
(wcar {} (echo 1) (println (with-replies :as-pipeline (ping))) (echo 2))
(def setupf (fn [_] (println "boo")))
(wcar {:spec {:conn-setup-fn setupf}}))
(wcar {:spec {:conn-setup-fn setupf}})

(def conn-close-fn (fn [{spec :spec}] (println spec)))
(wcar {:spec {:conn-close-fn conn-close-fn}})

(def conn-open-fn (fn [{spec :spec}] (println spec)))
(wcar {:spec {:conn-open-fn conn-open-fn}})

(def conn-error-fn (fn [{spec :spec ex :ex}] (println ex)))
(wcar {:spec {:conn-error-fn conn-error-fn}}))

(comment
(wcar {}
Expand Down
16 changes: 14 additions & 2 deletions src/taoensso/carmine/connections.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@
(protocol/with-context this
(protocol/with-replies
(taoensso.carmine/ping)))
(catch Exception _)))))
(close-conn [_] (.close socket)))
(catch Exception ex
(if-let [conn-error-fn (:conn-error-fn spec)]
(conn-error-fn {:spec spec :ex ex})))))))
(close-conn [_]
(if-let [conn-close-fn (:conn-close-fn spec)]
(conn-close-fn {:spec spec}))
(.close socket)))

(defprotocol IConnectionPool
(get-conn [this spec])
Expand Down Expand Up @@ -100,6 +105,9 @@

db (when (and db (not (zero? db))) db)]

(if-let [conn-open-fn (:conn-open-fn spec)]
(conn-open-fn {:spec spec}))

(when (or password db conn-setup-fn)
(protocol/with-context conn
(protocol/with-replies ; Discard replies
Expand Down Expand Up @@ -150,12 +158,16 @@
:test-while-idle? (.setTestWhileIdle pool v) ; false
:time-between-eviction-runs-ms (.setTimeBetweenEvictionRunsMillis pool v) ; -1

:pool-create-fn ()

(throw (ex-info (str "Unknown pool option: " k) {:option k})))
pool)

(def conn-pool
(enc/memoize_
(fn [pool-opts]
(if-let [pool-create-fn (:pool-create-fn pool-opts)]
(pool-create-fn {:pool pool-opts}))
(cond
(identical? pool-opts :none) (->NonPooledConnectionPool)
;; Pass through pre-made pools (note that test reflects):
Expand Down
27 changes: 26 additions & 1 deletion test/taoensso/carmine/tests/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,32 @@
(remove-ns 'taoensso.carmine.tests.main)
(test/run-tests 'taoensso.carmine.tests.main))

(defmacro wcar* [& body] `(car/wcar {:pool {} :spec {}} ~@body))
(defn conn-setup [_]
(car/client-setname "the-name"))

(defn conn-error [{spec :spec ex :ex}]
(let [tags {:redis-host (:host spec)
:redis-port (:port spec)
:redis-db (:db spec)}]))

(defn conn-close [{spec :spec}]
(let [tags {:redis-host (:host spec)
:redis-port (:port spec)
:redis-db (:db spec)}]))

(defn conn-open [{spec :spec}]
(let [tags {:redis-host (:host spec)
:redis-port (:port spec)
:redis-db (:db spec)}]))

(defn pool-create [{pool :pool}])

(defmacro wcar* [& body] `(car/wcar {:pool {:pool-create-fn pool-create}
:spec {:conn-setup-fn conn-setup
:conn-error-fn conn-error
:conn-close-fn conn-close
:conn-open-fn conn-open}} ~@body))

(def tkey (partial car/key :carmine :temp :test))
(defn clean-up-tkeys! [] (when-let [ks (seq (wcar* (car/keys (tkey :*))))]
(wcar* (apply car/del ks))))
Expand Down