fanout

package
v2.4.4 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2024 License: Apache-2.0 Imports: 26 Imported by: 1

Documentation

Index

Constants

View Source
const (
	DefaultFanoutTimeout time.Duration = 45 * time.Second
	DefaultClientTimeout time.Duration = 30 * time.Second
	DefaultConcurrency                 = 1000
)

Variables

This section is empty.

Functions

func DefaultShouldTerminate

func DefaultShouldTerminate(result Result) bool

DefaultShouldTerminate is the default strategy for determining if an HTTP transaction should result in early termination of the fanout. This function returns true if the status code is a non-error HTTP status.

func MonitorEndpoints

func MonitorEndpoints(e Endpoints, options ...monitor.Option) (monitor.Interface, error)

MonitorEndpoints applies service discovery updates to the given Endpoints, if and only if the Endpoints implements monitor.Listener. If Endpoints does not implement monitor.Listener, this function return a nil monitor and a nil error.

func MustFanoutURLs

func MustFanoutURLs(e Endpoints, original *http.Request) []*url.URL

MustFanoutURLs invokes FanoutURLs on the given Endpoints instance, and panics if there's an error.

func NewChain

func NewChain(c Configuration, rf ...gokithttp.RequestFunc) alice.Chain

NewChain constructs an Alice constructor Chain from a set of fanout options and zero or more application-layer request functions. nolint:govet

func NewTransactor

func NewTransactor(c Configuration) func(*http.Request) (*http.Response, error)

NewTransactor constructs an HTTP client transaction function from a set of fanout options. nolint:govet

func ServiceEndpointsAlternate

func ServiceEndpointsAlternate(options ...ServiceEndpointsOption) func() (Endpoints, error)

ServiceEndpointsAlternate creates an alternate closure appropriate for NewEndpoints. This function allows service discovery to be used for fanout endpoints when no fixed endpoints are provided via Options.

The returned Endpoints, being a ServiceEndpoints, will implement monitor.Listener. This function does not start listening to service discovery events. That must be done by application code.

Types

type Configuration

type Configuration struct {
	// Endpoints are the URLs for each endpoint to fan out to.  If unset, the default is supplied
	// by application code, which is normally a set of endpoints driven by service discovery.
	Endpoints []string `json:"endpoints,omitempty"`

	// Authorization is the Basic Auth token.  There is no default for this field.
	Authorization string `json:"authorization"`

	// Transport is the http.Client transport
	Transport http.Transport `json:"transport"`

	// FanoutTimeout is the timeout for the entire fanout operation.  If not supplied, DefaultFanoutTimeout is used.
	FanoutTimeout time.Duration `json:"fanoutTimeout"`

	// ClientTimeout is the http.Client Timeout.  If not set, DefaultClientTimeout is used.
	ClientTimeout time.Duration `json:"clientTimeout"`

	// Concurrency is the maximum number of concurrent fanouts allowed.  If this is not set, DefaultConcurrency is used.
	Concurrency int `json:"concurrency"`

	// MaxRedirects defines the maximum number of redirects each fanout will allow
	MaxRedirects int `json:"maxRedirects"`

	// RedirectExcludeHeaders are the headers that will *not* be copied on a redirect
	RedirectExcludeHeaders []string `json:"redirectExcludeHeaders,omitempty"`

	// Tracing contains information to setup OpenTelemetry tracing on the fanout
	// HTTP client.
	Tracing candlelight.Tracing
}

Configuration defines the configuration structure for externally configuring a fanout.

type Endpoints

type Endpoints interface {
	// FanoutURLs determines the URLs that an original request should be dispatched
	// to as part of a fanout.  Each returned URL will be associated with a single http.Request
	// object and transaction.
	FanoutURLs(*http.Request) ([]*url.URL, error)
}

Endpoints is a strategy interface for determining the set of HTTP URL endpoints that a fanout should use.

func MustNewEndpoints

func MustNewEndpoints(c Configuration, alternate func() (Endpoints, error)) Endpoints

MustNewEndpoints is like NewEndpoints, save that it panics upon any error. nolint:govet

func NewEndpoints

func NewEndpoints(c Configuration, alternate func() (Endpoints, error)) (Endpoints, error)

