selector

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package selector provides pluggable load-balancing over a set of nodes, with optional result feedback via DoneFunc so strategies can adapt to per-node health (strategy pattern + feedback loop).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NoopDone

func NoopDone(ctx context.Context, di DoneInfo)

NoopDone is a DoneFunc that discards feedback.

func RegisterSelector

func RegisterSelector(name string, f Factory)

RegisterSelector registers a selector Factory under name. Implementations call this from init().

func Registered

func Registered(name string) bool

Registered reports whether a selector strategy is registered under name.

Types

type DoneFunc

type DoneFunc func(ctx context.Context, di DoneInfo)

DoneFunc is called after a call completes so the selector can feed the result back into its decision making.

type DoneInfo

type DoneInfo struct {
	Err           error
	BytesSent     int64
	BytesReceived int64
}

DoneInfo reports the outcome of a completed call on a selected node.

type Factory

type Factory func() Selector

Factory constructs a Selector instance.

type Node

type Node interface {
	// Address is the host:port to connect to.
	Address() string
	// Scheme is the transport scheme, e.g. "grpc" or "http".
	Scheme() string
	// ServiceName is the logical service name.
	ServiceName() string
	// Weight is the node's static weight (>=0).
	Weight() int
	// Metadata carries arbitrary tags.
	Metadata() map[string]string
}

Node abstracts a selectable instance.

func NewNode

func NewNode(address, scheme, serviceName string, weight int, metadata map[string]string) Node

NewNode builds a Node from its parts.

type Selector

type Selector interface {
	// Select returns the chosen node and a DoneFunc to report the call result.
	// It returns an error when no node is available.
	Select(ctx context.Context, nodes []Node) (Node, DoneFunc, error)
}

Selector selects a node from the candidate set.

func GetSelector

func GetSelector(name string) (Selector, error)

GetSelector returns a new Selector by name.

func WithCircuitBreaker

func WithCircuitBreaker(sel Selector, opts ...resilience.BreakerOption) Selector

WithCircuitBreaker decorates sel with per-node circuit breaking. Selection skips nodes whose breaker has tripped, and each call's result is fed back into that node's breaker via the DoneFunc, so a failing node is gradually cut off from selection and can recover through the breaker's force-pass probe. It composes with any strategy, including p2c's EWMA feedback.

Directories

Path Synopsis
Package p2c implements a power-of-two-choices selector with time-decayed EWMA latency and success tracking, inspired by go-zero's p2c_ewma.
Package p2c implements a power-of-two-choices selector with time-decayed EWMA latency and success tracking, inspired by go-zero's p2c_ewma.
Package random provides a uniformly random selector.
Package random provides a uniformly random selector.
Package roundrobin provides a round-robin selector.
Package roundrobin provides a round-robin selector.
Package weighted provides a weight-aware random selector.
Package weighted provides a weight-aware random selector.

Jump to

Keyboard shortcuts

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