Generic retry utilities.
Summary
Functions
Returns an infinite stream of the delays retry/2 waits between attempts.
Takes the same options as retry/2 and applies the :delay, :add, :exp
and :cap modifiers, ignoring :max_attempts. The first element is the
initial :delay, and each following element applies the modifiers to the
previous one. Useful to inspect or test a backoff schedule on its own.
iex> Ark.Retry.delay_stream(delay: 1000, add: 5, exp: 2) |> Enum.take(4)
[1000, 2010, 4030, 8070]
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. Giving1will call the function once and return its result. Defaults to2.:delay- the delay between the first attempt and the second attempt, in milliseconds. Defaults to0. 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 to0.:exp- the exponent to multiply the delay between each attempt. Defaults to1. Accepts an integer or float, but not that any float result after the multiplication will be truncated as we are usingProcess.sleep/1. When the previous delay is lower than1, the multiplication is done from1instead.: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:capshould 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)