NewEndpoints accepts a Configuration, typically injected via configuration, and an alternate function that can create an Endpoints. If the Configuration has a fixed set of endpoints, this function returns a FixedEndpoints built from those URLs. Otherwise, the alternate function is invoked to produce and Endpoints instance to return.

This function allows an application-layer Endpoints, returned by alternate, to be used when injected endpoints are not present. nolint:govet

type EndpointsFunc

type EndpointsFunc func(*http.Request) ([]*url.URL, error)

func (EndpointsFunc) FanoutURLs

func (ef EndpointsFunc) FanoutURLs(original *http.Request) ([]*url.URL, error)

type FanoutRequestFunc

type FanoutRequestFunc func(ctx context.Context, original, fanout *http.Request, body []byte) (context.Context, error)

FanoutRequestFunc is invoked to build a fanout request. It can transfer information from the original request, set the body, update the context, etc. This is the analog of go-kit's RequestFunc.

func ForwardBody

func ForwardBody(followRedirects bool) FanoutRequestFunc

ForwardBody creates a FanoutRequestFunc that sends the original request's body to each fanout. If followRedirects is true, this function also sets fanout.GetBody so that the same body is read for redirects.

This function also sets the ContentLength and Content-Type header appropriately.

func ForwardHeaders

func ForwardHeaders(headers ...string) FanoutRequestFunc

ForwardHeaders creates a FanoutRequestFunc that copies headers from the original request onto each fanout request

func ForwardVariableAsHeader

func ForwardVariableAsHeader(variable, header string) FanoutRequestFunc

ForwardVariableAsHeader returns a request function that copies the value of a gorilla/mux path variable from the original HTTP request into an HTTP header on each fanout request.

The fanout request will always have the given header. If no path variable is supplied (or no path variables are found), the fanout request will have the header associated with an empty string.

func UsePath

func UsePath(path string) FanoutRequestFunc

UsePath sets a constant URI path for every fanout request. Essentially, this replaces the original URL's Path with the configured value.

type FanoutResponseFunc

type FanoutResponseFunc func(ctx context.Context, response http.ResponseWriter, result Result) context.Context

FanoutResponseFunc is a strategy applied to the termination fanout response.

func ReturnHeaders

func ReturnHeaders(headers ...string) FanoutResponseFunc

ReturnHeaders copies zero or more headers from the fanout response into the top-level HTTP response.

func ReturnHeadersWithPrefix

func ReturnHeadersWithPrefix(headerPrefixs ...string) FanoutResponseFunc

ReturnHeadersWithPrefix copies zero or more headers from the fanout where the headerPrefix is matched in the response into the top-level HTTP response.

type FixedEndpoints

type FixedEndpoints []*url.URL

FixedEndpoints represents a set of URLs that act as base URLs for a fanout.

func MustParseURLs

func MustParseURLs(urls ...string) FixedEndpoints

MustParseURLs is like ParseURLs, except that it panics instead of returning an error.

func ParseURLs

func ParseURLs(values ...string) (FixedEndpoints, error)

ParseURLs parses each URL to produce a FixedEndpoints. Each supplied URL should have a scheme instead of being abbreviated, e.g. "http://hostname" or "http://hostname:1234" instead of "hostname" or "hostname:1234"

func (FixedEndpoints) FanoutURLs

func (fe FixedEndpoints) FanoutURLs(original *http.Request) ([]*url.URL, error)

type Handler

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

Handler is the http.Handler that fans out HTTP requests using the configured Endpoints strategy.

func New

func New(e Endpoints, options ...Option) *Handler

New creates a fanout Handler. The Endpoints strategy is required, and this constructor function will panic if it is nil.

By default, all fanout requests have the same HTTP method as the original request, but no body is set.. Clients must use the OriginalBody strategy to set the original request's body on each fanout request.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(response http.ResponseWriter, original *http.Request)

type Option

type Option func(*Handler)

Option provides a single configuration option for a fanout Handler

func WithClientAfter

func WithClientAfter(after ...gokithttp.ClientResponseFunc) Option

WithClientAfter allows zero or more go-kit ClientResponseFuncs to be used as fanout after functions.

func WithClientBefore

func WithClientBefore(before ...gokithttp.RequestFunc) Option

WithClientBefore adds zero or more go-kit RequestFunc functions that will be applied to each fanout request.

func WithClientFailure

