selector

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultNodeBuilder 是默认的节点构建器
	DefaultNodeBuilder = func(id string, address string, metadata map[string]string) (Node, error) {
		return Node{
			ID:       id,
			Address:  address,
			Metadata: metadata,
			Weight:   100,
		}, nil
	}
)
View Source
var (
	// ErrNoAvailable 是没有可用节点的错误
	ErrNoAvailable = errors.New("no available node")
)

Functions

This section is empty.

Types

type BalancerType

type BalancerType interface {
	// Pick 从节点列表中选择一个节点
	Pick(ctx context.Context, nodes []Node) (Node, error)
}

BalancerType 是负载均衡器接口类型

type FilterFunc

type FilterFunc func(nodes []Node) []Node

FilterFunc 是节点选择过滤器

type HealthCheckParams

type HealthCheckParams struct {
	Interval      time.Duration
	Timeout       time.Duration
	MaxContinuous int
}

HealthCheckParams 是健康检查参数

type Node

type Node struct {
	// ID 是节点的唯一标识符
	ID string
	// Address 是节点的地址
	Address string
	// Metadata 是节点的元数据
	Metadata map[string]string
	// Weight 是节点的权重
	Weight int64
}

Node 是一个节点

type NodeBuilderFunc

type NodeBuilderFunc func(id string, address string, metadata map[string]string) (Node, error)

NodeBuilderFunc 构建节点

type Option

type Option func(o *options)

Option 是选择器选项

func WithBalancer

func WithBalancer(balancer BalancerType) Option

WithBalancer 选项用于设置均衡器

func WithCacheTTL

func WithCacheTTL(ttl time.Duration) Option

WithCacheTTL 选项用于设置缓存TTL

func WithFilter

func WithFilter(filters ...FilterFunc) Option

WithFilter 选项用于设置过滤器

func WithHealthCheck

func WithHealthCheck(enable bool, params HealthCheckParams) Option

WithHealthCheck 选项用于设置健康检查

func WithNodeBuilder

func WithNodeBuilder(builder NodeBuilderFunc) Option

WithNodeBuilder 选项用于设置节点构建器

func WithSubsetSize

func WithSubsetSize(size int) Option

WithSubsetSize 选项用于设置子集大小

type Random

type Random struct{}

Random 是随机负载均衡器

func (*Random) Pick

func (r *Random) Pick(_ context.Context, nodes []Node) (Node, error)

Pick 随机选择一个节点

type RoundRobin

type RoundRobin struct {
	// contains filtered or unexported fields
}

RoundRobin 是轮询负载均衡器

func (*RoundRobin) Pick

func (r *RoundRobin) Pick(_ context.Context, nodes []Node) (Node, error)

Pick 轮询选择一个节点

type Selector

type Selector interface {
	// Select 选择一个节点
	Select(ctx context.Context) (Node, error)
	// Update 更新节点列表
	Update(nodes []Node) error
	// Apply 应用过滤器
	Apply(filters ...FilterFunc)
}

Selector 是节点选择器接口

func BuildSelector

func BuildSelector(discovery registry.Discovery, serviceName string, opts ...Option) (Selector, error)

BuildSelector 从注册中心构建选择器

func NewSelector

func NewSelector(opts ...Option) Selector

NewSelector 创建一个新的选择器

type WeightedRandom

type WeightedRandom struct{}

WeightedRandom 是加权随机负载均衡器

func (*WeightedRandom) Pick

func (w *WeightedRandom) Pick(_ context.Context, nodes []Node) (Node, error)

Pick 加权随机选择一个节点

Jump to

Keyboard shortcuts

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