FUNCTIONS

retry

The retry function works just like try from Try::Tiny, except that if an exception is thrown, the block may be executed again, depending on the retry_if and delay blocks.

If one or more retry_if blocks are provided, as long as any of them evaluate to true, a retry will be attempted unless the result of the delay block indicates otherwise. If none of them evaluate to true, no retry will be attempted and the delay block will not be called.

If no delay block is provided, the default will be 10 tries with a random delay up to 100 milliseconds with an exponential backoff. (See "delay_exp".) This has an expected cumulative delay of around 25 seconds if all retries fail.

retry_if

retry { ... }
retry_if { /^could not connect/ }
catch { ... };

A retry_if block controls whether a retry should be attempted after an exception (assuming there are any retry attempts remaining).

The block is passed the cumulative number of attempts as an argument. The exception caught is provided in $_, just as with catch. It should return a true value if a retry should be attempted.

Multiple retry_if blocks may be provided. Only one needs to evaluate to true to enable a retry.

Using a retry_if block based on the retry count is an alternate way to allow fewer (but not greater) tries than the default delay function, but with the default exponential backoff behavior. These are effectively equivalent:

delay_exp

This function is an exponential-backoff delay-function generator. The delay between attempts is randomly selected between 0 and an upper bound. The upper bound doubles after each failure.

It requires a code block as an argument. The block will be evaluated in list context and must return two elements. The first element is the number of tries allowed. The second element is the starting upper bound in microseconds.

Given number of tries N and upper bound U, the expected cumulative delay time if all attempts fail is 0.5 * U * ( 2^(N-1) - 1 ).

SEE ALSO

There are other retry modules on CPAN, but none of them worked seamlessly with Try::Tiny.