Execution Info

Failsafe-go can provide an Execution object containing execution related information such as the number of execution attempts, start and elapsed times, and the last result or error:

failsafe.RunWithExecution(func(e failsafe.Execution[any]) error {
  logger.Info("Connecting", "attempts", e.Attempts())
  return Connect()
}, retryPolicy)

This is useful for retrying executions that depend on results from a previous attempt:

failsafe.GetWithExecution(func(e failsafe.Execution[int]) (int, error) {
  return e.LastResult() + 1, nil
}, retryPolicy)

The Execution object can also tell you if an execution is being retried or if it’s the first attempt:

failsafe.RunWithExecution(func(e failsafe.Execution[any]) error {
  if e.IsRetry() {
    logger.Info("Retrying connection attempt")
  }
  return Connect()
}, retryPolicy)