Giter Club home page Giter Club logo

diehard's Introduction

diehard

Build Status Clojars license Donate

Clojure library to provide safety guard to your application. Some of the functionality is wrapper over Failsafe.

Note that from 0.7 diehard uses Clojure 1.9 and spec.alpha for configuration validation. Clojure 1.8 users could stick with diehard 0.6.0.

Usage

A quick example for diehard usage.

Retry block

A retry block will re-execute inner forms when retry criteria matches.

(require '[diehard.core :as dh])
(dh/with-retry {:retry-on TimeoutException
                :max-retries 3}
  (fetch-data-from-the-moon))

Circuit breaker

A circuit breaker will track the execution of inner block and skip execution if the open condition triggered.

(require '[diehard.core :as dh])

(defcircuitbreaker my-cb {:failure-threshold-ratio [8 10]
                          :delay-ms 1000})

(dh/with-circuit-breaker my-cb
  (fetch-data-from-the-moon))

Rate limiter

A rate limiter protects your code block to run limited times per second. It will block or throw exception depends on your configuration.

(require '[diehard.core :as dh])

(defratelimiter my-rl {:rate 100})

(dh/with-rate-limiter my-rl
  (send-people-to-the-moon))

Bulkhead

Bulkhead allows you to limit concurrent execution on a code block.

(require '[diehard.core :as dh])

;; at most 10 threads can run the code block concurrently
(defbulkhead my-bh {:concurrency 10})

(dh/with-bulkhead my-bh
  (send-people-to-the-moon))

Examples

Retry block

(dh/with-retry {:retry-on          Exception
                :max-retries       3
                :on-retry          (fn [val ex] (prn "retrying..."))
                :on-failure        (fn [_ _] (prn "failed..."))
                :on-failed-attempt (fn [_ _] (prn "failed attempt"))
                :on-success        (fn [_] (prn "did it! success!"))}
               (throw (ex-info "not good" {:not "good"})))

output:

"failed attempt"
"retrying..."
"failed attempt"
"retrying..."
"failed attempt"
"retrying..."
"failed attempt"
"failed..."
Execution error (ExceptionInfo) at main.user$eval27430$reify__27441/get (form-init6791465293873302710.clj:7).
not good

Docs

More options can be found in the documentation from cljdoc.

License

Copyright © 2016-2019 Ning Sun

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

Donation

I'm now accepting donation on liberapay, if you find my work helpful and want to keep it going.

diehard's People

Contributors

sunng87 avatar codahale avatar notnoopci avatar mjhanninen avatar neilprosser avatar buzzdan avatar dhruvbhatia avatar lewang avatar vendethiel avatar

Watchers

James Cloos avatar

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.