README

GoDoc Codeship Codecov Go Report Card

Retry

Retry a function execution with specific intervals with panic recovery

Make sure to read the docs to understand how this package works and what do expected from it.

Installation

go get -u github.com/txgruppi/retry-go

Example

package main

import (
  "log"
  "time"

  "github.com/txgruppi/retry-go"
)

func main() {
  toTry := func(attempt, limit int) error {
    // Do you stuff and return an error if there is any
    return nil
  }

  err := retry.WithFixedInterval(1 * time.Second, 5, toTry)
  if err != nil {
    log.Fatal(err) // It should log the errors if there were any
  }
}

Tests

go get -u -t github.com/txgruppi/retry-go
cd $GOPATH/src/github.com/txgruppi/retry-go
go test ./...

License

MIT

Expand ▾ Collapse ▴

Documentation

Overview

    Package retry manages the execution of certain pieces of code that must run a specific number of times before being considered as failed.

    The execution of a function will be considered failed only if all the tries returned an error, when it happens the 'With*' function will return all the errors in a ErrorGroup.

    If the TryFunc return a nil error at any moment it will be considered a successful execution and nil will be returned by the 'With*' function.

    To know more about the ErrorGroup go to https://github.com/txgruppi/errorgroup-go

    How many times TryFunc will execute?

    The general rule is 'len(BackoffArray) + 1'

    This happens because the BackoffArray is an array of intervals and makes no sense to have an interval after the last execution. Because of this every time the last interval is extracted from the BackoffArray the TryFunc is executed one more time. If you want to run the function only one time you should give a BackoffArray with zero items.

    Index

    Constants

    This section is empty.

    Variables

    View Source
    var (
    	// ErrTryFuncNil is returned when the TryFunc is nil
    	ErrTryFuncNil = errors.New("TryFunc can not be nil")
    )

    Functions

    func WithBackoffArray

    func WithBackoffArray(backoff BackoffArray, fn TryFunc) error

      WithBackoffArray runs the TryFunc with intervals from the given BackoffArray

      It is important to notice that the TryFunc will run 'len(BackoffArray) + 1' times

      func WithFixedInterval

      func WithFixedInterval(interval time.Duration, repeat int, fn TryFunc) error

        WithFixedInterval runs the TryFunc with a BackoffArray created with the given 'interval' repeated 'repeat' times

        It is important to notice that the TryFunc will run 'repeat + 1' times

        Types

        type BackoffArray

        type BackoffArray []time.Duration

          BackoffArray an vector of interval to wait between each retry

          type TryFunc

          type TryFunc func(attempt, limit int) error

            TryFunc is the function to try to execute.

            It receives as arguments: - the number of this attempt '[0..len(BackoffArray]' - the limit of executions 'len(BackoffArray) + 1'

            Source Files