Liking cljdoc? Tell your friends :D

diehard-async.core


*elapsed-time-ms*clj

Available in retry block. Contextual value represents time elasped since first attempt

Available in retry block. Contextual value represents time elasped since first attempt
raw docstring

*executions*clj

Available in retry block. Contextual value represents execution times

Available in retry block. Contextual value represents execution times
raw docstring

*start-time-ms*clj

Available in retry block. Contextual value represents first attempt time

Available in retry block. Contextual value represents first attempt time
raw docstring

defbulkheadcljmacro

(defbulkhead name opts)

Create bulkhead config from option map.

  • concurrency the max number of concurrent executions
Create bulkhead config from option map.
* `concurrency` the max number of concurrent executions
raw docstring

defcircuitbreakercljmacro

(defcircuitbreaker name opts)

Define a circuit breaker with option.

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 failures sets the number of failures that must occur before the circuit opens (count-based).
  • :failure-threshold-ratio [failures executions] sets the failure ratio that must occur before the circuit opens (count-based). e.g. [4 10] means 4 out of the last 10 executions must fail to open the circuit.
  • :failure-threshold-ratio-in-period [failures min-executions period-ms] sets the number of failures that must occur within a time period to open the circuit (time-based). Must also exceed min-executions within the time period to open the circuit.
  • :failure-rate-threshold-in-period [failure-pct min-executions period-ms] sets the percent of executions that must fail (1-100) within the time period to open the circuit (time-based). Must also exceed min-executions within the time period to open the circuit.
  • :success-threshold successes sets the number of successful executions that must occur when in :half-open to return to a :closed state.
  • :success-threshold-ratio [successes executions] sets the success ratio that must occur when in :half-open to return to a :closed state. e.g. [6 10] means 6 out of the last 10 executions must succeed to close the circuit.
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
Define a circuit breaker with option.
#### 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 failures` sets the number of failures that must occur
  before the circuit opens (count-based).
* `:failure-threshold-ratio [failures executions]` sets the failure ratio that
  must occur before the circuit opens (count-based). e.g. [4 10] means 4 out of
  the last 10 executions must fail to open the circuit.
* `:failure-threshold-ratio-in-period [failures min-executions period-ms]` sets
  the number of failures that must occur within a time period to open the
  circuit (time-based). Must also exceed `min-executions` within the time
  period to open the circuit.
* `:failure-rate-threshold-in-period [failure-pct min-executions period-ms]`
  sets the percent of executions that must fail (1-100) within the time period
  to open the circuit (time-based). Must also exceed `min-executions` within
  the time period to open the circuit.
* `:success-threshold successes` sets the number of successful executions that
  must occur when in `:half-open` to return to a `:closed` state.
* `:success-threshold-ratio [successes executions]` sets the success ratio that
  must occur when in `:half-open` to return to a `:closed` state. e.g. [6 10]
  means 6 out of the last 10 executions must succeed to close the circuit.

##### 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`
raw docstring

defratelimitercljmacro

(defratelimiter name opts)

Create a rate limiter with options.

  • :rate execution permits per second.
  • :max-cached-tokens the max size of permits we can cache when idle
Create a rate limiter with options.

* `:rate` execution permits per second.
* `:max-cached-tokens` the max size of permits we can cache when idle
raw docstring

defretrypolicycljmacro

(defretrypolicy name opts)

Predefined retry policy.

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 truthy
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 truthy
  • :max-retries abort retry when max attempts reached
  • :max-duration-ms 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 (min (* initial-delay-ms (expt 2 (- n 1))) 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
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 exiting retry block with failure (matches retry criteria)
  • :on-success accepts a function which takes result as arguments, called when exiting retry block with success (mismatches retry criteria)
  • :on-retry accepts a function which takes result, exception as arguments, called when a retry attempted.
  • :on-retries-exceeded accepts a function which takes result, exception as arguments, called when max retries or max duration have been exceeded.
Use pre-defined policy

You can put together all those retry policies in a defretrypolicy. And use :policy option in option map.

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

(diehard/with-retry {:policy policy}
  ;; your code here
  )
Predefined retry policy.
#### 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 truthy

##### 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 truthy
* `:max-retries` abort retry when max attempts reached
* `:max-duration-ms` 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
  **n**th retry will be `(min (* initial-delay-ms (expt 2 (- n 1))) 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

##### 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 exiting `retry` block with
  failure (matches retry criteria)
* `:on-success` accepts a function which takes `result` as arguments,
  called when exiting `retry` block with success (mismatches retry
  criteria)
* `:on-retry` accepts a function which takes `result`, `exception` as
  arguments, called when a retry attempted.
* `:on-retries-exceeded` accepts a function which takes `result`,
  `exception` as arguments, called when max retries or max duration have
  been exceeded.

##### Use pre-defined policy

You can put together all those retry policies in a `defretrypolicy`.
And use `:policy` option in option map.

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

(diehard/with-retry {:policy policy}
  ;; your code here
  )
```
raw docstring

with-bulkheadcljmacro

(with-bulkhead opts & body)

Bulkhead block. Only given number of executions is allowed to be executed in parallel.

;; create a bulkhead that limit concurrency to 3
(defbulkhead mybh {:concurrency 3})

(with-bulkhead {:bulkhead mybh}
  ;; your task here
  )

By default it will wait until there is permits available for execution.

You can add max-wait-ms option for change this behavior. If no permits is available when max-wait-ms exceeded, an ex-info will be thrown with ex-data as {:bulkhead true :max-wait-ms wait-timeout}

(try
  (with-bulkhead {:bulkhead mybh
                  :max-wait-ms 1000}
    ;; your task here
    )
  (catch Exception e
    (is (::bulkhead (ex-data e)))))
Bulkhead block. Only given number of executions is allowed to be executed in parallel.

```clojure
;; create a bulkhead that limit concurrency to 3
(defbulkhead mybh {:concurrency 3})

(with-bulkhead {:bulkhead mybh}
  ;; your task here
  )
```

By default it will wait until there is permits available for execution.

You can add `max-wait-ms` option for change this behavior. If no permits is available
when `max-wait-ms` exceeded, an `ex-info` will be thrown with `ex-data` as `{:bulkhead true :max-wait-ms wait-timeout}`

```clojure
(try
  (with-bulkhead {:bulkhead mybh
                  :max-wait-ms 1000}
    ;; your task here
    )
  (catch Exception e
    (is (::bulkhead (ex-data e)))))
```
raw docstring

with-circuit-breakercljmacro

(with-circuit-breaker cb & body)

Circuit breaker protected block.

(require '[diehard-async.core :as dha])

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

(dha/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.

The block will throw CircuitBreakerOpenException when the circuit breaker is open and skip execution of inner forms. Otherwise it will return the value or throw the exception raised from inner.

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

Circuit breaker protected block.

```clj
(require '[diehard-async.core :as dha])

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

(dha/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.

The block will throw `CircuitBreakerOpenException` when the circuit breaker
is open and skip execution of inner forms. Otherwise it will return the value
or throw the exception raised from inner.

You can always check circuit breaker state with
`dha.circuitbreaker/state`.
raw docstring

with-rate-limitercljmacro

(with-rate-limiter opts & body)

Rate Limiter protected block. Code execution in this block is throttled to given rate. Use defratelimiter to define a ratelimiter and use it as option:

;; create a rate limiter for 100 executions for second
(defratelimiter myfl {:rate 100})

(with-rate-limiter {:ratelimiter myfl}
  ;; your task here
  )

By default it will wait forever until there is permits available. You can also specify a max-wait-ms to wait for a given time. If there's no permits in this period, this block will throw a Clojure ex-info, with ex-data as


(try
  (with-rate-limiter {:ratelimiter myfl
                      :max-wait-ms 1000}
    ;; your task here
    )
  (catch Exception e
    (is (:throttled (ex-data e)))))

If your execution has a greater graininess, you can customize the permits for this execution by setting :permits option.

(with-rate-limiter {:ratelimiter myfl
                    :permits (size-of-the-task)}
  ;; your task here
  )
Rate Limiter protected block. Code execution in this block is throttled
to given rate. Use `defratelimiter` to define a ratelimiter and use it as option:

```clojure
;; create a rate limiter for 100 executions for second
(defratelimiter myfl {:rate 100})

(with-rate-limiter {:ratelimiter myfl}
  ;; your task here
  )
```

By default it will wait forever until there is permits available. You can also specify a
`max-wait-ms` to wait for a given time. If there's no permits in this period, this block
will throw a Clojure `ex-info`, with `ex-data` as

```clojure

(try
  (with-rate-limiter {:ratelimiter myfl
                      :max-wait-ms 1000}
    ;; your task here
    )
  (catch Exception e
    (is (:throttled (ex-data e)))))
```

If your execution has a greater graininess, you can customize the permits for this execution
by setting `:permits` option.

```clojure
(with-rate-limiter {:ratelimiter myfl
                    :permits (size-of-the-task)}
  ;; your task here
  )
```
raw docstring

with-retrycljmacro

(with-retry opt & body)

Retry policy protected block. 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 the value or throw exception from the last execution. If :circuit-breaker is set, it will throw CircuitBreakerOpenException when the breaker becomes open.

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 truthy
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 truthy
  • :max-retries abort retry when max attempts reached
  • :max-duration-ms 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 (min (* initial-delay-ms (expt 2 (- n 1))) 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
Retry Listeners
  • :on-abort accepts a function which takes result, exception as arguments, called when retry aborted
  • :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 and exception as arguments, called when a retry attempted.
  • :on-retries-exceeded accepts a function which takes result and exception as arguments, called when retries exceeded
Use pre-defined policy

You can put together all those retry policies in a defretrypolicy. And use :policy option in option map.

(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 exiting retry block with failure (matches retry criteria)
  • :on-success accepts a function which takes result as arguments, called when exiting retry block with success (mismatches retry criteria)
  • :on-retry accepts a function which takes result as arguments, called when a retry attempted.
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 exception]
                         ;; value: value returned from last attempt
                         ;; exp: exception thrown from last attempt
                         )}
  ;; ...
  )

Circuit breaker
  • :circuit-breaker a circuit breaker created from defcircuitbreaker. It will work together with retry policy as quit criteria.
Retry policy protected block.
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 the value or throw exception from
the last execution. If `:circuit-breaker` is set, it will throw
 `CircuitBreakerOpenException` when the breaker becomes open.

#### 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 truthy

##### 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 truthy
* `:max-retries` abort retry when max attempts reached
* `:max-duration-ms` 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
  **n**th retry will be `(min (* initial-delay-ms (expt 2 (- n 1))) 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

##### Retry Listeners

* `:on-abort` accepts a function which takes `result`, `exception` as
  arguments, called when retry aborted
* `: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` and `exception` as
  arguments, called when a retry attempted.
* `:on-retries-exceeded` accepts a function which takes `result` and
  `exception` as arguments, called when retries exceeded

##### Use pre-defined policy

You can put together all those retry policies in a `defretrypolicy`.
And use `:policy` option in option map.

```clojure
(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 exiting `retry` block with
  failure (matches retry criteria)
* `:on-success` accepts a function which takes `result` as arguments,
  called when exiting `retry` block with success (mismatches retry
  criteria)
* `:on-retry` accepts a function which takes `result` as arguments,
  called when a retry attempted.

##### Fallback

* `:fallback` fallback value or handler function when retry blocks
  exists with failure.

```clojure
;; return 5 when attempts failure
(with-retry {:fallback 5}
  ;; ...
  )

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

```

##### Circuit breaker
* `:circuit-breaker` a circuit breaker created from `defcircuitbreaker`.
It will work together with retry policy as quit criteria.
raw docstring

with-timeoutcljmacro

(with-timeout opts & body)

Timeout block. This block will throw TimeoutExceededException when configured time elapsed.

Available options:

  • timeout-ms: required timeout
  • on-success: the callback when block execution succeeded
  • on-failure: the failure when block execution timed out
  • interrupt?: cancel the execution if it times out. boolean, default false
Timeout block. This block will throw TimeoutExceededException when
configured time elapsed.

Available options:
* `timeout-ms`: required timeout
* `on-success`: the callback when block execution succeeded
* `on-failure`: the failure when block execution timed out
* `interrupt?`: cancel the execution if it times out. boolean, default false
raw docstring

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close