transport

package module
v0.0.0-...-327d1f2 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

README

transport

We've moved to https://github.com/asecurityteam/transport.

This repository is read-only and maintained to support existing users. New development will happen in the new location.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts ...Option) *http.Transport

New applies the given options to a Transport and returns it.

func NewHeader

func NewHeader(provider HeaderProvider) func(http.RoundTripper) http.RoundTripper

NewHeader wraps a transport in order to include custom headers.

func NewHedger

func NewHedger(backoffPolicy BackoffPolicy) func(http.RoundTripper) http.RoundTripper

NewHedger configures a RoundTripper decorator to perform some number of hedged requests.

func NewRetrier

func NewRetrier(backoffPolicy BackoffPolicy, retryPolicies ...RetryPolicy) func(http.RoundTripper) http.RoundTripper

NewRetrier configures a RoundTripper decorator to perform some number of retries.

func OptionDefaultTransport

func OptionDefaultTransport(t *http.Transport) *http.Transport

OptionDefaultTransport configures a transport to match the http.DefaultTransport.

Types

type BackoffPolicy

type BackoffPolicy func() Backoffer

BackoffPolicy is a factory that generates a Backoffer.

func NewFixedBackoffPolicy

func NewFixedBackoffPolicy(wait time.Duration) BackoffPolicy

NewFixedBackoffPolicy generates a BackoffPolicy that always returns the same value.

func NewPercentJitteredBackoffPolicy

func NewPercentJitteredBackoffPolicy(wrapped BackoffPolicy, jitterPercent float64) BackoffPolicy

NewPercentJitteredBackoffPolicy wraps any backoff policy and applies a percentage based jitter to the original policy's value. The percentage float should be between 0 and 1. The jitter will be applied as a positive and negative value equally.

type Backoffer

type Backoffer interface {
	Backoff(*http.Request, *http.Response, error) time.Duration
}

Backoffer determines how much time to wait in between automated retires.

type Chain

type Chain []Decorator

Chain is an ordered collection of Decorators.

func (Chain) Apply

func (c Chain) Apply(base http.RoundTripper) http.RoundTripper

Apply wraps the given RoundTripper with the Decorator chain.

func (Chain) ApplyFactory

func (c Chain) ApplyFactory(base Factory) Factory

ApplyFactory wraps the given Factory such that all new instances produced will be decorated with the contents of the chain.

type Decorator

type Decorator func(http.RoundTripper) http.RoundTripper

Decorator is a named type for any function that takes a RoundTripper and returns a RoundTripper.

type Factory

type Factory func() http.RoundTripper

Factory is any function that takes no arguments and returns a Transport.

func NewFactory

func NewFactory(opts ...Option) Factory

NewFactory returns a Factory that is bound to the given Option set.

func NewRecyclerFactory

func NewRecyclerFactory(factory Factory, opts ...RecycleOption) Factory

NewRecyclerFactory is a counterpart for NewRecycler that generates a Factory function for use with other decorators.

func NewRotatorFactory

func NewRotatorFactory(factory Factory, opts ...RotatorOption) Factory

NewRotatorFactory is a counterpart for NewRotator that generates a Factory function for use with other decorators.

type FixedBackoffer

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

FixedBackoffer signals the client to wait for a static amount of time.

func (*FixedBackoffer) Backoff

Backoff for a static amount of time.

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

Header is a decorator that injects a header value into every request.

func (*Header) RoundTrip

func (c *Header) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip annotates the outgoing request and calls the wrapped Client.

type HeaderProvider

type HeaderProvider func(*http.Request) (headerName string, headerValue string)

HeaderProvider is mapping function that generates the required header name and value from an outgoing request.

type Hedger

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

Hedger is a wrapper that fans out a new request at each time interval defined by the backoff policy, and returns the first response received. For latency-based retries, this will often be a better approach than a "stop-and-retry" policy (such as the TimeoutRetrier). The hedging decorator allows for a worst case request to take up to a maximum configurable timeout, while pessimistically creating new requests before the timeout is reached.

func (*Hedger) RoundTrip

func (c *Hedger) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip executes a new request at each time interval defined by the backoff policy, and returns the first response received.

type LimitedRetrier

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

