GithubHelp home page GithubHelp logo

backoff's Introduction

Clojars Project

Backoff

This library mimics the go package github.com/cenkalti/backoff/v4.

Usage

Checkout more use case here.

Backoff

(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

(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<!! f (b/make-exponential-backoff) {:notify notify}))
i: 1 , backoff: 595
i: 2 , backoff: 1108
i: 3 , backoff: 2303
;; => 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<!! f (b/with-max-retries 100 5) {:notify notify}))
i: 1 , backoff: 100
i: 2 , backoff: 100
i: 3 , backoff: 100
i: 4 , backoff: 100
i: 5 , backoff: 100
;; => throws error

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.