vtransport

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2021 License: MIT Imports: 5 Imported by: 6

README

gone/http/vtransport

A golang library providing a http.RoundTripper implementation which will send a HTTP request to a URL picked by a VirtualUpstream implementation - maybe retrying the request on failure.

A reasonable functional RoundRobin implementation of VirtualUpstream is provided.

Example

	b1, err := url.Parse("http://localhost:8001")
	b2, err := url.Parse("http://localhost:8002")

	upstream, err := rr.NewRoundRobinUpstream(
		rr.Targets(b2, b1),
		rr.MaxFails(2),
		rr.Quarantine(time.Duration(2)*time.Second))

	tr := &vtransport.VirtualTransport{
		Transport: http.Transport{
			DisableKeepAlives: true,
		},
		RetryPolicy: vtransport.NoRetry,
		Upstreams:   map[string]vtransport.VirtualUpstream{"backend": upstream},
	}
	client := &http.Client{Transport: tr}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoRetry = RetryPolicy(
	func(req *http.Request, err error, ctx RoundTripContext) bool {
		return false
	})

NoRetry never retries. Equivalient to a nil RetryPolicy

Functions

This section is empty.

Types

type RetryPolicy

type RetryPolicy func(req *http.Request, err error, ctx RoundTripContext) bool

RetryPolicy is a function deciding whether the request should be retried after a non-nil error from a call to the underlying RoundTripper

func Retries

func Retries(retries int, maxRepeat int, nonIdempotent bool) RetryPolicy

Retries creates a RetryPolicy which will at most retry the request "retries" times. and at most repeat the backend host pool "maxRepeat" times. Per default if will only retry idempotent requests

type RoundTripContext

type RoundTripContext interface {
	// Target should return the URL scheme and the host for the
	// current backend target
	Target() (scheme, host string)
	// Retries should return the number of retries which has been made
	Retries() int
	// Exhausted should return how many times retries have led to all
	// relevant backend servers being tried.
	Exhausted() int
}

RoundTripContext is an object maintained by the virtual upstream implementation which is opaque to the VirtualTransport apart from the methods specified. It should carry information enough for the VirtualUpstream to do any book keeping needed when Update() is called with the result of sending the request to the current target host.

type VirtualTransport

type VirtualTransport struct {
	*http.Transport
	Upstreams   map[string]VirtualUpstream
	RetryPolicy RetryPolicy
}

VirtualTransport acts as a replacement for the stdlib http.Transport but uses a set of named VirtualUpstream implementations to do the RoundTripper functionality if the URL scheme is "vt" and will consult the provided RetryPolicy to decide whether to retry HTTP requests which fails.

func (*VirtualTransport) RoundTrip

func (vt *VirtualTransport) RoundTrip(req *http.Request) (resp *http.Response, err error)

RoundTrip implements the http.RoundTripper interface.

type VirtualUpstream

type VirtualUpstream interface {
	// NextTarget is called by the VirtualTransport to advance the context
	// to the next backend server. The first call to NextTarget will be done
	// with a nil context and the VirtualUpstream implemetation should return
	// the first context. The context will be released again with ReleaseContext()
	NextTarget(req *http.Request, context RoundTripContext) (RoundTripContext, error)
	// Update notified the VirtualUpstream of the result from sending the HTTP request
	// to the current Target host.
	Update(context RoundTripContext, err error)
	// ReleaseContext tells the VirtualUpstream that the context is no longer needed
	ReleaseContext(RoundTripContext)
}

VirtualUpstream is an implementation of a logical upstream, which may consist of one or more actual backend network hosts, implementing the selection process to chose the actual host for a HTTP request.

Directories

Path Synopsis
upstream
rr

Jump to

Keyboard shortcuts

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