• Stars
    star
    2,380
  • Rank 19,327 (Top 0.4 %)
  • Language
    Go
  • License
    MIT License
  • Created over 7 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple golang library for retry mechanism

retry

Release Software License GitHub Actions 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

  • 4.0.0

    • infinity retry is possible by set Attempts(0) by PR #49
  • 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

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) As

func (e Error) As(target interface{}) bool

func (Error) Error

func (e Error) Error() string

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

func (Error) Is

func (e Error) Is(target error) bool

func (Error) Unwrap

func (e Error) Unwrap() error

Unwrap the last error for compatibility with errors.Unwrap(). When you need to unwrap all errors, you should use WrappedErrors() instead.

err := Do(
	func() error {
		return errors.New("original error")
	},
	Attempts(1),
)

fmt.Println(errors.Unwrap(err)) # "original error" is printed

Added in version 4.2.0.

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. Setting to 0 will retry until the retried function succeeds. default is 10

func AttemptsForError

func AttemptsForError(attempts uint, err error) Option

AttemptsForError sets count of retry in case execution results in given err Retries for the given err are also counted against total retries. The retry will stop if any of given retries is exhausted.

added in 4.3.0

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"))
	}
)

func WithTimer

func WithTimer(t Timer) Option

WithTimer provides a way to swap out timer module implementations. This primarily is useful for mocking/testing, where you may not want to explicitly wait for a set duration for retries.

example of augmenting time.After with a print statement

type struct MyTimer {}

func (t *MyTimer) After(d time.Duration) <- chan time.Time {
    fmt.Print("Timer called!")
    return time.After(d)
}

retry.Do(

    func() error { ... },
	   retry.WithTimer(&MyTimer{})

)

type RetryIfFunc

type RetryIfFunc func(error) bool

Function signature of retry if function

type RetryableFunc

type RetryableFunc func() error

Function signature of retryable function

type Timer

type Timer interface {
	After(time.Duration) <-chan time.Time
}

Timer represents the timer used to track time for a retry.

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

maybe you need make setup in order to setup environment

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.

More Repositories

1

retdec

RetDec is a retargetable machine-code decompiler based on LLVM.
C++
7,957
star
2

android-butterknife-zelezny

Android Studio plug-in for generating ButterKnife injections from selected layout XML.
Java
3,383
star
3

android-styled-dialogs

Backport of Material dialogs with easy-to-use API based on DialogFragment
Java
2,147
star
4

retdec-idaplugin

RetDec plugin for IDA
C++
753
star
5

pytest-docker

Docker-based integration tests
Python
426
star
6

gradle-docker-compose-plugin

Simplifies usage of Docker Compose for integration testing in Gradle environment.
Groovy
412
star
7

ioc

Threat Intel IoCs + bits and pieces of dark matter
C
368
star
8

scala-server-toolkit

Functional programming toolkit for building server applications in Scala.
Scala
198
star
9

hdfs-shell

HDFS Shell is a HDFS manipulation tool to work with functions integrated in Hadoop DFS
Java
150
star
10

apkparser

APK manifest & resources parsing in Golang.
Go
123
star
11

yaramod

Parsing of YARA rules into AST and building new rulesets in C++.
C++
114
star
12

topee

Google Chrome Extension API for Safari
JavaScript
104
star
13

yari

YARI is an interactive debugger for YARA Language.
Rust
85
star
14

apkverifier

APK Signature verification in Go. Supports scheme v1, v2 and v3 and passes Google apksig's testing suite.
Go
81
star
15

gradle-dependencies-viewer

A simple web UI to analyze dependencies for your project based on the text data generated from "gradle dependencies" command.
JavaScript
78
star
16

yls

YARA Language Server
Python
67
star
17

yarang

Alternative YARA scanning engine
C++
66
star
18

pelib

PE file manipulation library.
C++
62
star
19

datadog4s

Making great monitoring easy in functional Scala
Scala
62
star
20

pe_tools

A cross-platform Python toolkit for parsing/writing PE files.
Python
62
star
21

k8s-admission-webhook

A general-purpose Kubernetes admission webhook to aid with enforcing best practices within your cluster.
Go
54
star
22

grpc-java-jwt

JWT based authentication for gRPC-Java.
Java
46
star
23

yaracpp

C++ wrapper for YARA.
C++
45
star
24

hexrays-demo

IDA SDK tech demo
C++
35
star
25

rabbitmq-scala-client

Scala wrapper over standard RabbitMQ Java client library
Scala
35
star
26

marathon-vault-plugin

Marathon plugin which injects Vault secrets via environment variables
Scala
30
star
27

android-lectures

Class material for lectures about Android development
Kotlin
27
star
28

avast-ctu-cape-dataset

Jupyter Notebook
25
star
29

retdec-regression-tests-framework

A framework for writing and running regression tests for RetDec and related tools.
Python
24
star
30

capstone-dumper

Utility for dumping all the information Capstone has on given instructions.
C++
24
star
31

libdwarf

Library to provide access to DWARF debugging information.
C
23
star
32

PurpleDome

Simulation environment for attacks on computer networks
Python
21
star
33

llvm

An LLVM clone modified for use in RetDec and associated tools.
LLVM
19
star
34

wanna-ml

Complete MLOps framework for Vertex-AI
Python
18
star
35

grpc-json-bridge

Library for exposing gRPC endpoints via HTTP (JSON) API
Scala
18
star
36

authenticode-parser

Authenticode-parser is a simple C library for Authenticode format parsing using OpenSSL.
C
16
star
37

