FabienHenon/jsonapi-http-retry/1.0.1

Formerly calions-app/jsonapi-http-retry. Search finds this package by either name.

Http.Retry

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

List (Policy RequestError)
-> List FailureCondition
-> Task Never (RemoteData RequestError data)
-> Task Never (RemoteData RequestError data)

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

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

Policies

type alias Policy 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

  • Int timestamp of when we first started originalTask, in milliseconds
  • Policy x the current policy; destructure to obtain the function to call
  • x last error from attempting originalTask Refer to maxRetries source code for a simple example.
maxRetries : 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 : 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 : 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

{ interval : Float
, maxInterval : 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

Failure conditions

type alias FailureCondition =
FailureCondition

FailureCondition contains a function that filter request errors

onStatus : Int -> FailureCondition

To use with with function. Filters errors to be retried by retrying errors based on the status code

onUnauthenticatedStatus : FailureCondition

To use with with function. Filters errors to be retried by retrying errors base on the 401 status code

onUnauthorizedStatus : FailureCondition

To use with with function. Filters errors to be retried by retrying errors base on the 403 status code

onNetworkError : FailureCondition

To use with with function. Filters errors to be retried by retrying all network errors

onTimeout : FailureCondition

To use with with function. Filters errors to be retried by retrying all timeout errors

onAllFailures : FailureCondition

To use with with function. Filters errors to be retried by retrying all errors