LimitedRetrier wraps a series of retry policies in a hard upper limit.

func (*LimitedRetrier) Request

func (r *LimitedRetrier) Request(req *http.Request) *http.Request

Request implements Requester by calling the wrapped Request methods where needed.

func (*LimitedRetrier) Retry

func (r *LimitedRetrier) Retry(req *http.Request, resp *http.Response, e error) bool

Retry the request based on the wrapped policies until the limit is reached. Once the limit is reached then this method always returns false.

type Option

type Option func(*http.Transport) *http.Transport

Option is a function that either modifies or generates a new Transport.

func OptionDial

func OptionDial(dial func(network, addr string) (net.Conn, error)) Option

OptionDial installs a custom Dial configuration in the Transport.

func OptionDialContext

func OptionDialContext(dialCtx func(ctx context.Context, network, addr string) (net.Conn, error)) Option

OptionDialContext installs a custom DialContext configuration in the Transport.

func OptionDialTLS

func OptionDialTLS(dial func(network, addr string) (net.Conn, error)) Option

OptionDialTLS installs a custom DialTLS configuration in the Transport.

func OptionDisableCompression

func OptionDisableCompression(disabled bool) Option

OptionDisableCompression installs a custom DisableCompression option in the Transport.

func OptionDisableKeepAlives

func OptionDisableKeepAlives(disabled bool) Option

OptionDisableKeepAlives installs a custom DisableKeepAlives option in the Transport.

func OptionExpectContinueTimeout

func OptionExpectContinueTimeout(timeout time.Duration) Option

OptionExpectContinueTimeout installs a custom ExpectContinueTimeout option in the Transport.

func OptionIdleConnTimeout

func OptionIdleConnTimeout(timeout time.Duration) Option

OptionIdleConnTimeout installs a custom IdleConnTimeout option in the Transport.

func OptionMaxIdleConns

func OptionMaxIdleConns(max int) Option

OptionMaxIdleConns installs a custom MaxIdleConns option in the Transport.

func OptionMaxIdleConnsPerHost

func OptionMaxIdleConnsPerHost(max int) Option

OptionMaxIdleConnsPerHost installs a custom MaxIdleConnsPerHost option in the Transport.

func OptionMaxResponseHeaderBytes

func OptionMaxResponseHeaderBytes(max int64) Option

OptionMaxResponseHeaderBytes installs a custom MaxResponseHeaderBytes option in the Transport.

func OptionProxy

func OptionProxy(proxy func(*http.Request) (*url.URL, error)) Option

OptionProxy installs a custom Proxy configuration in the Transport.

func OptionProxyConnectHeader

func OptionProxyConnectHeader(header http.Header) Option

OptionProxyConnectHeader installs a custom ProxyConnectHeader option in the Transport.

func OptionResponseHeaderTimeout

func OptionResponseHeaderTimeout(timeout time.Duration) Option

OptionResponseHeaderTimeout installs a custom ResponseHeaderTimeout option in the Transport.

func OptionTLSClientConfig

func OptionTLSClientConfig(config *tls.Config) Option

OptionTLSClientConfig installs a custom TLSClientConfig in the Transport.

func OptionTLSHandshakeTimeout

func OptionTLSHandshakeTimeout(timeout time.Duration) Option

OptionTLSHandshakeTimeout installs a custom TLSHandshakeTimeout in the Transport.

func OptionTLSNextProto

func OptionTLSNextProto(next map[string]func(authority string, c *tls.Conn) http.RoundTripper) Option

OptionTLSNextProto installs a custom TLSNextProto option in the Transport.

type PercentJitteredBackoffer

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

PercentJitteredBackoffer adjusts the backoff time by a random amount within N percent of the duration to help with thundering herds.

func (*PercentJitteredBackoffer) Backoff

func (b *PercentJitteredBackoffer) Backoff(r *http.Request, response *http.Response, e error) time.Duration

Backoff for a jittered amount.

type RecycleOption

type RecycleOption func(*Recycler) *Recycler

RecycleOption is a configuration for the Recycler decorator

func RecycleOptionChannel

func RecycleOptionChannel(signal chan struct{}) RecycleOption

RecycleOptionChannel configures the recycler to rotate based on input from a channel.

