registry

package
v0.0.0-...-9a2c5cd Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoAvailable = werror.Unavailable("no available service node", werror.WithID("registry.selector.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, opts ...SelectOption) (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 Discovery

type Discovery interface {
	// GetService return the service instances in memory according to the service name.
	GetService(ctx context.Context, serviceName string) ([]*ServiceInstance, error)

	// Watch creates a watcher according to the service name.
	Watch(ctx context.Context, serviceName string) (Watcher, error)
}

Discovery is service discovery.

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 *ServiceInstance) Node

NewNode new node

type NodeFilter

type NodeFilter func(context.Context, []Node) []Node

NodeFilter is select filter.

func Version

func Version(version string) NodeFilter

Version is version filter.

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 Registrar

type Registrar interface {
	// Register the registration.
	Register(ctx context.Context, service *ServiceInstance) error

	// Deregister the registration.
	Deregister(ctx context.Context, service *ServiceInstance) error
}

Registrar is service registrar.

type ReplyMD

type ReplyMD interface {
	Get(key string) string
}

ReplyMD is Reply Metadata.

type SelectOption

type SelectOption func(*SelectOptions)

SelectOption is Selector option.

func WithNodeFilter

func WithNodeFilter(fn ...NodeFilter) SelectOption

WithNodeFilter with filter options

type SelectOptions

type SelectOptions struct {
	NodeFilters []NodeFilter
}

SelectOptions is Select Options.

type Selector

type Selector interface {
	Rebalancer

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

Selector is node pick balancer.

type ServiceInstance

type ServiceInstance struct {
	// ID is the unique instance ID as registered.
	ID string `json:"id"`

	// Name is the service name as registered.
	Name string `json:"name"`

	// Version is the version of the compiled.
	Version string `json:"version"`

	// Metadata is the kv pair metadata associated with the service instance.
	Metadata map[string]string `json:"metadata"`

	// Endpoints are endpoint addresses of the service instance.
	// Schema:
	//   http://127.0.0.1:8000?isSecure=false
	//   grpc://127.0.0.1:9000?isSecure=false
	Endpoints []string `json:"endpoints"`
}

ServiceInstance is an instance of a service in a discovery system.

func (*ServiceInstance) Equal

func (i *ServiceInstance) Equal(o interface{}) bool

Equal returns whether i and o are equivalent.

func (*ServiceInstance) String

func (i *ServiceInstance) String() string

type Watcher

type Watcher interface {
	// Next returns services in the following two cases:
	// 1.the first time to watch and the service instance list is not empty.
	// 2.any service instance changes found.
	// if the above two conditions are not met, it will block until context deadline exceeded or canceled
	Next() ([]*ServiceInstance, error)

	// Stop close the watcher.
	Stop() error
}

Watcher is service watcher.

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
balancer
p2c
Package p2c helps you to to select two nodes randomly from all available nodes and then select a less loaded node based on the load of these two nodes (in other words - "Power of Two").
Package p2c helps you to to select two nodes randomly from all available nodes and then select a less loaded node based on the load of these two nodes (in other words - "Power of Two").
wrr
node
ewma
Package ewma (Exponentially-Weighted Moving Average) maintain a moving average of each replica’s round-trip time, weighted by the number of outstanding requests, and distribute traffic to replicas where that cost function is smallest.
Package ewma (Exponentially-Weighted Moving Average) maintain a moving average of each replica’s round-trip time, weighted by the number of outstanding requests, and distribute traffic to replicas where that cost function is smallest.
provider

Jump to

Keyboard shortcuts

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