lib

package
v0.0.0-...-6d0cf7b Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2023 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LoadBalancer

type LoadBalancer interface {
	Handler(w http.ResponseWriter, r *http.Request)
	AsyncHealthChecks()
}

LoadBalancer common inteface

type Node

type Node struct {
	Address   string `mapstructure:"address"`
	Port      string `mapstructure:"port"`
	HealthURL string `mapstructure:"health"`

	ActiveConnections int
	// contains filtered or unexported fields
}

Node ...

func (*Node) CheckHealth

func (n *Node) CheckHealth() bool

CheckHealth manually performs a health check at the node's health url. It returns true if healthy, false if unhealthy. Note: This does not set the node's `healthy:bool` field, as that's the purview of the orchestrating load-balancer.

func (*Node) Handler

func (n *Node) Handler(w http.ResponseWriter, req *http.Request) int

Handler forwards request to node. Based on https://stackoverflow.com/a/34725635

func (*Node) Init

func (n *Node) Init()

Init readies the node for requests.

func (*Node) IsHealthy

func (n *Node) IsHealthy() bool

IsHealthy returns true if healthy.

func (*Node) IsUnhealthy

func (n *Node) IsUnhealthy() bool

IsUnhealthy returns true if unhealthy.

func (*Node) SetHealthy

func (n *Node) SetHealthy()

SetHealthy sets the node to "healthy", meaning it is perceived to be able to successfully process requests.

func (*Node) SetUnhealthy

func (n *Node) SetUnhealthy()

SetUnhealthy sets the node to "unhealthy", meaning it is not perceived to be able to successfully process requests.

type RoundRobin

type RoundRobin struct {
	Nodes []*Node
	// contains filtered or unexported fields
}

RoundRobin struct contains: - A slice of node pointers - The current "active" node index - The maximum number of Healthy nodes

func NewRoundRobin

func NewRoundRobin(nodes []*Node) *RoundRobin

NewRoundRobin creates a new RoundRobin load balancer.

func (*RoundRobin) AsyncHealthChecks

func (rr *RoundRobin) AsyncHealthChecks()

AsyncHealthChecks performs health checks in the background at an interval set by asyncHealthChecksTimeSeconds.

func (*RoundRobin) Handler

func (rr *RoundRobin) Handler(w http.ResponseWriter, r *http.Request)

Handler selects a node via round robin and passes the request to the selected node.

type TwoChoice

type TwoChoice struct {
	Nodes          []*Node
	HealthyNodes   map[int]bool
	UnhealthyNodes map[int]bool
}

TwoChoice struct contains: - A slice of node pointers - A set of indexes to healthy nodes (as a map) - A set of indexes to unhealthy ndoes (as a map)

func NewTwoChoice

func NewTwoChoice(nodes []*Node) *TwoChoice

NewTwoChoice creates a new TwoChoice load balancer

func (*TwoChoice) AsyncHealthChecks

func (tc *TwoChoice) AsyncHealthChecks()

AsyncHealthChecks performs health checks in the background at an interval set by asyncHealthChecksTimeSeconds.

func (*TwoChoice) Handler

func (tc *TwoChoice) Handler(w http.ResponseWriter, r *http.Request)

Handler selects a node via random two choice and passes the request to the selected node. See https://www.nginx.com/blog/nginx-power-of-two-choices-load-balancing-algorithm/

Jump to

Keyboard shortcuts

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