retry

package
v0.0.0-...-e0889ae Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2019 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package retry implements a http.RoundTripper that retries requests

This package uses a configurable ExponentialBackoff mechanism:

r := retry.Transport {
    Backoff: backoff.NewExponentialBackoff(),
    ShouldRetry: /* optional custom retry check function */,
    Transport: /* chain transports! */,
}
client := http.Client{Transport: r}
res, err := client.Do(http.NewRequest("GET", url, nil))
Example
package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/tvastar/http/retry"

	"github.com/cenkalti/backoff"
)

func main() {
	count := 0
	r := retry.Transport{
		Backoff: backoff.NewExponentialBackOff(),
		ShouldRetry: func(res *http.Response, err error, lastAttempt bool) (error, bool) {
			count++
			return err, err != nil && !lastAttempt
		},
		Transport: http.DefaultTransport,
	}
	r.Backoff.MaxElapsedTime = time.Second

	client := http.Client{Transport: r}
	req, err := http.NewRequest("GET", "x.boo.bohemian/a/b/c/d", nil)
	if err != nil {
		panic(err)
	}
	_, err = client.Do(req)
	fmt.Println("Got:", count, err == nil)

}
Output:

Got: 3 false

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Transport

type Transport struct {
	Backoff     *backoff.ExponentialBackOff
	ShouldRetry func(res *http.Response, err error, lastAttempt bool) (error, bool)
	Transport   http.RoundTripper
}

Transport implements the http Retry middleware

func (Transport) RoundTrip

func (t Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the http.RoundTripper interface

Jump to

Keyboard shortcuts

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