pkg

package
v0.0.0-...-3a7c5b4 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2021 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RetryID int = 1

	RetryAttempts  int           = 10
	RetryDelayInMS time.Duration = 50
)

Variables

View Source
var BackendSelectionMap = map[string]BackendSelectionMethod{
	"roundrobin":      BackendRoundRobin,
	"inuseconnection": BackendInuseConnections,
	"random":          BackendRandom,
}

Functions

func GetRetryFromContext

func GetRetryFromContext(r *http.Request) int

GetAttemptsFromContext returns the attempts for request

Types

type Backend

type Backend struct {
	Host               string
	Port               int
	BackendConnections []*BackendConnection
	MaxConnections     int

	// Is this backend alive/dead
	Alive bool
	// contains filtered or unexported fields
}

Backend is unique for a given host:port. This might be pointing to a single machine or possibly a LB/cluster. The Backend has a collection of BackendConnections. These BackendConnections are the REAL connections to the given target machine

func NewBackend

func NewBackend(host string, port int, maxConnections int) *Backend

func (*Backend) GetBackendConnection

func (ber *Backend) GetBackendConnection() (*BackendConnection, error)

GetBackendConnection either retrieves BackendConnection from a pool OR adds new entry to pool (or errors out)

func (*Backend) IsAlive

func (b *Backend) IsAlive() bool

func (*Backend) LogStats

func (ber *Backend) LogStats() error

LogStats... just a hack to get some data. Log stats (used connections etc).

func (*Backend) SetIsAlive

func (b *Backend) SetIsAlive(alive bool)

type BackendConfig

type BackendConfig struct {
	Host           string `json:"host"`
	Port           int    `json:"port"`
	MaxConnections int    `json:"maxconnections"`
}

type BackendConnection

type BackendConnection struct {
	InUse bool

	ReverseProxy *httputil.ReverseProxy
	// contains filtered or unexported fields
}

BackendConnection has the ReverseProxy to the real backend server. There may be many BackendConnections going from the same Backend to the real host/IP. Each BackendConnection will be in it's own goroutine (probably).

func NewBackendConnection

func NewBackendConnection(uri string) *BackendConnection

func (*BackendConnection) IsInUse

func (b *BackendConnection) IsInUse() bool

func (*BackendConnection) SetInUse

func (b *BackendConnection) SetInUse(inUse bool)

type BackendRouter

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

BackendRouter is in control of a particular path (eg. /foo). The BackendRouter has a collection of Backends. Each Backend is a unique host/ip (could be single machine or another cluster/LB etc). The BackendRouter determines which Backend should receive the request, this could be based on random/round-robin/load-tracking/wild-guess etc.

func NewBackendRouter

func NewBackendRouter(acceptedHeaders map[string]string, acceptedPaths map[string]bool, bes BackendSelectionMethod) *BackendRouter

func (*BackendRouter) AddBackend

func (ber *BackendRouter) AddBackend(backend *Backend) error

AddBackend adds backend to router.

func (*BackendRouter) GetBackend

func (ber *BackendRouter) GetBackend() (*Backend, error)

GetBackend either retrieves backend from a pool OR adds new entry to pool (or errors out) This needs to be based on random/load/wild-guess/spirits....

type BackendRouterConfig

type BackendRouterConfig struct {
	SelectionMethod string            `json:"SelectionMethod"`
	AcceptedPaths   []string          `json:"AcceptedPaths,omitempty"`
	AcceptedHeaders map[string]string `json:"AcceptedHeaders,omitempty"`
	BackendConfigs  []BackendConfig   `json:"BackendConfigs,omitempty"`
}

type BackendSelectionMethod

type BackendSelectionMethod int
const (
	BackendRoundRobin       BackendSelectionMethod = 1
	BackendInuseConnections BackendSelectionMethod = 2
	BackendRandom           BackendSelectionMethod = 3
)

func ParseBackendSelectionString

func ParseBackendSelectionString(bes string) BackendSelectionMethod

type Config

type Config struct {
	HealthCheckTimerInSeconds int                   `json:"HealthCheckTimerInSeconds"`
	CertCrtPath               string                `json:"certcrtpath"`
	CertKeyPath               string                `json:"certkeypath"`
	Host                      string                `json:"host"`
	Port                      int                   `json:"port"`
	TlsListener               bool                  `json:"tlslistener"`
	BackendRouterConfigs      []BackendRouterConfig `json:"BackendRouterConfigs"`
}

func LoadConfig

func LoadConfig(filePath string) Config

LoadConfig, loads configuation for LBLight. Primarily backend host, port, paths etc.

type LBLight

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

LBLight is the core of the load balancer. Listens to port, parses both headers and request paths and determines (based on configuration) where the request should be forwarded on to. All WIP and learning.

func NewLBLight

func NewLBLight(port int, tlsListener bool) *LBLight

func (*LBLight) AddBackendRouter

func (l *LBLight) AddBackendRouter(ber *BackendRouter) error

AddBackendRouter register a BackendRouter to both pathPrefix map and header maps for lookup at runtime. If we have multiple, then we'd definitely NOT know who the request really should go to. If any of the paths/headers fail for thie BER, then fail them all.

func (*LBLight) CheckHealthOfAllBackendRouters

func (l *LBLight) CheckHealthOfAllBackendRouters() error

checkHealthOfAllBackendRouters loops through all BackendRouters, in turn each BackendRouter will check each backend and attempt a connection... to determine health.

func (*LBLight) GetBackendRouterByExactPathPrefix

func (l *LBLight) GetBackendRouterByExactPathPrefix(path string) (*BackendRouter, error)

GetBackendRouterByExactPathPrefix returns the backend router which is registered for the exact match of "path". This is more for registration.

func (*LBLight) GetBackendRouterByHeader

func (l *LBLight) GetBackendRouterByHeader(headerName string, headerValue string) (*BackendRouter, error)

func (*LBLight) GetBackendRouterByPathPrefix

func (l *LBLight) GetBackendRouterByPathPrefix(path string) (*BackendRouter, error)

GetBackendRouterByPathPrefix Checks all routers that have been registered for path prefixes and searches each registered BackendRouter for a prefix match. This means it's NOT just a map lookup but iterating over all of them looking for prefix matches. May need to rethink this a bit.

func (*LBLight) GetBackendStats

func (l *LBLight) GetBackendStats() error

GetBackendStats just a hacky get stats/connection and logs it. will be replaced by prometheus/whatever metrics.

func (*LBLight) ListenAndServeTraffic

func (l *LBLight) ListenAndServeTraffic(certCRTPath string, certKeyPath string) error

Jump to

Keyboard shortcuts

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