loadbalancer

package
v0.0.0-...-76f49ba Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package loadbalancer provides a simple TCP load balancer.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMTLSParamsMissing = errors.New("certificate, private key and CA certificate are required for mTLS")

	ErrNotTLSConnection = errors.New("not a TLS connection")

	ErrClientNameNotFound = errors.New("client name not found")

	ErrNoAuthorizedClients = errors.New("no authorized clients")

	ErrNilConnection = errors.New("nil connection")

	ErrNilClientConfig = errors.New("nil client config")

	ErrNilTargetGroupsStore = errors.New("nil target groups store")
)

Functions

func ErrClientNotAuthorized

func ErrClientNotAuthorized(clientName string) error

func ErrLoadingCACert

func ErrLoadingCACert(err error) error

func ErrLoadingKeyPair

func ErrLoadingKeyPair(err error) error

func ErrTLSHandshakeFailed

func ErrTLSHandshakeFailed(err error) error

func ErrTargetGroupNotFound

func ErrTargetGroupNotFound(targetGroupName string) error

func GetClientConfigFromConn

func GetClientConfigFromConn(
	conn net.Conn,
	authorizedClientsStore *ClientsStore,
) (ratelimit.ClientInfoInterface, error)

func GetClientName

func GetClientName(conn *tls.Conn) (string, error)

func GetNextUpstreamServer

func GetNextUpstreamServer(
	clientInfo ratelimit.ClientInfoInterface,
	targetGroupsStore *TargetGroupsStore,
) (loadbalance.UpstreamServerInterface, error)

func NewNetDialer

func NewNetDialer(timeout time.Duration, retryLimit int) loadbalance.NetDialerInterface

NewNetDialer creates a new NetDialer.

func NewTLSConfig

func NewTLSConfig(tlsConfigParams *TLSConfigParams) (*tls.Config, error)

func NewUpstreamServer

func NewUpstreamServer(address string) loadbalance.UpstreamServerInterface

Types

type ClientConfig

type ClientConfig struct {
	// ClientId is the unique identifier for the client. This should match the CN in the client's
	// certificate.
	ClientId string `yaml:"clientId"`
	// AllowedTargetGroup is the target group that the client is allowed to access.
	AllowedTargetGroup string `yaml:"allowedTargetGroup"`
	RequestsPerSecond  int    `yaml:"requestsPerSecond"`
}

ClientConfig is the configuration for the client access.

type ClientInfo

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

func NewClientInfo

func NewClientInfo(clientID, allowedTargetGroup string, maxRequestsPerWindow, maxConnections int) *ClientInfo

NewClientInfo initializes a ClientInfo with specified limits.

func (*ClientInfo) GetAllowedTargetGroup

func (c *ClientInfo) GetAllowedTargetGroup() string

func (*ClientInfo) GetClientID

func (c *ClientInfo) GetClientID() string

func (*ClientInfo) GetConnections

func (c *ClientInfo) GetConnections() int

func (*ClientInfo) GetLastWindow

func (c *ClientInfo) GetLastWindow() time.Time

func (*ClientInfo) GetMaxConnections

func (c *ClientInfo) GetMaxConnections() int

func (*ClientInfo) GetMaxRequestsPerWindow

func (c *ClientInfo) GetMaxRequestsPerWindow() int

func (*ClientInfo) GetRequestCount

func (c *ClientInfo) GetRequestCount() int

func (*ClientInfo) IncrementRequestCount

func (c *ClientInfo) IncrementRequestCount()

func (*ClientInfo) SetLastWindow

func (c *ClientInfo) SetLastWindow(now time.Time)

type ClientsStore

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

func NewClientStore

func NewClientStore() *ClientsStore

func (*ClientsStore) AddClientsFromClientConfigList

func (cs *ClientsStore) AddClientsFromClientConfigList(clients []ClientConfig)

func (*ClientsStore) GetClient

func (cs *ClientsStore) GetClient(clientId string) (ratelimit.ClientInfoInterface, bool)

func (*ClientsStore) GetClients

func (cs *ClientsStore) GetClients() map[string]ratelimit.ClientInfoInterface

type Instance

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

Instance represents an instance of the load balancer.

func NewLoadBalancer

func NewLoadBalancer(config *LoadBalancerConfig) (*Instance, error)

NewLoadBalancer creates and initializes a new load balancer instance based on the provided configuration file.

