client

package module
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2025 License: Apache-2.0 Imports: 11 Imported by: 1

README

Client

The mt-sre/client package provides an opinionated HTTP client with optional transport wrappers to facilitate writing reliable clients.

Development

Local testing

Tests can be run locally with the following command:

./pr_check.sh

License

See LICENSE for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConstantBackoffGenerator

func ConstantBackoffGenerator(d time.Duration) func() backoff.BackOff

ConstantBackoffGenerator returns a backoff with constant intervals between retries as set with the parameter 'd'.

func ExponentialBackoffGenerator

func ExponentialBackoffGenerator(opts ...ExponentialBackoffOption) func() backoff.BackOff

ExponentialBackoffGenerator returns an ExponentialBackoff instance.

func NoBackoffGenerator

func NoBackoffGenerator() func() backoff.BackOff

NoBackoffGenerator returns a backoff which has no time interval between retries.

Types

type BackoffGenerator

type BackoffGenerator func() backoff.BackOff

BackoffGenerator always returns a new instance of backoff.Backoff to ensure a fresh backoff state for repeated reqeusts

type Client

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

func NewClient

func NewClient(opts ...ClientOption) *Client

NewClient returns an opionanted HTTP client which can be optionally augmented with TransportWrappers which add features such as retries with exponential backoff.

func (*Client) Connect

func (c *Client) Connect(ctx context.Context, url string, body io.Reader) (*http.Response, error)

Connect performs a HTTP CONNECT request against the provided URL with the given body.

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, url string) (*http.Response, error)

Delete performs a HTTP DELETE request against the provided URL.

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string) (*http.Response, error)

Get performs a HTTP GET request against the provided URL.

func (*Client) Head

func (c *Client) Head(ctx context.Context, url string) (*http.Response, error)

Head performs a HTTP HEAD request against the provided URL.

func (*Client) Options

func (c *Client) Options(ctx context.Context, url string) (*http.Response, error)

Options performs a HTTP OPTIONS request against the provided URL.

func (*Client) Patch

func (c *Client) Patch(ctx context.Context, url string, body io.Reader) (*http.Response, error)

Patch performs a HTTP PATCH request against the provided URL with the given body.

func (*Client) Post

func (c *Client) Post(ctx context.Context, url string, body io.Reader) (*http.Response, error)

Post performs a HTTP POST request against the provided URL with the given body.

func (*Client) Put

func (c *Client) Put(ctx context.Context, url string, body io.Reader) (*http.Response, error)

Put performs a HTTP PUT request against the provided URL with the given body.

func (*Client) Trace

func (c *Client) Trace(ctx context.Context, url string) (*http.Response, error)

Trace performs a HTTP TRACE request against the provided URL.

type ClientConfig

type ClientConfig struct {
	Transport http.RoundTripper
	Wrappers  []TransportWrapper
}

func (*ClientConfig) Default

func (c *ClientConfig) Default()

func (*ClientConfig) Option

func (c *ClientConfig) Option(opts ...ClientOption)

func (*ClientConfig) Wrap

func (c *ClientConfig) Wrap(client *http.Client)

type ClientOption

type ClientOption interface {
	ConfigureClient(*ClientConfig)
}

type DefaultRetryPolicy

type DefaultRetryPolicy struct{}

func NewDefaultRetryPolicy

func NewDefaultRetryPolicy() DefaultRetryPolicy

NewDefaultRetryPolicy returns the default retry policy implementation.

func (DefaultRetryPolicy) IsErrorRetryable

func (p DefaultRetryPolicy) IsErrorRetryable(err error) bool

func (DefaultRetryPolicy) IsStatusRetryableForMethod

func (p DefaultRetryPolicy) IsStatusRetryableForMethod(method string, code int) bool

type ExponentialBackoffOption

type ExponentialBackoffOption interface {
	ConfigureExponentialBackoff(*backoff.ExponentialBackOff)
}

type OAUTHConfig

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

func (*OAUTHConfig) Option

func (c *OAUTHConfig) Option(opts ...OAUTHOption)

type OAUTHOption

type OAUTHOption interface {
	ConfigureOAUTH(*OAUTHConfig)
}

type OAUTHWrapper

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

func NewOAUTHWrapper

func NewOAUTHWrapper(opts ...OAUTHOption) *OAUTHWrapper

NewOAUTHWrapper returns a TransportWrapper which adds OAUTH2 authentication to a HTTP transport.

func (*OAUTHWrapper) RoundTrip

func (w *OAUTHWrapper) RoundTrip(req *http.Request) (*http.Response, error)

func (*OAUTHWrapper) Wrap

type RetryPolicy

