Skip to content

Instantly share code, notes, and snippets.

@TimoKramer
Last active December 16, 2020 18:48
Show Gist options
  • Save TimoKramer/fb9ba9548a3d1badf001f7326ac5a348 to your computer and use it in GitHub Desktop.
Save TimoKramer/fb9ba9548a3d1badf001f7326ac5a348 to your computer and use it in GitHub Desktop.
Fibonacci Backoff
(defn fib [a b]
(lazy-seq (cons a (fib b (+ a b)))))
(defn fib-backoff [exec-fn test-fn]
(loop [retries (take 10 (fib 1 2))]
(let [result (exec-fn)]
(if (test-fn result)
result
(when-let [sleep-ms (first retries)]
(log/debug "Retrying with remaining retries: " retries)
(log/debug "Result was: " result)
(Thread/sleep (* 1000 sleep-ms))
(recur (rest retries)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment