Created
December 5, 2016 09:55
-
-
Save codecitizen/c8911c4bf532a70830354e03dbd27a74 to your computer and use it in GitHub Desktop.
Exponential Backoff in Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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