sd

package
v2.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2022 License: Apache-2.0 Imports: 7 Imported by: 10

Documentation

Overview

Package sd defines some interfaces and implementations for service discovery

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNoHosts = errors.New("no hosts available")

ErrNoHosts is the error the balancer must return when there are 0 hosts ready

Functions

This section is empty.

Types

type Balancer

type Balancer interface {
	Host() (string, error)
}

Balancer applies a balancing stategy in order to select the backend host to be used

func NewBalancer

func NewBalancer(subscriber Subscriber) Balancer

NewBalancer returns the best perfomant balancer depending on the number of available processors. If GOMAXPROCS = 1, it returns a round robin LB due there is no contention over the atomic counter. If GOMAXPROCS > 1, it returns a pseudo random LB optimized for scaling over the number of CPUs.

func NewRandomLB

func NewRandomLB(subscriber Subscriber) Balancer

NewRandomLB returns a new balancer using a fastrand pseudorandom generator

Example
balancer := NewRandomLB(FixedSubscriber([]string{"a", "b", "c"}))

// code required in order to make the test deterministic
{
	var counter uint32
	balancer.(*randomLB).rand = func(max uint32) uint32 {
		if max != 3 {
			fmt.Println("unexpected max:", max)
		}
		defer func() { counter++ }()
		return counter % max
	}
}

for i := 0; i < 5; i++ {
	h, err := balancer.Host()
	if err != nil {
		fmt.Println(err.Error())
		continue
	}
	fmt.Println(h)
}

// output
// a
// b
// c
// a
// b
Output:

func NewRoundRobinLB

func NewRoundRobinLB(subscriber Subscriber) Balancer

NewRoundRobinLB returns a new balancer using a round robin strategy and starting at a random position in the set of hosts.

Example
balancer := NewRoundRobinLB(FixedSubscriber([]string{"a", "b", "c"}))

// code required in order to make the test deterministic
balancer.(*roundRobinLB).counter = 1

for i := 0; i < 5; i++ {
	h, err := balancer.Host()
	if err != nil {
		fmt.Println(err.Error())
		continue
	}
	fmt.Println(h)
}

// output
// b
// c
// a
// b
// a
Output:

type FixedSubscriber

type FixedSubscriber []string

FixedSubscriber has a constant set of backend hosts and they never get updated

func NewRandomFixedSubscriber

func NewRandomFixedSubscriber(hosts []string) FixedSubscriber

NewRandomFixedSubscriber randomizes a list of hosts and builds a FixedSubscriber with it

func (FixedSubscriber) Hosts

func (s FixedSubscriber) Hosts() ([]string, error)

Hosts implements the subscriber interface

type Register

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

Register is a SD register, mapping different SD subscriber factories to their respective name, so they can be accessed by name

func GetRegister

func GetRegister() *Register

GetRegister returns the package register

func (*Register) Get

func (r *Register) Get(name string) SubscriberFactory

Get returns the SubscriberFactory stored under the given name. It falls back to a FixedSubscriberFactory if there is no factory with that name

func (*Register) Register

func (r *Register) Register(name string, sf SubscriberFactory) error

Register adds the SubscriberFactory to the internal register under the given name

type Subscriber

type Subscriber interface {
	Hosts() ([]string, error)
}

Subscriber keeps the set of backend hosts up to date

func FixedSubscriberFactory

func FixedSubscriberFactory(cfg *config.Backend) Subscriber

FixedSubscriberFactory builds a FixedSubscriber with the received config

type SubscriberFactory

type SubscriberFactory func(*config.Backend) Subscriber

SubscriberFactory builds subscribers with the received config

type SubscriberFunc

type SubscriberFunc func() ([]string, error)

SubscriberFunc type is an adapter to allow the use of ordinary functions as subscribers. If f is a function with the appropriate signature, SubscriberFunc(f) is a Subscriber that calls f.

func (SubscriberFunc) Hosts

func (f SubscriberFunc) Hosts() ([]string, error)

Hosts implements the Subscriber interface by executing the wrapped function

Directories

Path Synopsis
Package dnssrv defines some implementations for a dns based service discovery
Package dnssrv defines some implementations for a dns based service discovery

Jump to

Keyboard shortcuts

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