func RecycleOptionMaxUsage

func RecycleOptionMaxUsage(max int) RecycleOption

RecycleOptionMaxUsage configures the recycler to rotate Transports after a number of uses.

func RecycleOptionTTL

func RecycleOptionTTL(ttl time.Duration) RecycleOption

RecycleOptionTTL configures the recycler to rotate Transports on an interval.

func RecycleOptionTTLJitter

func RecycleOptionTTLJitter(jitter time.Duration) RecycleOption

RecycleOptionTTLJitter adds a randomised jitter to the TTL that is plus or minus the duration value given.

type Recycler

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

Recycler is a decorator that discards and regenerates the transport after a given set of triggers.

func NewRecycler

func NewRecycler(factory Factory, opts ...RecycleOption) *Recycler

NewRecycler uses the given factory as a source and recycles the transport based on the options given.

func (*Recycler) RoundTrip

func (c *Recycler) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip applies the discard and regenerate policy.

type Requester

type Requester interface {
	Request(*http.Request) *http.Request
}

Requester can be implemented if the Retrier needs to manipulate the request or request context before it is executed.

type Retrier

type Retrier interface {
	Retry(*http.Request, *http.Response, error) bool
}

Retrier determines whether or not the transport will automatically retry a request.

type Retry

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

Retry is a wrapper for applying various retry policies to requests.

func (*Retry) RoundTrip

func (c *Retry) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip executes a request and applies one or more retry policies.

type RetryPolicy

type RetryPolicy func() Retrier

RetryPolicy is a factory that generates a Retrier.

func NewLimitedRetryPolicy

func NewLimitedRetryPolicy(limit int, policies ...RetryPolicy) RetryPolicy

NewLimitedRetryPolicy wraps a series of retry policies in an upper limit.

func NewStatusCodeRetryPolicy

func NewStatusCodeRetryPolicy(codes ...int) RetryPolicy

NewStatusCodeRetryPolicy generates a RetryPolicy that retries on specified status codes in the HTTP response.

func NewTimeoutRetryPolicy

func NewTimeoutRetryPolicy(timeout time.Duration) RetryPolicy

NewTimeoutRetryPolicy generates a RetryPolicy that ends a request after a given timeout duration and tries again.

type Rotator

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

Rotator contains multiple instances of a RoundTripper and rotates through them per-request. This is useful when dealing with HTTP/2 in situations where more than one TCP connection per host is required.

func NewRotator

func NewRotator(factory Factory, opts ...RotatorOption) *Rotator

NewRotator uses the given factory as a source and generates a number of instances based on the options given. The instances are called in a naive, round-robin manner.

func (*Rotator) RoundTrip

func (c *Rotator) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip round-robins the outgoing requests against all of the internal instances.

type RotatorOption

type RotatorOption func(*Rotator) *Rotator

RotatorOption is a configuration for the Rotator decorator

func RotatorOptionInstances

func RotatorOptionInstances(number int) RotatorOption

RotatorOptionInstances configurs a rotator with the number of internal RoundTripper instances it should maintain for the rotation.

type RoundTripperFunc

type RoundTripperFunc func(r *http.Request) (*http.Response, error)

RoundTripperFunc is modelled after the http.HandlerFunc and converts a function matching the RoundTrip signature to a RoundTripper implementation.

func (RoundTripperFunc) RoundTrip

func (d RoundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip calls the wrapped function.

type StatusCodeRetrier

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

StatusCodeRetrier retries based on HTTP status codes.

func (*StatusCodeRetrier) Retry

func (r *StatusCodeRetrier) Retry(req *http.Request, resp *http.Response, e error) bool

Retry the request if the response has a valid code that matches on of the codes given in the retry set.

type TimeoutRetrier

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

TimeoutRetrier applies a timeout to requests and retries if the request took longer than the timeout duration.

func (*TimeoutRetrier) Request

func (r *TimeoutRetrier) Request(req *http.Request) *http.Request

Request adds a timeout to the request context.

func (*TimeoutRetrier) Retry

func (r *TimeoutRetrier) Retry(req *http.Request, resp *http.Response, e error) bool

Retry the request if the context exceeded the deadline.

Jump to

Keyboard shortcuts

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