upstream

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: MPL-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package upstream provides utilities for choosing upstream backends based on score.

Index

Constants

This section is empty.

Variables

View Source
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 Backend

type Backend interface {
	HealthCheck(ctx context.Context) (Tier, error)
}

Backend is an interface which should be implemented for a Pick entry.

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

func (list *List[T]) Pick() (T, error)

Pick returns next backend to be used.

Default policy is to pick healthy (non-negative score) backend in round-robin fashion.

func (*List[T]) Reconcile

func (list *List[T]) Reconcile(upstreams []T)

Reconcile the list of backends with passed list.

Any new backends are added with initial score, score is untouched for backends which haven't changed their score.

func (*List[T]) Shutdown

func (list *List[T]) Shutdown()

Shutdown stops healthchecks.

func (*List[T]) Up

func (list *List[T]) Up(upstream T)

Up increases backend score by success score delta.

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.

type Tier added in v0.3.0

type Tier int

Tier is a type for backend tier.

Jump to

Keyboard shortcuts

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