Documentation ¶
Overview ¶
Package loadbalancer provides a simple TCP load balancer.
Index ¶
- Variables
- func ErrClientNotAuthorized(clientName string) error
- func ErrLoadingCACert(err error) error
- func ErrLoadingKeyPair(err error) error
- func ErrTLSHandshakeFailed(err error) error
- func ErrTargetGroupNotFound(targetGroupName string) error
- func GetClientConfigFromConn(conn net.Conn, authorizedClientsStore *ClientsStore) (ratelimit.ClientInfoInterface, error)
- func GetClientName(conn *tls.Conn) (string, error)
- func GetNextUpstreamServer(clientInfo ratelimit.ClientInfoInterface, targetGroupsStore *TargetGroupsStore) (loadbalance.UpstreamServerInterface, error)
- func NewNetDialer(timeout time.Duration, retryLimit int) loadbalance.NetDialerInterface
- func NewTLSConfig(tlsConfigParams *TLSConfigParams) (*tls.Config, error)
- func NewUpstreamServer(address string) loadbalance.UpstreamServerInterface
- type ClientConfig
- type ClientInfo
- func (c *ClientInfo) GetAllowedTargetGroup() string
- func (c *ClientInfo) GetClientID() string
- func (c *ClientInfo) GetConnections() int
- func (c *ClientInfo) GetLastWindow() time.Time
- func (c *ClientInfo) GetMaxConnections() int
- func (c *ClientInfo) GetMaxRequestsPerWindow() int
- func (c *ClientInfo) GetRequestCount() int
- func (c *ClientInfo) IncrementRequestCount()
- func (c *ClientInfo) SetLastWindow(now time.Time)
- type ClientsStore
- type Instance
- type LoadBalancerConfig
- type NetDialer
- type TLSConfigParams
- type TargetGroupConfig
- type TargetGroupsStore
- func (t *TargetGroupsStore) AddTargetGroups(targetGroups []TargetGroupConfig)
- func (t *TargetGroupsStore) GetNextUpstreamServer(targetGroupName string) (loadbalance.UpstreamServerInterface, error)
- func (t *TargetGroupsStore) GetTargetGroups() map[string][]loadbalance.UpstreamServerInterface
- func (t *TargetGroupsStore) StartHealthChecks(ctx context.Context, wg *sync.WaitGroup)
- type UpstreamServer
Constants ¶
This section is empty.
Variables ¶
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 ErrLoadingCACert ¶
func ErrLoadingKeyPair ¶
func ErrTLSHandshakeFailed ¶
func ErrTargetGroupNotFound ¶
func GetClientConfigFromConn ¶
func GetClientConfigFromConn( conn net.Conn, authorizedClientsStore *ClientsStore, ) (ratelimit.ClientInfoInterface, 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 (*Instance) GetTargetGroupsStore ¶
func (i *Instance) GetTargetGroupsStore() *TargetGroupsStore
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) DialTimeout ¶
func (*NetDialer) GetRetryLimit ¶
func (*NetDialer) GetTimeout ¶
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)