selector

package
v0.94.1 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package selector 提供负载均衡的相关功能

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrNoPeer

func ErrNoPeer() error

ErrNoPeer 表示没有有效的节点

Types

type Peer

type Peer interface {
	encoding.TextMarshaler
	encoding.TextUnmarshaler

	// Addr 返回节点地址
	//
	// 返回值应该是一个有效的地址,且要满足以下条件:
	//  - 不能以 / 结尾;
	// 比如 https://example.com:8080/s1、/path。
	Addr() string
}

Peer 后端节点对象

NOTE: 节点的序列化内容中不能包含半角分号(;)。

func NewPeer

func NewPeer(addr string) Peer

type Selector

type Selector interface {
	// Next 返回下一个可用的节点地址
	Next() (string, error)
}

Selector 负载均衡接口

type Updateable

type Updateable interface {
	Selector

	// Update 更新节点信息
	//
	// 如果传递空数组,则清空节点。
	Update(...Peer)
}

Updateable 可动态更新节点信息的负载均衡接口

func NewRandom

func NewRandom(weight bool, cap int) Updateable

NewRandom 返回随机算法的负载均衡实现

weight 是否采用加权算法,如果此值为 true, 在调用 [Selector.Add] 时参数必须实现 WeightedPeer

func NewRoundRobin

func NewRoundRobin(weight bool, cap int) Updateable

NewRoundRobin 轮询法

weight 是否采用加权算法,如果此值为 true, 在调用 [Selector.Add] 时参数必须实现 WeightedPeer

type WeightedPeer

type WeightedPeer interface {
	Peer

	// Weight 节点的权重值
	Weight() int
}

WeightedPeer 带权重的节点对象

func NewWeightedPeer

func NewWeightedPeer(addr string, weight int) WeightedPeer

Jump to

Keyboard shortcuts

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