client

package
v1.6.3 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Default

func Default(attempts int, opts ...transport.Option) (*http.Client, error)

Default uses an unconfigured http.Client with the http.DefaultTransport set.

Example
package main

import (
	"net/http"
	"time"

	"github.com/MovieStoreGuy/retry"
	"github.com/MovieStoreGuy/retry/http/client"
	"github.com/MovieStoreGuy/retry/http/status"
	"github.com/MovieStoreGuy/retry/http/transport"
)

func main() {
	c, err := client.Default(6, // Sets the static limit of allowed attempts
		transport.WithRetryOnStatusCode(http.StatusConflict, http.StatusTooManyRequests),
		transport.WithRetryOnStatusGroup(status.Group5xx),
		transport.WithRetryOptions(
			retry.WithExponentialBackoff(200*time.Millisecond, 1.4),
			retry.WithJitter(80*time.Millisecond),
		),
	)
	if err != nil {
		panic(err)
	}
	resp, err := c.Get(`https://golang.org`)
	if err != nil {
		panic(err)
	}
	// handle response
	_ = resp
}

func New

func New(c *http.Client, attempts int, opts ...transport.Option) (*http.Client, error)

New will add transport decorator to in use transport of the client If the transport is not already set, http.DefaultTransport is used

Example
package main

import (
	"crypto/tls"
	"net/http"

	"github.com/MovieStoreGuy/retry/http/client"
	"github.com/MovieStoreGuy/retry/http/status"
	"github.com/MovieStoreGuy/retry/http/transport"
)

func main() {
	t := &http.Transport{
		TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper), // Disable HTTP/2 support
	}

	c, err := client.New(&http.Client{Transport: t}, 8,
		transport.WithRetryOnStatusCode(http.StatusTooManyRequests),
		transport.WithRetryOnStatusGroup(status.Group5xx),
	)

	if err != nil {
		panic(err)
	}

	if _, err := c.Get(`https://site-with-rate-limits.com`); err != nil {
		panic(err)
	}
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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