type RetryPolicy interface {
	// IsErrorRetryable determines which url.Error
	// instances can be retried.
	IsErrorRetryable(error) bool
	// IsStatusRetryableForMethod accepts a HTTP method
	// name and a status code and returns 'true' if a
	// given combination of the aforementioned parameters
	// should be retried.
	IsStatusRetryableForMethod(string, int) bool
}

RetryPolicy configures a RetryWrapper's logic to determine when a HTTP request is retryable.

type RetryWrapper

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

func NewRetryWrapper

func NewRetryWrapper(opts ...RetryWrapperOption) *RetryWrapper

NewRetryWrapper returns a TransportWrapper which detects whether a HTTP request should be retried given a particular failure scenario. A variadic slice of options can be provided to configure the retry behavior from default.

func (*RetryWrapper) RoundTrip

func (w *RetryWrapper) RoundTrip(req *http.Request) (*http.Response, error)

func (*RetryWrapper) Wrap

type RetryWrapperConfig

type RetryWrapperConfig struct {
	Logger          logr.Logger
	GenerateBackoff func() backoff.BackOff
	Policy          RetryPolicy
	// contains filtered or unexported fields
}

func (*RetryWrapperConfig) Default

func (c *RetryWrapperConfig) Default()

func (*RetryWrapperConfig) Option

func (c *RetryWrapperConfig) Option(opts ...RetryWrapperOption)

type RetryWrapperOption

type RetryWrapperOption interface {
	ConfigureRetryWrapper(*RetryWrapperConfig)
}

type TransportWrapper

type TransportWrapper interface {
	// Wrap return a http.RoundTripper which has been
	// wrapped with the pre and post functionality the
	// TransportWrapper provides.
	Wrap(http.RoundTripper) http.RoundTripper
}

TransportWrapper adds functionality to a http.RoundTripper by adding pre and post call execution steps.

type WithAccessToken

type WithAccessToken string

WithAccessToken configures a OAUTHWrapper with an OAUTH2 token used when making requests.

func (WithAccessToken) ConfigureOAUTH

func (at WithAccessToken) ConfigureOAUTH(c *OAUTHConfig)

type WithBackoffGenerator

type WithBackoffGenerator func() backoff.BackOff

WithBackoffGenerator configures a RetryWrapper instance with the provided BackoffGenerator.

func (WithBackoffGenerator) ConfigureRetryWrapper

func (bg WithBackoffGenerator) ConfigureRetryWrapper(c *RetryWrapperConfig)

type WithInitialInterval

type WithInitialInterval time.Duration

WithInitialInterval sets the wait time between the first and second request attempts.

func (WithInitialInterval) ConfigureExponentialBackoff

func (w WithInitialInterval) ConfigureExponentialBackoff(bo *backoff.ExponentialBackOff)

type WithLogger

type WithLogger struct{ logr.Logger }

WithLogger configures a RetryWrapper instance with the provided logr.Logger instance.

func (WithLogger) ConfigureRetryWrapper

func (l WithLogger) ConfigureRetryWrapper(c *RetryWrapperConfig)

type WithMaxElapsedTime

type WithMaxElapsedTime time.Duration

WithMaxElapsedTime sets the maximum cumulative time after which retries are no longer performed.

func (WithMaxElapsedTime) ConfigureExponentialBackoff

func (w WithMaxElapsedTime) ConfigureExponentialBackoff(bo *backoff.ExponentialBackOff)

type WithMaxRetries

type WithMaxRetries uint64

WithMaxRetries sets the maximum retry attempts for a RetryWrapper instance.

func (WithMaxRetries) ConfigureRetryWrapper

func (mr WithMaxRetries) ConfigureRetryWrapper(c *RetryWrapperConfig)

type WithMultiplier

type WithMultiplier float64

WithMultiplier sets the exponential base used for increasing backoff.

func (WithMultiplier) ConfigureExponentialBackoff

func (w WithMultiplier) ConfigureExponentialBackoff(bo *backoff.ExponentialBackOff)

type WithRandomizationFactor

type WithRandomizationFactor float64

WithRandomizationFactor sets the degree to which jitter is applied to successive retries.

func (WithRandomizationFactor) ConfigureExponentialBackoff

func (w WithRandomizationFactor) ConfigureExponentialBackoff(bo *backoff.ExponentialBackOff)

type WithTransport

type WithTransport struct{ http.RoundTripper }

WithTransport configures a Client instance with the given http.RoundTripper instance.

func (WithTransport) ConfigureClient

func (t WithTransport) ConfigureClient(c *ClientConfig)

type WithWrapper

type WithWrapper struct{ TransportWrapper }

WithWrapper configures a Client instance with the given TransportWrapper. This option can be provided multiple times to apply several TransportWrappers. The order in which the TransportWrappers is applied is important!

func (WithWrapper) ConfigureClient

func (ww WithWrapper) ConfigureClient(c *ClientConfig)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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