elixir_retry v0.3.0 Retry
Provides a convenient interface to retrying behavior. All durations a specified in milliseconds.
Examples
use Retry
import Stream
retry with: exp_backoff |> randomize |> cap(1000) |> expiry(10000) do
# interact with external service
end
retry with: lin_backoff(10, @fibonacci) |> cap(1000) |> 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:
.
Summary
Macros
Retry block of code with a exponential backoff delay between attempts
Retry block of code a maximum number of times with a fixed delay between attempts
Macros
Retry block of code with a exponential backoff delay between attempts.
Example
backoff 1000, delay_cap: 100 do
# interact the external service
end
Runs the block repeated until it succeeds or 1 second elapses with an
exponentially increasing delay between attempts. Execution is deemed a failure
if the block returns {:error, _}
or raises a runtime error.
The delay_cap
is optional. If specified it will be the max duration of any
delay. In the example this is saying never delay more than 100ms between
attempts. Omitting delay_cap
is the same as setting it to :infinity
.
Retry block of code a maximum number of times with a fixed delay between attempts.
Example
retry 5 in 500 do
# interact with external service
end
Runs the block up to 5 times with a half second sleep between each
attempt. Execution is deemed a failure if the block returns {:error, _}
or
raises a runtime error.