since import * as M from 'fp-ts/lib/Monoid' ; import * as O from 'fp-ts/lib/Option' ; import { getJoinSemigroup } from 'fp-ts/lib/Semigroup' ; import { ordNumber } from 'fp-ts/lib/Ord' ; import { pipe } from 'fp-ts/lib/pipeable' ; since export interface RetryStatus { /** Iteration number, where `0` is the first try */ iterNumber : number ; /** Delay incurred so far from retries */ cumulativeDelay : number ; /** Latest attempt's delay. Will always be `none` on first run. */ previousDelay : O . Option < number > ; } since export interface RetryPolicy { ( status ) : O . Option < number > ; } example since export const Monoid = M . getFunctionMonoid ( O . getApplyMonoid ( { concat : getJoinSemigroup ( ordNumber ) . concat , empty : 0 } ) ) < RetryStatus > ( ) ; since export function limitRetries ( i ) { return ( status ) => ( status . iterNumber >= i ? O . none : O . some ( 0 ) ) ; } since export function limitRetriesByDelay ( maxDelay , policy ) { return ( status ) => pipe ( status , policy , O . filter ( ( delay ) => delay < maxDelay ) ) ; } since export function constantDelay ( delay ) { return ( ) => O . some ( delay ) ; } since export function capDelay ( maxDelay , policy ) { return ( status ) => pipe ( status , policy , O . map ( ( delay ) => Math . min ( maxDelay , delay ) ) ) ; } since export function exponentialBackoff ( delay ) { return ( status ) => O . some ( delay * Math . pow ( 2 , status . iterNumber ) ) ; } since export const defaultRetryStatus = { iterNumber : 0 , cumulativeDelay : 0 , previousDelay : O . none } ; since export function applyPolicy ( policy , status ) { const previousDelay = policy ( status ) ; return { iterNumber : status . iterNumber + 1 , cumulativeDelay : status . cumulativeDelay + O . getOrElse ( ( ) => 0 ) ( previousDelay ) , previousDelay } ; } since deprecated export const monoidRetryPolicy = Monoid ;