ep-stats

Statistics for Experimentation Platform
Python
15
star
38

elfio

Library for reading and generating ELF files.
C++
14
star
39

vuei18n-po

transform gettext .po files for vue-i18n
JavaScript
14
star
40

retdec-regression-tests

A collection of regression tests for RetDec and associated tools.
Python
12
star
41

safariextz

Safari extension packer for node.js
JavaScript
9
star
42

cactus

Library for easy conversion between GPB and Scala case classes.
Scala
9
star
43

hermes

SMTP honeypot built on top of the Salmon mail server
Python
8
star
44

kafka-tests

Integration test of Apache Kafka 0.9.0+ and Java clients.
Java
8
star
45

bytes

Library providing universal interface for having an immutable representation of sequence of bytes.
Java
7
star
46

genrex

Generator of regular expressions
Python
7
star
47

ctf-aca-brno-2020

Tasks from Avast Cyber Adventure 2020 Brno
Objective-C
6
star
48

metrics

Java/Scala library defining API for metrics publishing
Java
6
star
49

slog4s

Structured and contextual logging for Scala
Scala
6
star
50

Stor

HTTP API for SHA256 objects
Perl
5
star
51

clockwork

An adoption of the map-reduce paradigm based on the concept of coroutines to the world of stream data processing.
Java
5
star
52

covid-19-ioc

HTML
5
star
53

asio-mutex

Awaitable Mutex compatible with Boost.Asio
C++
5
star
54

tlshc

TLSH library in C
C
5
star
55

decryptor-keys

Decryption keys for our ransomware decryptors
5
star
56

scala-hashes

Case-classes representing MD5, SHA1 and SHA256.
Scala
5
star
57

bytecompressor

Java and Scala abstractions for some compression algorithms.
Java
5
star
58

retdec-support

Support packages for the RetDec decompiler.
5
star
59

hackcambridge-ccleaner-app

A custom build of CCleaner that enables the integration of Avast Secure Browser
Visual Basic
5
star
60

mongodb-oplog-stats

A tool for obtaining statistics about a MongoDB replica-set oplog
Rust
5
star
61

hackcambridge-ccleaner-extension

A stub for the CCleaner extension for Avast Secure Browser
JavaScript
5
star
62

machine-learning-python

Machine learning in Python Workshop
Jupyter Notebook
4
star
63

syringe

Syringe - Dependency Injection and Configuration Library from AVAST Software
Java
4
star
64

labmanager-unit-vsphere

REST service for vmWare vSphere virtual machine control
Python
4
star
65

syringe-maven-plugin

Supporting Maven plugin for Syringe
Java
3
star
66

cargo-depdiff

Inspecting what changed around dependencies between versions
Rust
3
star
67

ndisdump

A no-dependencies network packet capture tool for Windows
C++
3
star
68

webtrails

Svelte
3
star
69

BigMap

Scala Map that uses binary search in memory mapped sorted file. It makes possible usage of data sets bigger than available memory as a Map.
Scala
3
star
70

management-console-config

Sample configuration for Avast Business management console
2
star
71

hackcambridge-challenge

Integrate the Avast Secure Browser (ASB) and CCleaner products to improve user privacy, prevent website tracking, and reduce the user’s online footprint.
2
star
72

storage-client

Scala
2
star
73

boost-python-examples

Examples that show capabilities of Boost Python
C++
2
star
74

docker-centos_perl_cpanm

2
star
75

adblock

JavaScript
2
star
76

retdec-build-system-tests

Tests of RetDec build system. This can also serve as RetDec component usage examples.
C++
2
star
77

eslint-plugin-apklab-frida

ESLint plugin & config for the Frida scripts used in the apklab.io platform.
JavaScript
2
star
78

VSArchConv

Converts .sln/.vcxproj to support different architecture
C++
2
star
79

stor-client

Go
2
star
80

stepdance

Functional iterators for easy and elegant parsing, scanning, iterating etc. Written Scala.
Scala
1
star
81

docker-flume-hdfs

Shell
1
star
82

vsphere-instaclone

Really quickly clone machines to be used as TeamCity agents
Kotlin
1
star
83

jmx-publisher

Tool to get properties and methods published via JMX easily.
Java
1
star
84

browser-extension-messaging-sample

JavaScript
1
star
85

continuity

Library for passing context between threads in multi-threaded applications
Scala
1
star
86

instaprofiles-sync

application is used to regularly synchronize defined cloud profiles for [TeamCity plugin vsphere-instaclone](https://github.com/avast/vsphere-instaclone)
Java
1
star
87

jasmine-class-mock

Create a mock class for the Jasmine framework
JavaScript
1
star
88

jfrog-verisign

JFrog plugin to verify deploying artifacts signatures. It supports both JAR and RPM (PGP) verification
Java
1
star
89

https-encryption

Avast HTTPS Encryption powered by HTTPSEverywhere
JavaScript
1
star
90

kluzo

Library for passing tracing ID between threads in multi-threaded applications
Scala
1
star
91

firefox-xpi

Firefox extension packer for node.js
JavaScript
1
star
92

fairy-tale

Toolbox for functional programming in Scala using Finally Tagless approach
Scala
1
star
93

ResolveTest

Simple dns resolve utility.
C++
1
star
94

mdevcamp-workshop

Kotlin Multiplatform - Unleash the Power of Shared Code
Kotlin
1
star
95

gossip-bot

Find out what is happening within the company
Go
1
star