/diehard

Clojure wrapper over Failsafe, retry and circuit breaker

Primary LanguageClojureEclipse Public License 1.0EPL-1.0

diehard

Build Status Clojars license

A Clojure wrapper over Failsafe

Usage

Retry block

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

(diehard/with-retry {:retry-on IOException}
  ;; your code here
  )

If the return value of or exception thrown from the code block matches the criteria of your retry policy, the code block will be executed again, until it mismatch the retry policy or matches the abort criteria. The block will return or throw the value or exception from the last execution.

Available options

Retry criteria
  • :retry-when retry when return value is given value
  • :retry-on retry on given exception / exceptions(vector) were thrown
  • :retry-if specify a function (fn [return-value exception-thrown]), retry if the function returns true
Retry abortion criteria
  • :abort-when abort retry when return value is given value
  • :abort-on abort retry on given exception / exceptions(vector) were thrown
  • :abort-if specify a function (fn [return-value exception-thrown]), abort retry if the function returns true
  • :max-retries abort retry when max attempts reached
  • :max-duration abort retry when duration reached
Delay
  • :backoff-ms specify a vector [initial-delay-ms max-delay-ms multiplier] to control the delay between each retry, the delay for nth retry will be (max (* initial-delay-ms n) max-delay-ms)
  • :delay-ms use constant delay between each retry
  • :jitter-factor random factor for each delay
  • :jitter-ms random time (-jitter-ms, jitter-ms) adds to each delay
Use pre-defined policy

You can put together all those retry policies in a defretrypolicy.

(diehard/defretrypolicy policy
  {:max-retries 5
   :backoff-ms [1000 10000]})

(diehard/with-retry {:policy policy}
  ;; your code here
  )
Retry Listeners
  • :on-abort accepts a function which takes result, exception as arguments, called when retry aborted
  • :on-complete accepts a function which takes result, exception as arguments, called when exiting retry block
  • :on-failed-attempt accepts a function which takes result, exception as arguments, called when execution failed (matches retry criteria)
  • :on-failure accepts a function which takes result, exception as arguments, called when existing retry block with failure (matches retry criteria)
  • :on-success accepts a function which takes result as arguments, called when existing retry block with success (mismatches retry criteria)
  • :on-retry accepts a function which takes result as arguments, called when a retry attempted.
Use predefined listeners
(diehard/deflistener listener
  {:on-retry (fn [return-value exception-thrown] (println "retried"))})

(diehard/with-retry {:policy policy :listener listener}
  ;; your code here
  )
Fallback
  • :fallback fallback value or handler function when retry blocks exists with failure.
;; return 5 when attempts failure
(with-retry {:fallback 5}
  ;; ...
  )

;; return fallback handler function result when failed
(with-retry {:fallback (fn [value exp]
                         ;; value: value returned from last attempt
                         ;; exp: exception thrown from last attempt
                         )}
  ;; ...
  )

Circuit breaker protected block

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

(diehard/defcircuitbreaker test-cb {:failure-threshold-ratio [35 50]
                                    :delay-ms 1000})

(diehard/with-circuit-breaker test-cb
  ;; your protected code here
  )

In this scenario, if the circuit breaker protected code block fails 35 times in 50 executions, as defined in :failure-threshold-ratio, the test-cb is entering into :open state. When circuit breaker is open, all execution requests will be rejected immediately.

After :delay-ms, the circuit breaker will be :half-open. At the moment, 50 execution will be allowed, to test the state to see if it's recovered. If success, the circuit breaker is back to :closed state. Otherwise, it will be :open again.

You can always check circuit breaker state with diehard.circuitbreaker/state.

Available options

There options are available when creating circuit breaker in defcircuitbreaker.

Failure criteria

All the three fail options share same meaning with similar option in retry block.

  • :fail-if
  • :fail-on
  • :fail-when
  • :timeout-ms while give all you code a timeout is best practice in application level, circuit breaker also provides a timeout for marking a long running block as failure
Delay and threshold
  • :delay-ms required. the delay for :open circuit breaker to turn into :half-open.
  • :failure-threshold
  • :failure-threshold-ratio
  • :success-threshold
  • :success-threshold-ratio All these four option is to determine at what condition the circuit breaker is open.
Listeners
  • :on-open a function to be called when state goes :open
  • :on-close a function to be called when state goes :closed
  • :on-half-open a function to be called when state goes :half-open

Clojure API docs

The API is pretty simple but you can still find API docs here.

License

Copyright © 2016 Ning Sun

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