retry v0.10.0 Retry View Source
Provides a convenient interface to retrying behavior. All durations are specified in milliseconds.
Examples
use Retry
import Stream
retry with: exp_backoff |> randomize |> cap(1_000) |> expiry(10_000) do
# interact with external service
end
retry with: lin_backoff(10, @fibonacci) |> cap(1_000) |> take(10) do
# interact with external service
end
retry with: cycle([500]) |> take(10) do
# interact with external service
end
The first retry will exponentially increase the delay, fudging each delay up to 10%, until the delay reaches 1 second and then give up after 10 seconds.
The second retry will linearly increase the retry from 10ms following a Fibonacci pattern giving up after 10 attempts.
The third example shows how we can produce a delay stream using standard
Stream functionality. Any stream of integers may be used as the value of
with:.
Link to this section Summary
Functions
Retry a block of code delaying between each attempt the duration specified by
the next item in the with delay stream
Retry a block of code until halt is emitted delaying between each attempt
the duration specified by the next item in the with delay stream
Wait for a block of code to be truthy delaying between each attempt the duration specified by the next item in the delay stream
Link to this section Functions
Retry a block of code delaying between each attempt the duration specified by
the next item in the with delay stream.
If the block returns any of the atoms specified in atoms, a retry will be attempted.
Other atoms or atom-result tuples will not be retried. If atoms is not specified,
it defaults to [:error].
Similary, if the block raises any of the exceptions specified in rescue_only, a retry
will be attempted. Other exceptions will not be retried. If rescue_only is
not specified, it defaults to [RuntimeError].
The after block evaluates only when the do block returns a valid value before timeout.
On the other hand, the else block evaluates only when the do block remains erroneous after timeout.
Example
use Retry
retry with: exp_backoff |> cap(1_000) |> expiry(1_000), rescue_only: [CustomError] do
# interact with external service
after
result -> result
else
error -> error
end
Retry a block of code until halt is emitted delaying between each attempt
the duration specified by the next item in the with delay stream.
The return value for block is expected to be {:cont, result}, return
{:halt, result} to end the retry early.
Example
retry_while with: lin_backoff(500, 1) |> take(5) do
call_service
|> case do
result = %{"errors" => true} -> {:cont, result}
result -> {:halt, result}
end
end
Wait for a block of code to be truthy delaying between each attempt the duration specified by the next item in the delay stream.
The after block evaluates only when the do block returns a truthy value.
On the other hand, the else block evaluates only when the do block remains falsy after timeout.
Example
wait lin_backoff(500, 1) |> take(5) do
we_there_yet?
after
_ ->
{:ok, "We have arrived!"}
else
_ ->
{:error, "We're still on our way :("}
end