balancer

package module
v0.0.0-...-63bfc48 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2020 License: MIT Imports: 8 Imported by: 0

README

balancer

Load Balance

Documentation

Index

Constants

View Source
const URLSeparator = '/'

URLSeparator define url separator

Variables

View Source
var Manager = &manager{
	balancers: new(sync.Map),
}

Manager is a struct map from name to balancer.

Functions

func HashCode

func HashCode(key string) int

HashCode generate hash code

func Register

func Register(b Builder)

Register registers the balancer builder to the balancer map. b.Name

func URLJoin

func URLJoin(urls ...string) string

URLJoin splicing url

func Unregister

func Unregister(name string)

Unregister deletes the balancer with the given name from the balancer map.

Types

type Backend

type Backend struct {
	URL       string
	State     *State
	Statistic *Statistic
	Cache     *common.Cache
}

Backend node specific information

func NewBackend

func NewBackend(url string, cacheSize int) *Backend

NewBackend creates a Backend.

type Backends

type Backends struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Backends backend node list

func NewBackends

func NewBackends() *Backends

NewBackends create a list of backend nodes

func (*Backends) Add

func (b *Backends) Add(newnode *Backend) bool

Add add backend node

func (*Backends) Delete

func (b *Backends) Delete(newnode *Backend) bool

Delete delete backend node

func (*Backends) Get

func (b *Backends) Get(key interface{}) (*Backend, bool)

Get get the backend node by index and name

func (*Backends) Len

func (b *Backends) Len() int

Len get the length of the backend node

func (*Backends) Range

func (b *Backends) Range(f func(index int, backend *Backend) bool)

Range traverse back-end nodes

type Balancer

type Balancer interface {
	Do(req *http.Request) (*http.Response, error)
	Pick() (*Backend, error)
	Backends() *Backends
	Close()
}

Balancer takes input from http, manages Backend, and collects and aggregates the connectivity states.

type Builder

type Builder interface {
	Build(client *http.Client, opts *Options) Balancer
	Name() string
}

Builder creates a balancer.

func Get

func Get(name string) Builder

Get returns the resolver builder registered with the given name.

type Doctor

type Doctor interface {
	HealthCheck()
	Ping(backend *Backend) error
}

Doctor checks the health of Backend

type DoctorBuilder

type DoctorBuilder interface {
	Build(ping PingHandler, backends *Backends) Doctor
	Name() string
}

DoctorBuilder creates balancer.Doctor.

type DoctorOptions

type DoctorOptions struct {
	Enable bool   `json:"enable" mapstructure:"enable"` //Whether to enable health check
	Type   string `json:"type" mapstructure:"type"`     //Doctor type
	Spec   string `json:"spec" mapstructure:"spec"`     //Time interval of scheduled tasks
}

DoctorOptions contains additional information for Doctor.

type DoneHandler

type DoneHandler func(info DoneInfo)

DoneHandler define the specific implementation of Done

type DoneInfo

type DoneInfo struct {
	Backend  *Backend
	Response *http.Response
	Error    error
}

DoneInfo contains additional information for done.

type FailureError

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

FailureError describe the failed error

func NewFailureError

func NewFailureError(err error) *FailureError

NewFailureError creates a fail error.

func (*FailureError) Error

func (f *FailureError) Error() string

Error return error message

type Options

type Options struct {
	Name        string           `json:"name" mapstructure:"name"`             //Balancer name
	Type        string           `json:"type" mapstructure:"type"`             //Picker type
	Timeout     int              `json:"timeout" mapstructure:"timeout"`       //http timeout, Unit: second
	CacheSize   int              `json:"cache_size" mapstructure:"cache_size"` //Node cache size
	NetParam    string           `json:"net_param" mapstructure:"net_param"`   //Node net param
	Urls        []string         `json:"urls" mapstructure:"urls"`             //Load node url
	Doctor      DoctorOptions    `json:"doctor" mapstructure:"doctor"`         //Health checker
	Statistic   StatisticOptions `json:"statistic" mapstructure:"statistic"`   //Statistics
	DoneHandler DoneHandler      `json:"-"`
	PingHandler PingHandler      `json:"-"`
}

Options contains additional information for Build.

type Picker

type Picker interface {
	Pick() (*Backend, error)
}

Picker is used by http to pick a Backend to send an http.

type PickerBuilder

type PickerBuilder interface {
	Build(backends *Backends) Picker
}

PickerBuilder creates balancer.Picker.

type PingHandler

type PingHandler func(backend *Backend) error

PingHandler define the specific implementation of Ping

type State

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

State describes the status information of the node

func (*State) AddFail

func (s *State) AddFail(err error)

AddFail add failed error

func (*State) Alive

func (s *State) Alive() bool

Alive if the backend is still alive, Alive returns true

func (*State) CleanFail

func (s *State) CleanFail(interval int64)

CleanFail clear outdated errors

func (*State) HealthCheck

func (s *State) HealthCheck(interval int64, max int) bool

HealthCheck determine whether the node is available

func (*State) LenFail

func (s *State) LenFail() int

LenFail return number of failures

func (*State) SetAlive

func (s *State) SetAlive(alive bool)

SetAlive set whether the node is available

type Statistic

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

Statistic describe statistics

func (*Statistic) Failure

func (s *Statistic) Failure() uint64

Failure return number of failures

func (*Statistic) IncFailure

func (s *Statistic) IncFailure() uint64

IncFailure auto-increment failure times

func (*Statistic) IncSuccess

func (s *Statistic) IncSuccess() uint64

IncSuccess auto-increment success times

func (*Statistic) Success

func (s *Statistic) Success() uint64

Success return number of success

type StatisticOptions

type StatisticOptions struct {
	Enable bool `json:"enable" mapstructure:"enable"` //Whether to enable statistics
	Port   int  `json:"port" mapstructure:"port"`     //Service port for obtaining statistics
}

StatisticOptions contains additional information for Statistic.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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