choonkeat / elm-retry / Retry

Add retries to a task, based on a list of retry policies, until any one of the policies fail too.


type Policy x
    = Policy (Basics.Int -> Policy x -> x -> Task x (Policy x))

A Policy is attached with a function that will return another Policy as a Task value.

The arguments of the function are

Refer to maxRetries source code for a simple example.

with : List (Policy x) -> Task x a -> Task x a

Given a list of error handling Policy we can make our originalTask retry on failure until any one of the Policy fails.

originalTask
    |> Retry.with
        [ Retry.maxDuration 7000
        , Retry.exponentialBackoff { interval = 500, maxInterval = 3000 }
        ]
    |> Task.attempt DidOriginalTask

Common policies

maxRetries : Basics.Int -> Policy x

Stop retrying originalTask after a number of retries.

Retry.with [ Retry.maxRetries 20 ] originalTask
    |> Task.attempt DidOriginalTask

NOTE: The code above does NOT sleep between retries; best to combine with constantInterval or exponentialBackoff

maxDuration : Basics.Int -> Policy x

Stop retrying originalTask after some number of milliseconds.

Retry.with [ Retry.maxDuration 7000 ] originalTask
    |> Task.attempt DidOriginalTask

NOTE: The code above does NOT sleep between retries; best to combine with constantInterval or exponentialBackoff

constantInterval : Basics.Float -> Policy x

Sleep for the same number of milliseconds before every retry.

Retry.with [ Retry.constantInterval 1000 ] originalTask
    |> Task.attempt DidOriginalTask

NOTE: The code above will keep retrying originalTask; best to combine with maxRetries or maxDuration

exponentialBackoff : { interval : Basics.Float, maxInterval : Basics.Float } -> Policy x

Sleep for an increasing number of milliseconds before every retry. Backoff algorithim is based off https://github.com/cenkalti/backoff

Retry.with [ Retry.exponentialBackoff { interval = 500, maxInterval = 3000 } ] originalTask
    |> Task.attempt DidOriginalTask

NOTE: The code above will keep retrying originalTask; best to combine with maxRetries or maxDuration