selector

package
v0.0.0-...-5e9c659 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound      = errors.New("not found")
	ErrNoneAvailable = errors.New("service none available")
)

Functions

func Logger

func Logger() logger.ILogger

func Register

func Register(name string, fn func(services []*registry.Service) Next)

func Use

func Use(name string) func(services []*registry.Service) Next

Types

type Config

type Config struct {
	config.Config `field:"-"`
	Name          string             `field:"-"` // config name/path in config file
	PrefixName    string             `field:"-"` // config prefix name
	Logger        logger.ILogger     `field:"-"` // 保留:提供给扩展使用
	Registry      registry.IRegistry `field:"-"`
	Strategy      Strategy           `field:"-"`
	// Other options for implementations of the interface
	// can be stored in a context
	Context context.Context `field:"-"`
}

func (*Config) Init

func (self *Config) Init(opts ...Option)

func (*Config) Load

func (self *Config) Load() error

func (*Config) Save

func (self *Config) Save(immed ...bool) error

func (*Config) String

func (self *Config) String() string

type Filter

type Filter func([]*registry.Service) []*registry.Service

Filter is used to filter a service during the selection process

func FilterEndpoint

func FilterEndpoint(name string) Filter

FilterEndpoint is an endpoint based Select Filter which will only return services with the endpoint specified.

func FilterLabel

func FilterLabel(key, val string) Filter

FilterLabel is a label based Select Filter which will only return services with the label specified.

func FilterTrasport

func FilterTrasport(tp transport.ITransport) Filter

过滤传输层

func FilterVersion

func FilterVersion(version string) Filter

FilterVersion is a version based Select Filter which will only return services with the version specified.

type ISelector

type ISelector interface {
	Init(opts ...Option) error
	Config() *Config
	Match(endpoint string, opts ...SelectOption) (Next, error)
	// Select returns a function which should return the next node
	Select(service string, opts ...SelectOption) (Next, error)
	// Mark sets the success/error against a node
	Mark(service string, node *registry.Node, err error)
	// Reset returns state back to zero for a service
	Reset(service string)
	// Close renders the selector unusable
	Close() error
	// Name of the selector
	String() string
}

Selector builds on the registry as a mechanism to pick nodes and mark their status. This allows host pools and other things to be built using various algorithms.

func Default

func Default(new ...ISelector) ISelector

func New

func New(opts ...Option) ISelector

type Next

type Next func() (*registry.Node, error)

Next is a function that returns the next node based on the selector's strategy

func Random

func Random(services []*registry.Service) Next

Random is a random strategy algorithm for node selection

func RoundRobin

func RoundRobin(services []*registry.Service) Next

RoundRobin is a roundrobin strategy algorithm for node selection

type Option

type Option func(*Config)

OptionFn configures options of server.

func Debug

func Debug() Option

func Registry

func Registry(r registry.IRegistry) Option

Registry sets the registry used by the selector

func WithConfigPrefixName

func WithConfigPrefixName(prefixName string) Option

修改Config.json的路径

func WithStrategy

func WithStrategy(name string) Option

type SelectConfig

type SelectConfig struct {
	Filters  []Filter
	Strategy Strategy

	// Other options for implementations of the interface
	// can be stored in a context
	Context context.Context
}

type SelectOption

type SelectOption func(*SelectConfig)

SelectOption used when making a select call

func WithFilter

func WithFilter(fn ...Filter) SelectOption

WithFilter adds a filter function to the list of filters used during the Select call.

type Strategy

type Strategy func([]*registry.Service) Next

Strategy is a selection strategy e.g random, round robin

Jump to

Keyboard shortcuts

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