View Source Bunch.Retry (Bunch v1.6.1)

A bunch of helpers for handling scenarios when some actions should be repeated until it succeeds.

Summary

Types

Possible options for retry/3.

Functions

Calls fun function until arbiter function decides to stop.

Types

@type retry_option_t() ::
  {:times, non_neg_integer()}
  | {:duration, milliseconds :: pos_integer()}
  | {:delay, milliseconds :: pos_integer()}

Possible options for retry/3.

Functions

Link to this function

retry(fun, arbiter, options \\ [])

View Source
@spec 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(100); Agent.get_and_update(pid, &{&1, &1+1}) end,
...> & &1 > 3,
...> duration: 150
...> )
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