picker

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package picker provides functionality for picking a connection. This is used by an httplb.Client to actually select a connection for use with a given request.

This package defines the core interface, Picker, which is used to select a single connection, from multiple connections in a pool.

This package also contains numerous implementations, all in the form of various functions whose names start with "New". Each such function produces pickers that implement a particular picking algorithm, like round-robin, random, or least-loaded.

None of the provided implementations in this package make use of custom metadata (attribute.Values) for an address. But custom Picker implementations could, for example to prefer backends in clusters that are geographically closer, or to implement custom affinity policies, or even to implement weighted selection algorithms in the face of heterogeneous backends (where the name resolution/service discovery system has information about a backend's capacity).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Picker

type Picker interface {
	Pick(req *http.Request) (conn conn.Conn, whenDone func(), err error)
}

Picker implements connection selection. For a given request, it returns the connection to use. It also returns a callback that, if non-nil, will be invoked when the operation is complete. (This happens when the HTTP response is returned and its body fully consumed or closed.) Such a callback can be used, for example, to track the number of active requests for a least-loaded implementation.

func ErrorPicker

func ErrorPicker(err error) Picker

ErrorPicker returns a picker that always fails with the given error.

func NewLeastLoadedRandom

func NewLeastLoadedRandom(prev Picker, allConns conn.Conns) Picker

NewLeastLoadedRandom creates pickers that pick the connection with the fewest in-flight requests. When a tie occurs, tied hosts will be picked at random.

func NewLeastLoadedRoundRobin

func NewLeastLoadedRoundRobin(prev Picker, allConns conn.Conns) Picker

NewLeastLoadedRoundRobin creates pickers that pick the connection with the fewest in-flight requests. When a tie occurs, tied hosts will be picked in an arbitrary but sequential order.

func NewPowerOfTwo

func NewPowerOfTwo(prev Picker, allConns conn.Conns) Picker

NewPowerOfTwo creates pickers that select two connections at random and pick the one with fewer requests. This takes advantage of the power of two random choices, which provides substantial benefits over a simple random picker and, unlike the least-loaded policy, doesn't need to maintain a heap.

func NewRandom

func NewRandom(_ Picker, allConns conn.Conns) Picker

NewRandom creates pickers that picks a connections at random.

func NewRoundRobin

func NewRoundRobin(_ Picker, allConns conn.Conns) Picker

NewRoundRobin creates pickers that pick connections in a "round-robin" fashion, that is to say, in sequential order. In order to mitigate the risk of a "thundering herd" scenario, the order of connections is randomized each time the list of hosts changes.

Jump to

Keyboard shortcuts

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