lb

package
v0.0.0-...-28cd0dc Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultRefreshInterval = 5 * time.Second
	DefaultExpireInterval  = 15 * time.Second
)

Variables

View Source
var DefaultLbOpts = Options{
	RefreshInterval: DefaultRefreshInterval,
	ExpireInterval:  DefaultExpireInterval,
}

Functions

func NewNetAddr

func NewNetAddr(network, address string) net.Addr

NewNetAddr creates a new NetAddr object with the network and address provided.

Types

type BalancerFactory

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

func NewBalancerFactory

func NewBalancerFactory(config Config) *BalancerFactory

NewBalancerFactory get or create a balancer with given target. If it has the same key(resolver.Target(target)), we will cache and reuse the Balance.

func (*BalancerFactory) GetInstance

func (b *BalancerFactory) GetInstance(ctx context.Context, req string, tag map[string]string) (Instance, error)

type Config

type Config struct {
	Resolver Resolver
	Balancer Loadbalancer
	LbOpts   Options
}

type Instance

type Instance interface {
	Address() net.Addr
	Weight() int
	Tag(key string) (value string, exist bool)
}

Instance contains information of an instance from the target service.

func NewInstance

func NewInstance(network, address string, weight int, tags map[string]string) Instance

NewInstance creates an Instance using the given network, address and tags

type Loadbalancer

type Loadbalancer interface {
	// Pick is used to select an instance according to discovery result
	Pick(Result) Instance

	// Rebalance is used to refresh the cache of load balance's information
	Rebalance(Result)

	// Delete is used to delete the cache of load balance's information when it is expired
	Delete(string)

	// Name returns the name of the Loadbalancer.
	Name() string
}

Loadbalancer picks instance for the given service discovery result.

func NewWeightedBalancer

func NewWeightedBalancer() Loadbalancer

type NetAddr

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

NetAddr implements the net.Addr interface.

func (*NetAddr) Network

func (na *NetAddr) Network() string

Network implements the net.Addr interface.

func (*NetAddr) String

func (na *NetAddr) String() string

String implements the net.Addr interface.

type Options

type Options struct {
	// refresh discovery result timely
	RefreshInterval time.Duration

	// Balancer expire check interval
	// we need remove idle Balancers for resource saving
	ExpireInterval time.Duration
}

Options for LoadBalance option

func (*Options) Check

func (v *Options) Check()

Check checks option's param

type Request

type Request struct {
}

type Resolver

type Resolver interface {
	// Target should return a description for the given target that is suitable for being a key for cache.
	Target(ctx context.Context, target *TargetInfo) string

	// Resolve returns a list of instances for the given description of a target.
	Resolve(ctx context.Context, desc string) (Result, error)

	// Name returns the name of the resolver.
	Name() string
}

type Result

type Result struct {
	CacheKey  string
	Instances []Instance
}

Result contains the result of service discovery process. the instance list can/should be cached and CacheKey can be used to map the instance list in cache.

type SynthesizedResolver

type SynthesizedResolver struct {
	TargetFunc  func(ctx context.Context, target *TargetInfo) string
	ResolveFunc func(ctx context.Context, key string) (Result, error)
	NameFunc    func() string
}

SynthesizedResolver synthesizes a Resolver using a resolve function.

func (SynthesizedResolver) Name

func (sr SynthesizedResolver) Name() string

Name implements the Resolver interface

func (SynthesizedResolver) Resolve

func (sr SynthesizedResolver) Resolve(ctx context.Context, key string) (Result, error)

func (SynthesizedResolver) Target

func (sr SynthesizedResolver) Target(ctx context.Context, target *TargetInfo) string

type TargetInfo

type TargetInfo struct {
	Host string
	Tags map[string]string
}

Jump to

Keyboard shortcuts

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