retryabletransport

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

Retryable Transport

The retryabletransport package provides a customizable HTTP transport mechanism with retry functionality built-in. This can be particularly useful for scenarios where network requests might fail intermittently due to transient errors, and retrying them can increase the chances of success.

Features

  • Customizable Retries: Users can specify a custom retry policy through the ShouldRetryFunc type, which determines whether a request should be retried based on the HTTP request, response, and error encountered.
  • Backoff Strategy: The package utilizes an exponential backoff strategy for retrying requests, which progressively increases the time between retries to mitigate overloading the server with retry attempts.
  • Notification: Users can optionally provide a NotifyFunc to receive notifications about retry attempts, including the error encountered and the duration between retries.
  • Configurable Maximum Retries: The BackOffPolicy struct allows users to set the maximum number of retries for a given request.

Usage

package main

import (
	"context"
	"errors"
	"fmt"
	"net/http"
	"syscall"
	"time"

	"github.com/linzhengen/retryabletransport"
)

func main() {
	client := &http.Client{
		Transport: retryabletransport.New(
			http.DefaultTransport,
			func(req *http.Request, resp *http.Response, err error) bool {
				if errors.Is(err, syscall.ECONNRESET) {
					return true
				}
				if resp != nil && resp.StatusCode == http.StatusTooManyRequests {
					return true
				}
				return false
			},
			func(ctx context.Context, err error, duration time.Duration) {
				fmt.Printf("retry http request, err: %v, duration: %v", err, duration)
			},
			&retryabletransport.BackOffPolicy{
				MaxRetries: 3,
			},
		),
		Timeout: 3 * time.Second,
	}
	_, err := client.Get("http://example.com")
	if err != nil {
		fmt.Println(err)
	}
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ShouldRetryRespError = errors.New("should retry response error")

ShouldRetryRespError is returned when a response indicates the request should be retried.

Functions

This section is empty.

Types

type BackOffPolicy

type BackOffPolicy struct {
	MaxRetries uint64
}

BackOffPolicy represents the maximum number of retries for a backoff policy.

type NotifyFunc

type NotifyFunc func(ctx context.Context, err error, duration time.Duration)

NotifyFunc represents a function that notifies about errors and durations during retries.

type RoundTripper

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

RoundTripper provides a retryable HTTP transport mechanism.

func New

func New(roundTripper http.RoundTripper, shouldRetryFunc ShouldRetryFunc, notifyFunc NotifyFunc, backOffPolicy *BackOffPolicy) *RoundTripper

New creates a new RoundTripper with the provided parameters. If roundTripper is nil, http.DefaultTransport is used. If backOffPolicy is nil, a default policy with MaxRetries set to 3 is used.

Example
package main

import (
	"context"
	"errors"
	"fmt"
	"net/http"
	"syscall"
	"time"

	"github.com/linzhengen/retryabletransport"
)

func main() {
	client := &http.Client{
		Transport: retryabletransport.New(
			http.DefaultTransport,
			func(req *http.Request, resp *http.Response, err error) bool {
				if errors.Is(err, syscall.ECONNRESET) {
					return true
				}
				if resp != nil && resp.StatusCode == http.StatusTooManyRequests {
					return true
				}
				return false
			},
			func(ctx context.Context, err error, duration time.Duration) {
				fmt.Printf("retry http request, err: %v, duration: %v", err, duration)
			},
			&retryabletransport.BackOffPolicy{
				MaxRetries: 3,
			},
		),
		Timeout: 3 * time.Second,
	}
	_, err := client.Get("http://example.com")
	if err != nil {
		fmt.Println(err)
	}
}
Output:

func (*RoundTripper) RoundTrip

func (p *RoundTripper) RoundTrip(req *http.Request) (resp *http.Response, err error)

RoundTrip executes a single HTTP transaction and returns a response. It implements the http.RoundTripper interface.

type ShouldRetryFunc

type ShouldRetryFunc func(*http.Request, *http.Response, error) bool

ShouldRetryFunc represents a function that determines whether a request should be retried based on the request, response, and error.

Jump to

Keyboard shortcuts

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