Skip to content

Instantly share code, notes, and snippets.

@dlebrero
Created June 20, 2017 18:41
Show Gist options
  • Save dlebrero/529b9bfd4c5a7bd4a56fd42a320f30d6 to your computer and use it in GitHub Desktop.
Save dlebrero/529b9bfd4c5a7bd4a56fd42a320f30d6 to your computer and use it in GitHub Desktop.
(import 'java.text.SimpleDateFormat)
(let [lock (Object.)
date-format (SimpleDateFormat. "HH:mm:ss")]
(defn log [& msgs]
(locking lock
(apply println (str (.format date-format (java.util.Date.))) "-" (.getName (Thread/currentThread)) "-" msgs))))
;; This is just for logging the connection number
(def connections-created (atom 0))
(defn create-new-hbase-connection []
;;some side effecting code that creates a new socket
(Thread/sleep 1000)
(let [new-connection-number (swap! connections-created inc)]
(log "Connection created!" new-connection-number)
new-connection-number))
;;
(def global-hbase-connection (atom (delay (create-new-hbase-connection))))
(defn refresh-connection! []
(swap! global-hbase-connection
(fn [old-connection]
;; should close old connection
(log "Closing" @old-connection)
(delay (create-new-hbase-connection)))))
(defn run-with-hbase [f]
(try
(f @@global-hbase-connection)
(catch Exception _
(refresh-connection!)
(log "Retrying")
;; this retry logic is again a simplification
(run-with-hbase f))))
(dotimes [_ 4]
(future (run-with-hbase
(let [errored (atom false)]
(fn [connection]
(if @errored
(log "Using" connection)
(do
(log "Not happy with" connection)
(reset! errored true)
(throw (RuntimeException.)))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment