retry

package
v0.0.0-...-bcea9b7 Latest Latest
Warning

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

Go to latest
Published: May 10, 2021 License: MIT Imports: 5 Imported by: 0

README

Usage of Retrier

Retry supervised do funcs which automatically handle failures when they occur by performing retries.

Example

package main 

import (
	"github.com/donutloop/toolkit/retry"
	"log"
	"context"
)

func main() {

    r := retry.NewRetrier(0.125, 0.25, 2, new(retry.Exp))
  
    err := r.Retry(context.Background(), func() (bool, error) {
   		// do things
   		return true, nil
    })
    
    if err != nil {
        log.Fatal(err)
    }
}

Usage of Roundtripper Retrier

Example

package main 

import (
	"github.com/donutloop/toolkit/retry"
	"log"
	"context"
)

func main() {
        retryRoundTripper := retry.NewRoundTripper(http.DefaultTransport, .50 , .15 , 3, []int{http.StatusBadRequest}, new(retry.Exp))
	httpClient := new(http.Client)
	httpClient.Transport = retryRoundTripper

	req, err := http.NewRequest(http.MethodGet, "http://example.com", nil )
	if err != nil {
		//...
	}

	resp, err := httpClient.Do(req)
	if err != nil {
		//...
	}
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExhaustedError

type ExhaustedError struct{}

func (*ExhaustedError) Error

func (r *ExhaustedError) Error() string

type Exp

type Exp struct{}

func (*Exp) Policy

func (e *Exp) Policy(intervalInSeconds, maxIntervalInSeconds float64) float64

type Retrier

type Retrier interface {
	Retry(ctx context.Context, do RetryableDo) error
}
Example
package main

import (
	"context"
	"fmt"

	"github.com/donutloop/toolkit/retry"
)

func main() {
	r := retry.NewRetrier(0.125, 0.25, 2, new(retry.Exp))
	err := r.Retry(context.Background(), func() (bool, error) {
		fmt.Println("fire request")
		return true, nil
	})

	if err != nil {
		fmt.Printf("error: (%v) \n", err)
	}

}
Output:

fire request

func NewRetrier

func NewRetrier(initialIntervalInSeconds, maxIntervalInSeconds float64, tries uint, strategy Strategy) Retrier

type RetryableDo

type RetryableDo func() (bool, error)

type RoundTripper

type RoundTripper struct {
	// contains filtered or unexported fields
}

func NewRoundTripper

func NewRoundTripper(next http.RoundTripper, maxInterval, initialInterval float64, tries uint, blacklistStatusCodes []int, strategy Strategy) *RoundTripper

NewRoundTripper is constructing a new retry RoundTripper with given default values.

func (*RoundTripper) RoundTrip

func (rt *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip is retrying a outgoing request in case of bad status code and not blacklisted status codes. if rt.next.RoundTrip(req) is return an error then it will abort the process retrying a request.

type Strategy

type Strategy interface {
	Policy(intervalInSeconds, maxIntervalInSeconds float64) float64
}

Jump to

Keyboard shortcuts

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