Skip to content

Instantly share code, notes, and snippets.

@codecitizen
Created December 5, 2016 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecitizen/c8911c4bf532a70830354e03dbd27a74 to your computer and use it in GitHub Desktop.
Save codecitizen/c8911c4bf532a70830354e03dbd27a74 to your computer and use it in GitHub Desktop.
Exponential Backoff in Clojure
(def time-slot-ms 52)
(def truncate 10)
(defn ** [b e]
(reduce * (repeat e b)))
(defn with-exp-backoff [action!]
(loop [c 0]
(let [slot (* time-slot-ms (dec (** 2 c)))]
(println "Sleeping for " slot "ms.")
(Thread/sleep slot)
(when (not (action!))
(recur (if (>= c truncate) c (inc c)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment