Ark.Retry (ark v0.12.0)

Copy Markdown View Source

Generic retry utilities.

Summary

Functions

Executes the given function multiple times until it returns :ok or {:ok, _} or the :max_attempts option is reached. When there is no more attempts, returns the last return value of the function.

Functions

delay_stream(opts)

retry(fun, opts \\ [])

Executes the given function multiple times until it returns :ok or {:ok, _} or the :max_attempts option is reached. When there is no more attempts, returns the last return value of the function.

Options

  • :max_attempts - the maximum number of attempts to execute the function. That is the maximum number of times the function is called, not retried. Giving 1 will call the function once and return its result. Defaults to 2.
  • :delay - the delay between the first attempt and the second attempt, in milliseconds. Defaults to 0. If no other option is given, the same delay will be used between the 2nd and 3rd call, and so on.
  • :add - the number of milliseconds to add to the delay between each attempt. Defaults to 0.
  • :exp - the exponent to multiply the delay between each attempt. Defaults to 1. Accepts an integer or float, but not that any float result after the multiplication will be truncated as we are using Process.sleep/1. When the previous delay is lower than 1, the multiplication is done from 1 instead.
  • :cap - the maximum number of milliseconds to wait between each attempt. If the delay is greater than the cap, the cap will be used instead. Defaults to :infinity, which means no cap. For instance, an exponential delay will quickly reach very high values, so you may want to cap it to a reasonable time, for instance 5000 milliseconds. Modifiers are applied in option order, so :cap should usually be passed last.

Delay modifiers are applied in the order they are given.

delay: 1000, add: 5, exp: 2 # => 1000, 2010, 4030, ...
delay: 1000, exp: 2, add: 5 # => 1000, 2005, 4015, ...

Example

The following function call will wait 1000, 2010, 4030 and 8070 in between attempts:

retry(fn -> call_api("/get/stuff") end, max_attempts: 5, delay: 1000, add: 5, exp: 2)