rr

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: 9 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRoundRobinUpstream

func NewRoundRobinUpstream(opts ...RROption) (*roundRobinUpstream, error)

NewRoundRobinUpstream returns a Round Robin VirtualUpstream configured with the provided options.

Types

type Cache

type Cache interface {
	Set(key string, value int, ttl time.Duration)
	// Get must return the value set, or negative if not found
	Get(key string) (value int)
	Delete(key string)
}

Cache is the interface needed to allow the RR upstream to remember where to send successful request for a period of time, (based on a cache key) to avoid moving requests too much around between backend servers.

type Event

type Event struct {
	Name   string
	Target *url.URL
	Err    error
}

Event reports named event with the upstream pool via a callback function. Currently named events: "quarantine", "retrying"

type PinKeyFunc

type PinKeyFunc func(req *http.Request) string

PinKeyFunc is a function which, based on a request, returns a string as a routing key for the request, to allow the Round Robin upstream to cache the picked server for requests with that routing key.

type RROption

type RROption func(*roundRobinUpstream)

RROption is an option function configuring a Round Robin VirtualUpstream

func BurstFailGrace

func BurstFailGrace(t time.Duration) RROption

BurstFailGrace sets a duration for counting multiple errors as one. This prevents backend servers which in case of an isolated incidence will kill multiple HTTP request from reaching the MaxFails limit immediately. Fails not counted due to BurstFailGrace do not count as successes though.

func EventCallback

func EventCallback(f func(Event)) RROption

EventCallback can be set to notify the application about changes to the upstream pool. This can be used for logging when a server is quarantined.

func HealthCheck

func HealthCheck(interval time.Duration, checkfunc func(*url.URL) error) (option RROption, startfunc func(context.Context) error)

HealthCheck configures a periodic health check callback. The provided checkfunc will be called with the URL of the backend target to check. checkfunc can perform any activity it wish and returning anything but a nil error, will mark the backend as down. The simple check would be to adjust the target URL to add a Request-URI and perform an HTTP request to verify it's up. If this check fails it will keep the target from being picked by the Round Robin algorithm until the next health check. The health check will run when startfunc is called until the context is canceled. This option will return nil if interval is zero.

func MaxFails

func MaxFails(m int) RROption

MaxFails configures how many times a backend server is allowed to fail before being put into quarantine. As fails are recorded for a backend it's picked less often with a chance proportional to MaxFails. A MaxFail of zero (default), means don't quarantine backend targets and don't weigh targets with fails lower when selecting next target.

func PinRequestsWith

func PinRequestsWith(cache Cache, ttl time.Duration, f PinKeyFunc) RROption

PinRequestsWith will - if provided a cache implementation - allow the Round Robin upstream to remember where it send requests with a similar routing key and keep sending them to that server. This prevents picking a new server for every single related request. The provided PinkeyFunc is called on requests to determine the routing key.

func Quarantine

func Quarantine(d time.Duration) RROption

Quarantine configures the quarantine period for failed servers.

func Targets

func Targets(urls ...*url.URL) RROption

Targets sets the backend URLs for the Round Robin upstream

Jump to

Keyboard shortcuts

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