func (*Instance) GetAuthorizedClientsStore

func (i *Instance) GetAuthorizedClientsStore() *ClientsStore

func (*Instance) GetConfig

func (i *Instance) GetConfig() *LoadBalancerConfig

func (*Instance) GetTLSConfig

func (i *Instance) GetTLSConfig() *tls.Config

func (*Instance) GetTargetGroupsStore

func (i *Instance) GetTargetGroupsStore() *TargetGroupsStore

func (*Instance) Start

func (i *Instance) Start(ctx context.Context) error

Start begins listening on the configured port and handling incoming connections.

type LoadBalancerConfig

type LoadBalancerConfig struct {
	ListenAddress string              `yaml:"listenAddress"`
	TLSParams     TLSConfigParams     `yaml:"tlsParams"`
	LogLevel      string              `yaml:"logLevel"`
	TargetGroups  []TargetGroupConfig `yaml:"targetGroups"`
	Clients       []ClientConfig      `yaml:"clients"`
	Logger        *logrus.Logger
}

LoadBalancerConfig is the configuration for the load balancer.

func ParseConfig

func ParseConfig(data io.Reader) (*LoadBalancerConfig, error)

ParseConfig parses the load balancer configuration from the given file.

type NetDialer

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

NetDialer uses the standard library's net.Dial function to dial network addresses.

func (*NetDialer) Dial

func (d *NetDialer) Dial(network, address string) (net.Conn, error)

Dial makes a network connection to the specified address.

func (*NetDialer) DialTimeout

func (d *NetDialer) DialTimeout(network, address string, timeout time.Duration) (net.Conn, error)

func (*NetDialer) GetRetryLimit

func (d *NetDialer) GetRetryLimit() int

func (*NetDialer) GetTimeout

func (d *NetDialer) GetTimeout() time.Duration

type TLSConfigParams

type TLSConfigParams struct {
	// Load balancer certificate.
	Certificate string `yaml:"certificate"`
	// Load balancer private key.
	PrivateKey string `yaml:"privateKey"`
	// Root CA's certificate. Needed for self-signed certificate support.
	CACertificate string `yaml:"caCert"`
}

TLSConfigParams is the configuration for the TLS.

type TargetGroupConfig

type TargetGroupConfig struct {
	Name            string   `yaml:"name"`
	UpstreamServers []string `yaml:"upstreamServers"`
}

TargetGroupConfig is the configuration for the target group. A TargetGroup is a collection of upstream servers serving a particular application e.g. Frontend, DB etc.

type TargetGroupsStore

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

TargetGroupsStore is the store for the target groups.

func NewTargetGroupsStore

func NewTargetGroupsStore(dialer loadbalance.NetDialerInterface) *TargetGroupsStore

func (*TargetGroupsStore) AddTargetGroups

func (t *TargetGroupsStore) AddTargetGroups(targetGroups []TargetGroupConfig)

func (*TargetGroupsStore) GetNextUpstreamServer

func (t *TargetGroupsStore) GetNextUpstreamServer(targetGroupName string) (loadbalance.UpstreamServerInterface, error)

func (*TargetGroupsStore) GetTargetGroups

func (t *TargetGroupsStore) GetTargetGroups() map[string][]loadbalance.UpstreamServerInterface

func (*TargetGroupsStore) StartHealthChecks

func (t *TargetGroupsStore) StartHealthChecks(ctx context.Context, wg *sync.WaitGroup)

StartHealthChecks starts the health checks for all the target groups.

type UpstreamServer

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

UpstreamServer is a struct that represents an upstream server. It implements the loadbalance.UpstreamServerInterface.

func (*UpstreamServer) DecrementConnectionCount

func (u *UpstreamServer) DecrementConnectionCount()

func (*UpstreamServer) GetAddress

func (u *UpstreamServer) GetAddress() string

func (*UpstreamServer) GetConnectionCount

func (u *UpstreamServer) GetConnectionCount() int

func (*UpstreamServer) IncrementConnectionCount

func (u *UpstreamServer) IncrementConnectionCount()

func (*UpstreamServer) IsHealthy

func (u *UpstreamServer) IsHealthy() bool

func (*UpstreamServer) SetHealthy

func (u *UpstreamServer) SetHealthy(healthy bool)

Jump to

Keyboard shortcuts

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