Documentation ¶
Overview ¶
Package upstream provides utilities for choosing upstream backends based on score.
Index ¶
- Variables
- type Backend
- type BackendCmp
- type List
- type ListConfig
- type ListOption
- func WithHealthcheckInterval(interval time.Duration) ListOption
- func WithHealthcheckTimeout(timeout time.Duration) ListOption
- func WithInitialScore(initialScore float64) ListOption
- func WithLowHighScores(lowScore, highScore float64) ListOption
- func WithScoreDeltas(failScoreDelta, successScoreDelta float64) ListOption
- func WithTiers(min, max, init Tier) ListOption
- type Tier
Constants ¶
This section is empty.
Variables ¶
var ErrNoUpstreams = fmt.Errorf("no upstreams available")
ErrNoUpstreams is returned from Pick method, when there are no upstreams available.
Functions ¶
This section is empty.
Types ¶
type BackendCmp ¶ added in v0.3.0
type BackendCmp interface { comparable Backend }
BackendCmp is an interface which should be implemented for a Pick entry. It is similar to Backend, but used for generics.
type List ¶
type List[T Backend] struct { // contains filtered or unexported fields }
List of upstream Backends with healthchecks and different strategies to pick a node.
List keeps track of Backends with score. Score is updated on health checks, and via external interface (e.g. when actual connection fails).
Initial score is set via options (default is +1). Low and high scores defaults are (-3, +3). Backend score is limited by low and high scores. Each time healthcheck fails score is adjusted by fail delta score, and every successful check updates score by success score delta (defaults are -1/+1).
Backend might be used if its score is not negative.
func NewList ¶
func NewList[T BackendCmp](upstreams []T, options ...ListOption) (*List[T], error)
NewList initializes new list with upstream backends and options and starts health checks. It uses
List should be stopped with `.Shutdown()`.
func NewListWithCmp ¶ added in v0.3.0
func NewListWithCmp[T Backend](upstreams []T, cmp func(T, T) bool, options ...ListOption) (*List[T], error)
NewListWithCmp initializes new list with upstream backends and options and starts health checks.
List should be stopped with `.Shutdown()`.
func (*List[T]) Down ¶
func (list *List[T]) Down(upstream T)
Down decreases backend score by fail score delta.
func (*List[T]) Pick ¶
Pick returns next backend to be used.
Default policy is to pick healthy (non-negative score) backend in round-robin fashion.
type ListConfig ¶ added in v0.3.0
type ListConfig struct {
// contains filtered or unexported fields
}
ListConfig is a configuration for List. It is separated from List to allow usage of functional options without exposing type in their API.
type ListOption ¶
type ListOption func(*ListConfig) error
ListOption allows to configure List.
func WithHealthcheckInterval ¶
func WithHealthcheckInterval(interval time.Duration) ListOption
WithHealthcheckInterval configures healthcheck interval.
func WithHealthcheckTimeout ¶
func WithHealthcheckTimeout(timeout time.Duration) ListOption
WithHealthcheckTimeout configures healthcheck timeout (for each backend).
func WithInitialScore ¶
func WithInitialScore(initialScore float64) ListOption
WithInitialScore configures initial backend score.
func WithLowHighScores ¶
func WithLowHighScores(lowScore, highScore float64) ListOption
WithLowHighScores configures low and high score.
func WithScoreDeltas ¶
func WithScoreDeltas(failScoreDelta, successScoreDelta float64) ListOption
WithScoreDeltas configures fail and success score delta.
func WithTiers ¶ added in v0.3.0
func WithTiers(min, max, init Tier) ListOption
WithTiers configures backend tier min, max, and start.