An open API service indexing awesome lists of open source software.

https://github.com/lotuc/backoff

Backoff & retries for Clojure/Script
https://github.com/lotuc/backoff

backoff clojure clojurescript retry-library

Last synced: 4 months ago
JSON representation

Backoff & retries for Clojure/Script

Awesome Lists containing this project

README

        

[![Clojars Project](https://img.shields.io/clojars/v/org.lotuc/backoff.svg)](https://clojars.org/org.lotuc/backoff)

# Backoff

This library mimics the go package
[github.com/cenkalti/backoff/v4](https://github.com/cenkalti/backoff).

## Usage

Checkout more use case [here](./test/lotuc/backoff_test.cljc).

### Backoff

```clojure
(require '[lotuc.backoff :as b])

;; nil represents *stop* backoff
(-> nil b/backoff) ; => nil

;; integer/long as constant backoff
(-> 2 b/backoff) ; => 2
(-> 2 b/nxt b/backoff) ; => 2
;; negative value *stops* backoff
(-> -1 b/backoff) ; => nil

;; integer/long sequence as backoff
(-> (range 1 5) b/backoff) ; => 1
(-> (range 1 5) b/nxt b/backoff) ; => 2
(-> '() b/backoff) ; => nil

;; exponential backoff
(def b0 (b/make-exponential-backoff))
[(-> b0 b/backoff) (-> b0 b/nxt b/backoff) (-> b0 b/nxt b/nxt b/backoff)] ; => [503 1099 1128]
```

### Retries

```clojure
(require '[lotuc.backoff :as b])

(let [i (atom 0)
f (fn [] (if (< (Math/random) 0.5) @i
(do (swap! i inc)
(throw (ex-info "error" {:i @i})))))
notify (fn [err b] (println "i:" (:i (ex-data err)) ", backoff:" (b/backoff b)))]
(b/retry 3

;; setup max-retries with `with-max-retries`
(let [i (atom 0)
f (fn [] (do (swap! i inc)
(throw (ex-info "error" {:i @i}))))
notify (fn [err b] (println "i:" (:i (ex-data err)) ", backoff:" (b/backoff b)))]
(b/retry throws error
```