selector

package
v0.5.8 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2023 License: MIT Imports: 6 Imported by: 0

README

server

builder 接口面临哪些问题需要解决:
1. 当grpc服务器只有一个的时候,如何处理?
2. 当grpc服务器有多个的时候,如何处理?
3. 当grpc服务器负载不同的时候如何处理? 或者说: 当多个链接都处于ready状态的时候,应该如何选择?
4. 当链接失败的时候,如何处理? 是否启动重试等等
grpc为了解决这些问题, 把链路分为不同的阶段:
balancer构建的阶段
子链接具体的连接阶段: 一个grpc服务器地址对应一个连接, 多个地址的时候就会有多个子连接
子连接的选择问题(picker接口完成)
balancer状态
链路创建, 删除,更新

负载均衡相关的接口:
1. Builder接口: 用于构建一个balancer接口实例 - 重点!!
2. SubConn接口: 主要负责具体的连接
3. Picker接口: 主要负责从众多的连接李,按照负载均衡算法选择一个连接供客户端使用 - 最重点!!
4. Balancer接口: 主要负责更新clientConn状态, 更新subConn状态 - 重点!!
5. ClientConn接口: 主要负责链路的维护, 包括创建一个子链路, 删除一个子链路, 更新ClientConn状态

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoAvailable = errors.New("no_available_node")

ErrNoAvailable is no available node.

Functions

func NewPeerContext

func NewPeerContext(ctx context.Context, p *Peer) context.Context

NewPeerContext creates a new context with peer information attached.

func SetGlobalSelector

func SetGlobalSelector(builder Builder)

SetGlobalSelector set global selector builder.

Types

type Balancer

type Balancer interface {
	Pick(ctx context.Context, nodes []WeightedNode) (selected WeightedNode, done DoneFunc, err error)
}

Balancer is balancer interface

type BalancerBuilder

type BalancerBuilder interface {
	Build() Balancer
}

BalancerBuilder build balancer

type Builder

type Builder interface {
	Build() Selector
}

Builder build selector

func GlobalSelector

func GlobalSelector() Builder

GlobalSelector returns global selector builder.

type Default

type Default struct {
	NodeBuilder WeightedNodeBuilder
	Balancer    Balancer
	// contains filtered or unexported fields
}

Default is composite selector.

func (*Default) Apply

func (d *Default) Apply(nodes []Node)

Apply update nodes info.

func (*Default) Select

func (d *Default) Select(ctx context.Context) (selected Node, done DoneFunc, err error)

Select is select one node.

type DefaultBuilder

type DefaultBuilder struct {
	Node     WeightedNodeBuilder
	Balancer BalancerBuilder
}

DefaultBuilder is de

func (*DefaultBuilder) Build

func (db *DefaultBuilder) Build() Selector

Build create builder

type DefaultNode

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

DefaultNode is selector node

func (*DefaultNode) Address

func (n *DefaultNode) Address() string

Address is node address

func (*DefaultNode) InitialWeight

func (n *DefaultNode) InitialWeight() *int64

InitialWeight is node initialWeight

func (*DefaultNode) Metadata

func (n *DefaultNode) Metadata() map[string]string

Metadata is node metadata

func (*DefaultNode) Scheme

func (n *DefaultNode) Scheme() string

Scheme is node scheme

func (*DefaultNode) ServiceName

func (n *DefaultNode) ServiceName() string

ServiceName is node serviceName

func (*DefaultNode) Version

func (n *DefaultNode) Version() string

Version is node version

type DoneFunc

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

DoneFunc is callback function when RPC invoke done.

type DoneInfo

type DoneInfo struct {
	// Response Error
	Err error
	// Response Metadata
	ReplyMD ReplyMD

	// BytesSent indicates if any bytes have been sent to the server.
	BytesSent bool
	// BytesReceived indicates if any byte has been received from the server.
	BytesReceived bool
}

DoneInfo is callback info when RPC invoke done.

type Node

type Node interface {
	// Scheme is service node scheme
	Scheme() string

	// Address is the unique address under the same service
	Address() string

	// ServiceName is service name
	ServiceName() string

	// InitialWeight is the initial value of scheduling weight
	// if not set return nil
	InitialWeight() *int64

	// Version is service node version
	Version() string

	// Metadata is the kv pair metadata associated with the service instance.
	// version,namespace,region,protocol etc..
	Metadata() map[string]string
}

Node is node interface.

func NewNode

func NewNode(scheme, addr string, ins *registry.ServiceInstance) Node

NewNode new node

type Peer

type Peer struct {
	// node is the peer node.
	Node Node
}

Peer contains the information of the peer for an RPC, such as the address and authentication information.

func FromPeerContext

func FromPeerContext(ctx context.Context) (p *Peer, ok bool)

FromPeerContext returns the peer information in ctx if it exists.

type Rebalancer

type Rebalancer interface {
	// Apply is apply all nodes when any changes happen
	Apply(nodes []Node)
}

Rebalancer is nodes rebalancer.

type ReplyMD

type ReplyMD interface {
	Get(key string) string
}

ReplyMD is Reply Metadata.

type Selector

type Selector interface {
	Rebalancer

	// Select nodes
	// if err == nil, selected and done must not be empty.
	Select(ctx context.Context) (selected Node, done DoneFunc, err error)
}

Selector is node pick balancer.

type WeightedNode

type WeightedNode interface {
	Node

	// Raw returns the original node
	Raw() Node

	// Weight is the runtime calculated weight
	Weight() float64

	// Pick the node
	Pick() DoneFunc

	// PickElapsed is time elapsed since the latest pick
	PickElapsed() time.Duration
}

WeightedNode calculates scheduling weight in real time

type WeightedNodeBuilder

type WeightedNodeBuilder interface {
	Build(Node) WeightedNode
}

WeightedNodeBuilder is WeightedNode Builder

Directories

Path Synopsis
node

Jump to

Keyboard shortcuts

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