func WithClientFailure(failure ...gokithttp.ClientResponseFunc) Option

WithClientFailure allows zero or more go-kit ClientResponseFuncs to be used as fanout failure functions.

func WithConfiguration

func WithConfiguration(c Configuration) Option

WithConfiguration uses a set of (typically injected) fanout configuration options to configure a Handler. Use of this option will not override the configured Endpoints instance.

func WithErrorEncoder

func WithErrorEncoder(encoder gokithttp.ErrorEncoder) Option

WithErrorEncoder configures a custom error encoder for errors that occur during fanout setup. If encoder is nil, go-kit's DefaultErrorEncoder is used.

func WithFanoutAfter

func WithFanoutAfter(after ...FanoutResponseFunc) Option

WithFanoutAfter adds zero or more response functions that are invoked to tailor the response when a successful (i.e. terminating) fanout response is received.

func WithFanoutBefore

func WithFanoutBefore(before ...FanoutRequestFunc) Option

WithFanoutBefore adds zero or more request functions that will tailor each fanout request.

func WithFanoutFailure

func WithFanoutFailure(failure ...FanoutResponseFunc) Option

WithFanoutFailure adds zero or more response functions that are invoked to tailor the response when a failed fanout responses have been received.

func WithShouldTerminate

func WithShouldTerminate(terminate ShouldTerminateFunc) Option

WithShouldTerminate configures a custom termination predicate for the fanout. If terminate is nil, DefaultShouldTerminate is used.

func WithTransactor

func WithTransactor(transactor func(*http.Request) (*http.Response, error)) Option

WithTransactor configures a custom HTTP client transaction function. If transactor is nil, http.DefaultClient.Do is used as the transactor.

type Result

type Result struct {
	// StatusCode is the HTTP status code from the response, or an inferred status code
	// if the transaction returned an error.  This value will be populated even if Response is nil.
	StatusCode int

	// Request is the HTTP request sent to the fanout endpoint.  This will always be non-nil.
	Request *http.Request

	// Response is the HTTP response returned by the fanout HTTP transaction.  If set, Err will be nil.
	Response *http.Response

	// Err is the error returned by the fanout HTTP transaction.  If set, Response will be nil.
	Err error

	// ContentType is the MIME type of the Body
	ContentType string

	// Body is the HTTP response entity returned by the fanout HTTP transaction.  This can be nil or empty.
	Body []byte

	// Span represents the execution block that handled this fanout transaction
	Span tracing.Span
}

Result is the result from a single fanout HTTP transaction

type ServiceEndpoints

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

ServiceEndpoints is an Endpoints implementation that is driven by service discovery. This type is a monitor.Listener, and fanout URLs are computed from all configured instancers.

func NewServiceEndpoints

func NewServiceEndpoints(options ...ServiceEndpointsOption) *ServiceEndpoints

NewServiceEndpoints creates a ServiceEndpoints instance. By default, device.IDHashParser is used as the KeyFunc and service.DefaultAccessorFactory is used as the accessor factory.

func (*ServiceEndpoints) FanoutURLs

func (se *ServiceEndpoints) FanoutURLs(original *http.Request) ([]*url.URL, error)

FanoutURLs uses the currently available discovered endpoints to produce a set of URLs. The original request is used to produce a hash key, then each accessor is consulted for the endpoint that matches that key.

func (*ServiceEndpoints) MonitorEvent

func (se *ServiceEndpoints) MonitorEvent(e monitor.Event)

MonitorEvent supplies the monitor.Listener behavior. An accessor is created and stored under the event Key.

type ServiceEndpointsOption

type ServiceEndpointsOption func(*ServiceEndpoints)

ServiceEndpointsOption is a strategy for configuring a ServiceEndpoints

func WithAccessorFactory

func WithAccessorFactory(af service.AccessorFactory) ServiceEndpointsOption

WithAccessorFactory configures an accessor factory for the given service endpoints. If nil, then service.DefaultAccessorFactory is used.

func WithKeyFunc

WithKeyFunc configures a hash function for the given service endpoints. If nil, then device.IDHashParser is used.

type ShouldTerminateFunc

type ShouldTerminateFunc func(Result) bool

ShouldTerminateFunc is a predicate for determining if a fanout should terminate early given the results of a single HTTP transaction.

Jump to

Keyboard shortcuts

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