Go library that makes it easy to add automatic retries to your projects, including support for context.Context.

go-retry

Go library that makes it easy to add automatic retries to your projects, including support for context.Context.

Example with context.Context

// Create a context that times out after 10 seconds
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

// Perform some action that might fail, with auto retries
err := retry.Run(
    ctx,
    retry.Limit(5), // <-- Limit retries
    retry.Exponential(time.Second), // <-- Exponential backoff
    func(ctx context.Context) error {

        // Do something here:
        err := doSomethingDangerous()

        // If it succeeds:
        return nil

        // If it fails, but is retryable:
        return retry.RetryErr(err)

        // If it fails, and it's not retryable:
        return err

    })

Example without context.Context

If you don't use context.Context or just want something simpler, you can omit the context part entirely:

// Perform some action that might fail, with auto retries
err := retry.Run(
    nil,
    retry.Limit(5), // <-- Limit retries
    retry.Exponential(time.Second), // <-- Exponential backoff
    func(ctx context.Context) error {

        // Do something here:
        err := doSomethingDangerous()

        // If it succeeds:
        return nil

        // If it fails, but is retryable:
        return retry.RetryErr(err)

        // If it fails, and it's not retryable:
        return err

    })

Functional pattern for Delays

In the example above, we use an exponential backoff starting with a 1 second delay. The magnitude of subsequent delays grows exponentially by a factor of two.

However, there are many other choices you could make, depending on your circumstances: Exponential, Fibonacci, Linear, Constant. Additionally, this library supports a functional pattern for layering in multiple delay behaviors, such as logging and random offsets.

Delay with random offset

You can easily add randomized offsets to your Delays with the following functional pattern:

// Just wrap the entire delay in the retry.Rand function
retry.Rand(retry.Exponential(time.Second))

This creates a new wrapper Delay that includes the exponential delay, but adds a random offset at the end.

Adding logging to delays and retries

In much the same way as above, you can add logging so you're notified every time a retry is taking place:

// Logging the plain exponential backoff:
retry.Log(retry.Exponential(time.Second))

// Logging and random offset:
retry.Log(retry.Rand(retry.Exponential(time.Second)))

Hopefully you can see that it's very easy to compose clever behaviors with this simple, functional pattern. You can also create your own functions if you choose. Here's the source code for the Linear delay function, for an example of how simple it is:

func Linear(base time.Duration) Delay {
	return func(iteration int) time.Duration {
		return base * time.Duration(iteration)
	}
}

Remove all retry limits

If you want no limit on the number of retries, you can use the predefined constant retry.RetryForever.

Running the Example code

There is an example in example/main.go that simply fetches the Google homepage using automatic retries. To see the retry behavior in action, turn off your internet connection and run go run ./example from the root of this repo.

Try also turning back on your internet access during the middle of the example code's execution. You'll notice it immediately succeeds and ceases retrying.

Contributions

Contributions are welcome, and encouraged!

Owner
Conner Douglass
Founder, CTO, systems engineer
Conner Douglass
Similar Resources

tiny Go library to normalize URLs

Purell Purell is a tiny Go library to normalize URLs. It returns a pure URL. Pure-ell. Sanitizer and all. Yeah, I know... Based on the wikipedia paper

Dec 7, 2022

httpreq is an http request library written with Golang to make requests and handle responses easily.

httpreq is an http request library written with Golang to make requests and handle responses easily. Install go get github.com/binalyze/http

Feb 10, 2022

Cake is a lightweight HTTP client library for GO, inspired by Java Open-Feign.

Cake is a lightweight HTTP client library for GO, inspired by Java Open-Feign. Installation # With Go Modules, recommanded with go version 1.16

Oct 6, 2022

Parcel - HTTP rendering and binding library for Go

parcel HTTP rendering/binding library for Go Getting Started Add to your project

Jan 1, 2022

Httpx - a fast and multi-purpose HTTP toolkit allow to run multiple probers using retryablehttp library

Httpx - a fast and multi-purpose HTTP toolkit allow to run multiple probers using retryablehttp library

httpx is a fast and multi-purpose HTTP toolkit allow to run multiple probers using retryablehttp library, it is designed to maintain the result reliability with increased threads.

Feb 3, 2022

retryablehttp package provides a familiar HTTP client interface with automatic retries and exponential backoff.

retryablehttp package provides a familiar HTTP client interface with automatic retries and exponential backoff.

Jan 4, 2023

Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables to be loaded from the .env file.

genv Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables

Dec 21, 2022

go HTTP client that makes it plain simple to configure TLS, basic auth, retries on specific errors, keep-alive connections, logging, timeouts etc.

goat Goat, is an HTTP client built on top of a standard Go http package, that is extremely easy to configure; no googling required. The idea is simila

Jun 25, 2022

go-fastdfs 是一个简单的分布式文件系统(私有云存储),具有无中心、高性能,高可靠,免维护等优点,支持断点续传,分块上传,小文件合并,自动同步,自动修复。Go-fastdfs is a simple distributed file system (private cloud storage), with no center, high performance, high reliability, maintenance free and other advantages, support breakpoint continuation, block upload, small file merge, automatic synchronization, automatic repair.(similar fastdfs).

