async_backoff: Retry an asynchronous function with exponential backoff

View source: R/backoff.R

async_backoffR Documentation

Retry an asynchronous function with exponential backoff

Description

Keeps trying until the function's deferred value resolves without error, or times tries have been performed, or time_limit seconds have passed since the start of the first try.

Usage

async_backoff(
  task,
  ...,
  .args = list(),
  times = Inf,
  time_limit = Inf,
  custom_backoff = NULL,
  on_progress = NULL,
  progress_data = NULL
)

Arguments

task

An asynchronous function.

...

Arguments to pass to task.

.args

More arguments to pass to task.

times

Maximum number of tries.

time_limit

Maximum number of seconds to try.

custom_backoff

If not NULL then a callback function to calculate waiting time, after the ithe try. i is passed as an argument. If NULL, then the default is used, which is a uniform random number of seconds between 1 and 2^i.

on_progress

Callback function for a progress bar. Retries are announced here, if not NULL. on_progress is called with two arguments. The first is a named list with entries:

  • event: string that is either "retry" or "givenup",

  • tries: number of tried so far,

  • spent: number of seconds spent trying so far,

  • error: the error object for the last failure,

  • retry_in: number of seconds before the next try. The second argument is progress_data.

progress_data

async_backoff() will pass this object to on_progress as the second argument.

Details

Note that all unnamed arguments are passed to task.

Value

Deferred value for the operation with retries.

See Also

Other async control flow: async_reflect(), async_retry(), async_retryable(), async_sequence(), async_try_each(), async_until(), async_whilst()

Examples


afun <- function() {
  wait_100_ms <- function(i) 0.1
  async_backoff(
    function() if (runif(1) < 0.8) stop("nope") else "yes!",
    times = 5,
    custom_backoff = wait_100_ms
  )
}

# There is a slight chance that it fails
tryCatch(synchronise(afun()), error = function(e) e)


gaborcsardi/async documentation built on Nov. 20, 2024, 4:51 p.m.