Bunch v0.1.2 Bunch.Retry View Source
A bunch of helpers for handling scenarios when some actions should be repeated until it succeeds.
Link to this section Summary
Functions
Calls fun
function until arbiter
function decides to stop
Link to this section Types
Link to this type
retry_option_t()
View Source
retry_option_t() :: {:times, non_neg_integer()} | {:duration, milliseconds :: pos_integer()} | {:delay, milliseconds :: pos_integer()}
Possible options for retry/3
.
Link to this section Functions
Link to this function
retry(fun, arbiter, options \\ [])
View Source
retry( fun :: (() -> res), arbiter :: (res -> stop? :: boolean()), options :: [retry_option_t()] ) :: res when res: any()
Calls fun
function until arbiter
function decides to stop.
Possible options are:
- times - limits amount of retries (first evaluation is not considered a retry)
- duration - limits total time of execution of this function, but breaks only before subsequent retry
- delay - introduces delay (
:timer.sleep/1
) before each retry
Examples
iex> {:ok, pid} = Agent.start_link(fn -> 0 end)
iex> Bunch.Retry.retry(fn -> Agent.get_and_update(pid, &{&1, &1+1}) end, & &1 > 3)
4
iex> {:ok, pid} = Agent.start_link(fn -> 0 end)
iex> Bunch.Retry.retry(fn -> Agent.get_and_update(pid, &{&1, &1+1}) end, & &1 > 3, times: 10)
4
iex> {:ok, pid} = Agent.start_link(fn -> 0 end)
iex> Bunch.Retry.retry(fn -> Agent.get_and_update(pid, &{&1, &1+1}) end, & &1 > 3, times: 2)
2
iex> {:ok, pid} = Agent.start_link(fn -> 0 end)
iex> Bunch.Retry.retry(
...> fn -> :timer.sleep(50); Agent.get_and_update(pid, &{&1, &1+1}) end,
...> & &1 > 3,
...> duration: 80
...> )
1
iex> {:ok, pid} = Agent.start_link(fn -> 0 end)
iex> Bunch.Retry.retry(
...> fn -> :timer.sleep(30); Agent.get_and_update(pid, &{&1, &1+1}) end,
...> & &1 > 3,
...> duration: 80, delay: 20
...> )
1