go-fastdfs 是一个简单的分布式文件系统(私有云存储),具有无中心、高性能,高可靠,免维护等优点,支持断点续传,分块上传,小文件合并,自动同步,自动修复。Go-fastdfs is a simple distributed file system (private cloud storage), with no center, high performance, high reliability, maintenance free and other advantages, support breakpoint continuation, block upload, small file merge, automatic synchronization, automatic repair.(similar fastdfs).

中文 English 愿景:为用户提供最简单、可靠、高效的分布式文件系统。 go-fastdfs是一个基于http协议的分布式文件系统,它基于大道至简的设计理念,一切从简设计,使得它的运维及扩展变得更加简单,它具有高性能、高可靠、无中心、免维护等优点。 大家担心的是这么简单的文件系统,靠不靠谱,可不

Jan 8, 2023

A quick and easy password protected web server for your files. httpfolder makes downloading/uploading files from your current working directory easy, even for fairly large files.

httpfolder A quick and easy password protected web server for your files. httpfolder makes downloading/uploading files from your current working direc

Sep 12, 2022

Morecontext - Context.Context helpers to make your life slightly easier

morecontext context.Context helpers to make your life slightly easier. -- import

Mar 5, 2022

Add request id to a request's context

RequestID 🔎 This is a very simple piece of middleware for adding request/correlation IDs to the context of a http request. By default, this module wi

Dec 4, 2021

Libraries and CLIs for my personal all-in-one productivity system including components like bookmarks, notes, todos, projects, etc.

bntp.go Libraries and CLIs for my personal all-in-one productivity system including components like bookmarks, notes, todos, projects, etc. Neovim int

Sep 13, 2022

Add MFA/2FA support in your CLI

go-oauth-cli-client Add MFA/2FA support in your CLI for IDPs that support OAuth 2.0 Authorization Code Flow Features MFA/2FA with minimal user interac

Mar 23, 2022

An easy way to add useful startup banners into your Go applications

An easy way to add useful startup banners into your Go applications

Try browsing the code on Sourcegraph! Banner Add beautiful banners into your Go applications Table of Contents Motivation Usage API Command line flags

Jan 1, 2023

Go (golang) http calls with retries and backoff

pester pester wraps Go's standard lib http client to provide several options to increase resiliency in your request. If you experience poor network co

Dec 28, 2022

Golang gRPC Middlewares: interceptor chaining, auth, logging, retries and more.

Golang gRPC Middlewares: interceptor chaining, auth, logging, retries and more.

Jan 7, 2023

Resilient wrapper around Go http.Client that uses exponential backoff and jitter for retries.

Hardy Hardy is a very simple wrapper around http.Client that enables you to add more resilience and reliability for your HTTP calls through retries. A

Dec 29, 2022
Go (golang) http calls with retries and backoff

pester pester wraps Go's standard lib http client to provide several options to increase resiliency in your request. If you experience poor network co

Dec 28, 2022
Retry - Efficient for-loop retries in Go

retry Package retry implements an efficient loop-based retry mechanism that allo

Aug 23, 2022
Replacement of ApacheBench(ab), support for transactional requests, support for command line and package references to HTTP stress testing tool.

stress stress is an HTTP stress testing tool. Through this tool, you can do a stress test on the HTTP service and get detailed test results. It is ins

Aug 23, 2022
A golang tool which makes http requests and prints the address of the request along with the MD5 hash of the response.

Golang Tool This repository is a golang tool which makes http requests to the external server and prints the address of the request along with the MD5

Oct 17, 2021
goql is a GraphQL client package written in Go. with built-in two-way marshaling support via struct tags.

goql is a GraphQL client package written in Go. with built-in two-way marshaling support via struct tags.

Dec 1, 2022
IceFireDB-Proxy - IceFireDB proxy, easier to use IceFireDB, support resp protocol
IceFireDB-Proxy - IceFireDB proxy, easier to use IceFireDB, support resp protocol

IceFireDB-Proxy IceFireDB-Proxy is a high-performance, highly available, and use

Jun 8, 2022
A Go "clone" of the great and famous Requests library

GRequests A Go "clone" of the great and famous Requests library License GRequests is licensed under the Apache License, Version 2.0. See LICENSE for t

Dec 23, 2022
Simple HTTP and REST client library for Go

Resty Simple HTTP and REST client library for Go (inspired by Ruby rest-client) Features section describes in detail about Resty capabilities Resty Co

Jan 1, 2023
A Go HTTP client library for creating and sending API requests
A Go HTTP client library for creating and sending API requests

Sling Sling is a Go HTTP client library for creating and sending API requests. Slings store HTTP Request properties to simplify sending requests and d

Jan 7, 2023
An HTTP proxy library for Go

Introduction Package goproxy provides a customizable HTTP proxy library for Go (golang), It supports regular HTTP proxy, HTTPS through CONNECT, and "h

Jan 4, 2023