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 a retry, a hedge, or the first attempt:

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

Context

A Context can also be provided to an Execution, by configuring it with an Executor:

executor := failsafe.NewExecutor[any](retryPolicy).WithContext(ctx)

The Context will be available via an Execution or any event.