All Projects → avast → Retry Go

avast / Retry Go

Licence: mit
Simple golang library for retry mechanism

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Retry Go

Electron Forge
A complete tool for creating, publishing, and installing modern Electron applications
Stars: ✭ 4,714 (+942.92%)
Mutual labels:  hacktoberfest
Dynamoid
Ruby ORM for Amazon's DynamoDB.
Stars: ✭ 449 (-0.66%)
Mutual labels:  hacktoberfest
Csi Digitalocean
A Container Storage Interface (CSI) Driver for DigitalOcean Block Storage
Stars: ✭ 452 (+0%)
Mutual labels:  hacktoberfest
The Swift Programming Language In Chinese
中文版 Apple 官方 Swift 教程《The Swift Programming Language》
Stars: ✭ 20,375 (+4407.74%)
Mutual labels:  hacktoberfest
Github Readme Stats
⚡ Dynamically generated stats for your github readmes
Stars: ✭ 34,955 (+7633.41%)
Mutual labels:  hacktoberfest
Telegram
✈️ Telegram Notifications Channel for Laravel
Stars: ✭ 450 (-0.44%)
Mutual labels:  hacktoberfest
Cucumber Js
Cucumber for JavaScript
Stars: ✭ 4,383 (+869.69%)
Mutual labels:  hacktoberfest
Gitlab Ci Pipeline Php
☕️ Docker images for test PHP applications with Gitlab CI (or any other CI platform!)
Stars: ✭ 451 (-0.22%)
Mutual labels:  hacktoberfest
Graphql Engine
Blazing fast, instant realtime GraphQL APIs on your DB with fine grained access control, also trigger webhooks on database events.
Stars: ✭ 24,845 (+5396.68%)
Mutual labels:  hacktoberfest
Justtryharder
JustTryHarder, a cheat sheet which will aid you through the PWK course & the OSCP Exam. (Inspired by PayloadAllTheThings)
Stars: ✭ 450 (-0.44%)
Mutual labels:  hacktoberfest
Mattermost Server
Mattermost is an open source platform for secure collaboration across the entire software development lifecycle.
Stars: ✭ 21,623 (+4683.85%)
Mutual labels:  hacktoberfest
React Native Elements
Cross-Platform React Native UI Toolkit
Stars: ✭ 21,758 (+4713.72%)
Mutual labels:  hacktoberfest
Libmesh
libMesh github repository
Stars: ✭ 450 (-0.44%)
Mutual labels:  hacktoberfest
Swagger Ui
Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
Stars: ✭ 21,279 (+4607.74%)
Mutual labels:  hacktoberfest
Zek
Generate a Go struct from XML.
Stars: ✭ 451 (-0.22%)
Mutual labels:  hacktoberfest
Bubbletea
A powerful little TUI framework 🏗
Stars: ✭ 7,886 (+1644.69%)
Mutual labels:  hacktoberfest
Superjson
Safely serialize JavaScript expressions to a superset of JSON, which includes Dates, BigInts, and more.
Stars: ✭ 446 (-1.33%)
Mutual labels:  hacktoberfest
Openshift Docs
OpenShift 3 and 4 product and community documentation
Stars: ✭ 452 (+0%)
Mutual labels:  hacktoberfest
React Masonry Css
React Masonry layout component powered by CSS, dependancy free
Stars: ✭ 451 (-0.22%)
Mutual labels:  hacktoberfest
Kubectl Neat
Clean up Kuberntes yaml and json output to make it readable
Stars: ✭ 451 (-0.22%)
Mutual labels:  hacktoberfest

retry

Release Software License Travis AppVeyor Go Report Card GoDoc codecov.io Sourcegraph

Simple library for retry mechanism

slightly inspired by Try::Tiny::Retry

SYNOPSIS

http get with retry:

url := "http://example.com"
var body []byte

err := retry.Do(
	func() error {
		resp, err := http.Get(url)
		if err != nil {
			return err
		}
		defer resp.Body.Close()
		body, err = ioutil.ReadAll(resp.Body)
		if err != nil {
			return err
		}

		return nil
	},
)

fmt.Println(body)

next examples

SEE ALSO

  • giantswarm/retry-go - slightly complicated interface.

  • sethgrid/pester - only http retry for http calls with retries and backoff

  • cenkalti/backoff - Go port of the exponential backoff algorithm from Google's HTTP Client Library for Java. Really complicated interface.

  • rafaeljesus/retry-go - looks good, slightly similar as this package, don't have 'simple' Retry method

  • matryer/try - very popular package, nonintuitive interface (for me)

BREAKING CHANGES

3.0.0

1.0.2 -> 2.0.0

0.3.0 -> 1.0.0

  • retry.Retry function are changed to retry.Do function

  • retry.RetryCustom (OnRetry) and retry.RetryCustomWithOpts functions are now implement via functions produces Options (aka retry.OnRetry)

