retry

package module
v0.0.0-...-5981a38 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 14, 2017 License: MIT Imports: 3 Imported by: 23

README

retry-go

  • Retrying made simple and easy for golang.

Installation

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

Usage

Do
package main

import (
  "time"

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

func main() {
  attempts := 3
  sleepTime := time.Second*2
  if err := retry.Do(func() error {
    return work()
  }, attempts, sleepTime); err != nil {
    // Retry failed
  }
}
DoHTTP
package main

import (
  "time"

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

func main() {
  attempts := 3
  sleepTime := time.Second*2
  if err := retry.DoHTTP(func() (*http.Response, error) {
    return makeRequest()
  }, attempts, sleepTime); err != nil {
    // Retry failed
  }
}

Contributing

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

Badges

Build Status Go Report Card Go Doc


GitHub @rafaeljesus  ·  Medium @_jesus_rafael  ·  Twitter @_jesus_rafael

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(fn Func, retries int, sleep time.Duration) error

Do runs the passed function until the number of retries is reached. Whenever Func returns err it will sleep and Func will be executed again in a recursive fashion. The sleep value is slightly modified on every retry (exponential backoff) to prevent the thundering herd problem (https://en.wikipedia.org/wiki/Thundering_herd_problem). If no value is given to sleep it will defaults to 500ms.

func DoHTTP

func DoHTTP(fn HTTPFunc, retries int, sleep time.Duration) (*http.Response, error)

DoHTTP wraps Func and returns *http.Response and error as returning arguments.

Types

type Func

type Func func() error

Func is the function to be executed and eventually retried.

type HTTPFunc

type HTTPFunc func() (*http.Response, error)

HTTPFunc is the function to be executed and eventually retried. The only difference from Func is that it expects an *http.Response on the first returning argument.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL