roundrobin

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2022 License: Apache-2.0 Imports: 11 Imported by: 200

Documentation

Overview

Package roundrobin implements dynamic weighted round robin load balancer http handler

Index

Constants

View Source
const (
	// FSMMaxWeight is the maximum weight that handler will set for the server.
	FSMMaxWeight = 4096
	// FSMGrowFactor Multiplier for the server weight.
	FSMGrowFactor = 4
)

Variables

This section is empty.

Functions

func SetDefaultWeight

func SetDefaultWeight(weight int) error

SetDefaultWeight sets the default server weight.

Types

type CookieOptions

type CookieOptions struct {
	HTTPOnly bool
	Secure   bool

	Path    string
	Domain  string
	Expires time.Time

	MaxAge   int
	SameSite http.SameSite
}

CookieOptions has all the options one would like to set on the affinity cookie.

type LBOption

type LBOption func(*RoundRobin) error

LBOption provides options for load balancer.

func EnableStickySession

func EnableStickySession(stickySession *StickySession) LBOption

EnableStickySession enable sticky session.

func ErrorHandler

func ErrorHandler(h utils.ErrorHandler) LBOption

ErrorHandler is a functional argument that sets error handler of the server.

func Logger added in v1.4.0

func Logger(l *log.Logger) LBOption

Logger defines the logger the round robin load balancer will use.

It defaults to logrus.StandardLogger(), the global logger used by logrus.

func RoundRobinLogger

func RoundRobinLogger(l *log.Logger) LBOption

RoundRobinLogger defines the logger the round robin load balancer will use.

It defaults to logrus.StandardLogger(), the global logger used by logrus. Deprecated: use Logger instead.

func RoundRobinRequestRewriteListener

func RoundRobinRequestRewriteListener(rrl RequestRewriteListener) LBOption

RoundRobinRequestRewriteListener is a functional argument that sets error handler of the server.

type Meter

type Meter interface {
	Rating() float64
	Record(int, time.Duration)
	IsReady() bool
}

Meter measures server performance and returns it's relative value via rating.

type NewMeterFn

type NewMeterFn func() (Meter, error)

NewMeterFn type of functions to create new Meter.

type Rebalancer

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

Rebalancer increases weights on servers that perform better than others. It also rolls back to original weights if the servers have changed. It is designed as a wrapper on top of the roundrobin.

func NewRebalancer

func NewRebalancer(handler balancerHandler, opts ...RebalancerOption) (*Rebalancer, error)

NewRebalancer creates a new Rebalancer.

func (*Rebalancer) RemoveServer

func (rb *Rebalancer) RemoveServer(u *url.URL) error

RemoveServer remove a server.

func (*Rebalancer) ServeHTTP

func (rb *Rebalancer) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Rebalancer) Servers

func (rb *Rebalancer) Servers() []*url.URL

Servers gets all servers.

func (*Rebalancer) UpsertServer

func (rb *Rebalancer) UpsertServer(u *url.URL, options ...ServerOption) error

UpsertServer upsert a server.

func (*Rebalancer) Wrap

func (rb *Rebalancer) Wrap(next balancerHandler) error

Wrap sets the next handler to be called by rebalancer handler.

type RebalancerOption

type RebalancerOption func(*Rebalancer) error

RebalancerOption - functional option setter for rebalancer.

func RebalancerBackoff

func RebalancerBackoff(d time.Duration) RebalancerOption

RebalancerBackoff sets a beck off duration.

func RebalancerErrorHandler

func RebalancerErrorHandler(h utils.ErrorHandler) RebalancerOption

RebalancerErrorHandler is a functional argument that sets error handler of the server.

func RebalancerLogger

func RebalancerLogger(l *log.Logger) RebalancerOption

RebalancerLogger defines the logger the rebalancer will use.

It defaults to logrus.StandardLogger(), the global logger used by logrus.

func RebalancerMeter

func RebalancerMeter(newMeter NewMeterFn) RebalancerOption

RebalancerMeter sets a Meter builder function.

func RebalancerRequestRewriteListener

func RebalancerRequestRewriteListener(rrl RequestRewriteListener) RebalancerOption

RebalancerRequestRewriteListener is a functional argument that sets error handler of the server.

func RebalancerStickySession

func RebalancerStickySession(stickySession *StickySession) RebalancerOption

RebalancerStickySession sets a sticky session.

type RequestRewriteListener

type RequestRewriteListener func(oldReq *http.Request, newReq *http.Request)

RequestRewriteListener function to rewrite request.

type RoundRobin

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

RoundRobin implements dynamic weighted round robin load balancer http handler.

func New

func New(next http.Handler, opts ...LBOption) (*RoundRobin, error)

New created a new RoundRobin.

func (*RoundRobin) Next

func (r *RoundRobin) Next() http.Handler

Next returns the next handler.

func (*RoundRobin) NextServer

func (r *RoundRobin) NextServer() (*url.URL, error)

NextServer gets the next server.

func (*RoundRobin) RemoveServer

func (r *RoundRobin) RemoveServer(u *url.URL) error

RemoveServer remove a server.

func (*RoundRobin) ServeHTTP

func (r *RoundRobin) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*RoundRobin) ServerWeight

func (r *RoundRobin) ServerWeight(u *url.URL) (int, bool)

ServerWeight gets the server weight.

func (*RoundRobin) Servers

func (r *RoundRobin) Servers() []*url.URL

Servers gets servers URL.

func (*RoundRobin) UpsertServer

func (r *RoundRobin) UpsertServer(u *url.URL, options ...ServerOption) error

UpsertServer In case if server is already present in the load balancer, returns error.

type ServerOption

type ServerOption func(*server) error

ServerOption provides various options for server, e.g. weight.

func Weight

func Weight(w int) ServerOption

Weight is an optional functional argument that sets weight of the server.

type StickySession

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

StickySession is a mixin for load balancers that implements layer 7 (http cookie) session affinity.

func NewStickySession

func NewStickySession(cookieName string) *StickySession

NewStickySession creates a new StickySession.

func NewStickySessionWithOptions

func NewStickySessionWithOptions(cookieName string, options CookieOptions) *StickySession

NewStickySessionWithOptions creates a new StickySession whilst allowing for options to shape its affinity cookie such as "httpOnly" or "secure".

func (*StickySession) GetBackend

func (s *StickySession) GetBackend(req *http.Request, servers []*url.URL) (*url.URL, bool, error)

GetBackend returns the backend URL stored in the sticky cookie, iff the backend is still in the valid list of servers.

func (*StickySession) SetCookieValue added in v1.3.0

func (s *StickySession) SetCookieValue(value stickycookie.CookieValue) *StickySession

SetCookieValue set the CookieValue for the StickySession.

func (*StickySession) StickBackend

func (s *StickySession) StickBackend(backend *url.URL, w http.ResponseWriter)

StickBackend creates and sets the cookie.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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