Usage

var (
	DefaultAttempts      = uint(10)
	DefaultDelay         = 100 * time.Millisecond
	DefaultMaxJitter     = 100 * time.Millisecond
	DefaultOnRetry       = func(n uint, err error) {}
	DefaultRetryIf       = IsRecoverable
	DefaultDelayType     = CombineDelay(BackOffDelay, RandomDelay)
	DefaultLastErrorOnly = false
	DefaultContext       = context.Background()
)

func BackOffDelay

func BackOffDelay(n uint, _ error, config *Config) time.Duration

BackOffDelay is a DelayType which increases delay between consecutive retries

func Do

func Do(retryableFunc RetryableFunc, opts ...Option) error

func FixedDelay

func FixedDelay(_ uint, _ error, config *Config) time.Duration

FixedDelay is a DelayType which keeps delay the same through all iterations

func IsRecoverable

func IsRecoverable(err error) bool

IsRecoverable checks if error is an instance of unrecoverableError

func RandomDelay

func RandomDelay(_ uint, _ error, config *Config) time.Duration

RandomDelay is a DelayType which picks a random delay up to config.maxJitter

func Unrecoverable

func Unrecoverable(err error) error

Unrecoverable wraps an error in unrecoverableError struct

type Config

type Config struct {
}

type DelayTypeFunc

type DelayTypeFunc func(n uint, err error, config *Config) time.Duration

DelayTypeFunc is called to return the next delay to wait after the retriable function fails on err after n attempts.

func CombineDelay

func CombineDelay(delays ...DelayTypeFunc) DelayTypeFunc

CombineDelay is a DelayType the combines all of the specified delays into a new DelayTypeFunc

type Error

type Error []error

Error type represents list of errors in retry

func (Error) Error

func (e Error) Error() string

Error method return string representation of Error It is an implementation of error interface

func (Error) WrappedErrors

func (e Error) WrappedErrors() []error

WrappedErrors returns the list of errors that this Error is wrapping. It is an implementation of the errwrap.Wrapper interface in package errwrap so that retry.Error can be used with that library.

type OnRetryFunc

type OnRetryFunc func(n uint, err error)

Function signature of OnRetry function n = count of attempts

type Option

type Option func(*Config)

Option represents an option for retry.

func Attempts

func Attempts(attempts uint) Option

Attempts set count of retry default is 10

func Context

func Context(ctx context.Context) Option

Context allow to set context of retry default are Background context

example of immediately cancellation (maybe it isn't the best example, but it describes behavior enough; I hope)

ctx, cancel := context.WithCancel(context.Background())
cancel()

retry.Do(
	func() error {
		...
	},
	retry.Context(ctx),
)

func Delay

func Delay(delay time.Duration) Option

Delay set delay between retry default is 100ms

func DelayType

func DelayType(delayType DelayTypeFunc) Option

DelayType set type of the delay between retries default is BackOff

func LastErrorOnly

func LastErrorOnly(lastErrorOnly bool) Option

return the direct last error that came from the retried function default is false (return wrapped errors with everything)

func MaxDelay

func MaxDelay(maxDelay time.Duration) Option

MaxDelay set maximum delay between retry does not apply by default

func MaxJitter

func MaxJitter(maxJitter time.Duration) Option

MaxJitter sets the maximum random Jitter between retries for RandomDelay

func OnRetry

func OnRetry(onRetry OnRetryFunc) Option

OnRetry function callback are called each retry

log each retry example:

retry.Do(
	func() error {
		return errors.New("some error")
	},
	retry.OnRetry(func(n uint, err error) {
		log.Printf("#%d: %s\n", n, err)
	}),
)

func RetryIf

func RetryIf(retryIf RetryIfFunc) Option

RetryIf controls whether a retry should be attempted after an error (assuming there are any retry attempts remaining)

skip retry if special error example:

retry.Do(
	func() error {
		return errors.New("special error")
	},
	retry.RetryIf(func(err error) bool {
		if err.Error() == "special error" {
			return false
		}
		return true
	})
)

By default RetryIf stops execution if the error is wrapped using retry.Unrecoverable, so above example may also be shortened to:

retry.Do(
	func() error {
		return retry.Unrecoverable(errors.New("special error"))
	}
)

type RetryIfFunc

type RetryIfFunc func(error) bool

Function signature of retry if function

type RetryableFunc

type RetryableFunc func() error

Function signature of retryable function

Contributing

Contributions are very much welcome.

Makefile

Makefile provides several handy rules, like README.md generator , setup for prepare build/dev environment, test, cover, etc...

Try make help for more information.

Before pull request

please try:

  • run tests (make test)
  • run linter (make lint)
  • if your IDE don't automaticaly do go fmt, run go fmt (make fmt)

README

README.md are generate from template .godocdown.tmpl and code documentation via godocdown.

Never edit README.md direct, because your change will be lost.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].