phttp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultBackOff = backoff.ExponentialBackOff{
	InitialInterval:     500 * time.Millisecond,
	RandomizationFactor: 0.5,
	Multiplier:          1.5,
	MaxInterval:         5 * time.Second,
	MaxElapsedTime:      30 * time.Second,
	Stop:                backoff.Stop,
	Clock:               backoff.SystemClock,
}

DefaultBackOff is an opinionated backoff.ExponentialBackOff which implements the backoff.BackOff interface.

View Source
var DefaultRateLimiter = rate.NewLimiter(rate.Limit(1), 1)

DefaultRateLimiter is an opinionated rate.Limiter which implements the Waiter interface.

Functions

This section is empty.

Types

type Client

type Client struct {
	Requester Requester
	Waiter    Waiter
	Backoff   backoff.BackOff
}

Client provides all required functionality for 1. performing http requests with a Requester 2. while not exceeding a defined rate limit with a Waiter 3. temporary errors can be retried with a backoff.BackOff

func New

func New(opts ...Option) *Client

New creates a Client and accepts Options to configure it.

func NewWithDefaults

func NewWithDefaults() *Client

NewWithDefaults returns a client with the two default options for rate limiting and backoff DefaultRateLimiter and DefaultBackOff

func (*Client) Do

func (c *Client) Do(req *http.Request) (*http.Response, error)

Do is the interface for http.Client.Do.

type HTTPError

type HTTPError struct {
	Code     int
	Body     string
	Response *http.Response
}

HTTPError exposes the http.Response while also giving some convenience for the http status code & response body.

func (HTTPError) Error

func (e HTTPError) Error() string

Error will print the http status code and optionally the http response body.

type Option

type Option func(c *Client)

Option is a function to alter the behaviour of a Client.

func WithBackOff

func WithBackOff(bo backoff.BackOff) Option

WithBackOff configures the Client to use the given backoff.BackOff.

func WithHttpClient

func WithHttpClient(requester Requester) Option

WithHttpClient configures the Client to use the given Requester. http.DefaultClient implements the Requester interface.

func WithRateLimiter

func WithRateLimiter(waiter Waiter) Option

WithRateLimiter configures the Client to use the given Waiter.

type Requester

type Requester interface {
	Do(req *http.Request) (*http.Response, error)
}

Requester is this libraries interface for a http.Client. This is due to https://github.com/golang/go/issues/16047

type Waiter

type Waiter interface {
	Wait(ctx context.Context) error
}

Waiter should block when Wait is called, until events are allowed to happen. It should return an error if the Context is canceled, or the expected wait time exceeds the Context's Deadline.

Jump to

Keyboard shortcuts

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