Documentation ¶
Index ¶
- Constants
- func CreateTLSConfig(certFile, keyFile string) (*tls.Config, error)
- func GetID(network, address string, seed int, logger zerolog.Logger) string
- func LocalAddr(conn net.Conn) string
- func RemoteAddr(conn net.Conn) string
- func Resolve(network, address string, logger zerolog.Logger) (string, *gerr.GatewayDError)
- type Action
- type Client
- func (c *Client) Close()
- func (c *Client) IsConnected() bool
- func (c *Client) LocalAddr() string
- func (c *Client) Receive() (int, []byte, *gerr.GatewayDError)
- func (c *Client) Reconnect() error
- func (c *Client) RemoteAddr() string
- func (c *Client) Retry() *Retry
- func (c *Client) Send(data []byte) (int, *gerr.GatewayDError)
- type ConnWrapper
- func (cw *ConnWrapper) Close() error
- func (cw *ConnWrapper) Conn() net.Conn
- func (cw *ConnWrapper) IsTLSEnabled() bool
- func (cw *ConnWrapper) LocalAddr() net.Addr
- func (cw *ConnWrapper) Read(data []byte) (int, error)
- func (cw *ConnWrapper) RemoteAddr() net.Addr
- func (cw *ConnWrapper) UpgradeToTLS(upgrader UpgraderFunc) *gerr.GatewayDError
- func (cw *ConnWrapper) Write(data []byte) (int, error)
- type ConsistentHash
- type Field
- type IClient
- type IConnWrapper
- type IProxy
- type IRetry
- type IServer
- type LoadBalancerStrategy
- type Option
- type Proxy
- func (pr *Proxy) AvailableConnectionsString() []string
- func (pr *Proxy) BusyConnectionsString() []string
- func (pr *Proxy) Connect(conn *ConnWrapper) *gerr.GatewayDError
- func (pr *Proxy) Disconnect(conn *ConnWrapper) *gerr.GatewayDError
- func (pr *Proxy) GetBlockName() string
- func (pr *Proxy) GetGroupName() string
- func (pr *Proxy) IsExhausted() bool
- func (pr *Proxy) IsHealthy(client *Client) (*Client, *gerr.GatewayDError)
- func (pr *Proxy) PassThroughToClient(conn *ConnWrapper, stack *Stack) *gerr.GatewayDError
- func (pr *Proxy) PassThroughToServer(conn *ConnWrapper, stack *Stack) *gerr.GatewayDError
- func (pr *Proxy) Shutdown()
- type Random
- type Request
- type Retry
- type RetryCallback
- type RoundRobin
- type Server
- func (s *Server) CountConnections() int
- func (s *Server) GetProxyForConnection(conn *ConnWrapper) (IProxy, bool)
- func (s *Server) IsRunning() bool
- func (s *Server) IsTLSEnabled() bool
- func (s *Server) OnBoot() Action
- func (s *Server) OnClose(conn *ConnWrapper, err error) Action
- func (s *Server) OnOpen(conn *ConnWrapper) ([]byte, Action)
- func (s *Server) OnShutdown()
- func (s *Server) OnTick() (time.Duration, Action)
- func (s *Server) OnTraffic(conn *ConnWrapper, stopConnection chan struct{}) Action
- func (s *Server) RemoveConnectionFromMap(conn *ConnWrapper)
- func (s *Server) Run() *gerr.GatewayDError
- func (s *Server) Shutdown()
- type Stack
- type UpgraderFunc
- type WeightedRoundRobin
Constants ¶
const ( BackoffMultiplierCap = 10 BackoffDurationCap = time.Minute )
Variables ¶
This section is empty.
Functions ¶
func CreateTLSConfig ¶ added in v0.8.5
CreateTLSConfig returns a TLS config from the given cert and key. TODO: Make this more generic and configurable.
func RemoteAddr ¶ added in v0.6.9
RemoteAddr returns the remote address of the connection.
Types ¶
type Client ¶
type Client struct { GroupName string BlockName string TCPKeepAlive bool TCPKeepAlivePeriod time.Duration ReceiveChunkSize int ReceiveDeadline time.Duration SendDeadline time.Duration ReceiveTimeout time.Duration DialTimeout time.Duration ID string Network string // tcp/udp/unix Address string // contains filtered or unexported fields }
func NewClient ¶
func NewClient( ctx context.Context, clientConfig *config.Client, logger zerolog.Logger, retry *Retry, ) *Client
NewClient creates a new client.
func (*Client) IsConnected ¶ added in v0.1.0
IsConnected checks if the client is still connected to the server.
func (*Client) LocalAddr ¶ added in v0.6.9
LocalAddr returns the local address of the client safely.
func (*Client) Receive ¶
func (c *Client) Receive() (int, []byte, *gerr.GatewayDError)
Receive receives data from the server.
func (*Client) RemoteAddr ¶ added in v0.6.9
RemoteAddr returns the remote address of the client safely.
type ConnWrapper ¶ added in v0.8.5
type ConnWrapper struct { NetConn net.Conn TLSConfig *tls.Config HandshakeTimeout time.Duration // contains filtered or unexported fields }
func NewConnWrapper ¶ added in v0.8.5
func NewConnWrapper( connWrapper ConnWrapper, ) *ConnWrapper
NewConnWrapper creates a new connection wrapper. The connection wrapper is used to upgrade the connection to TLS if need be.
func (*ConnWrapper) Close ¶ added in v0.8.5
func (cw *ConnWrapper) Close() error
Close closes the connection.
func (*ConnWrapper) Conn ¶ added in v0.8.5
func (cw *ConnWrapper) Conn() net.Conn
Conn returns the underlying connection.
func (*ConnWrapper) IsTLSEnabled ¶ added in v0.8.5
func (cw *ConnWrapper) IsTLSEnabled() bool
IsTLSEnabled returns true if TLS is enabled.
func (*ConnWrapper) LocalAddr ¶ added in v0.8.5
func (cw *ConnWrapper) LocalAddr() net.Addr
LocalAddr returns the local address.
func (*ConnWrapper) Read ¶ added in v0.8.5
func (cw *ConnWrapper) Read(data []byte) (int, error)
Read reads data from the connection.
func (*ConnWrapper) RemoteAddr ¶ added in v0.8.5
func (cw *ConnWrapper) RemoteAddr() net.Addr
RemoteAddr returns the remote address.
func (*ConnWrapper) UpgradeToTLS ¶ added in v0.8.5
func (cw *ConnWrapper) UpgradeToTLS(upgrader UpgraderFunc) *gerr.GatewayDError
UpgradeToTLS upgrades the connection to TLS.
type ConsistentHash ¶ added in v0.9.7
type ConsistentHash struct {
// contains filtered or unexported fields
}
ConsistentHash implements a load balancing strategy based on consistent hashing. It routes client connections to specific proxies by hashing the client's IP address or the full connection address.
func NewConsistentHash ¶ added in v0.9.7
func NewConsistentHash(server *Server, originalStrategy LoadBalancerStrategy) *ConsistentHash
NewConsistentHash creates a new ConsistentHash instance. It requires a server configuration and an original load balancing strategy. The consistent hash can use either the source IP or the full connection address as the key for hashing.
func (*ConsistentHash) NextProxy ¶ added in v0.9.7
func (ch *ConsistentHash) NextProxy(conn IConnWrapper) (IProxy, *gerr.GatewayDError)
NextProxy selects the appropriate proxy for a given client connection. It first tries to find an existing proxy in the hash map based on the hashed key (either the source IP or the full address). If no match is found, it falls back to the original load balancing strategy, adds the selected proxy to the hash map, and returns it.
type IConnWrapper ¶ added in v0.8.5
type IProxy ¶ added in v0.2.2
type IProxy interface { Connect(conn *ConnWrapper) *gerr.GatewayDError Disconnect(conn *ConnWrapper) *gerr.GatewayDError PassThroughToServer(conn *ConnWrapper, stack *Stack) *gerr.GatewayDError PassThroughToClient(conn *ConnWrapper, stack *Stack) *gerr.GatewayDError IsHealthy(cl *Client) (*Client, *gerr.GatewayDError) IsExhausted() bool Shutdown() AvailableConnectionsString() []string BusyConnectionsString() []string GetGroupName() string GetBlockName() string }
type IRetry ¶ added in v0.8.6
type IRetry interface {
Retry(_ RetryCallback) (any, error)
}
type IServer ¶ added in v0.8.5
type IServer interface { OnBoot() Action OnOpen(conn *ConnWrapper) ([]byte, Action) OnClose(conn *ConnWrapper, err error) Action OnTraffic(conn *ConnWrapper, stopConnection chan struct{}) Action OnShutdown() OnTick() (time.Duration, Action) Run() *gerr.GatewayDError Shutdown() IsRunning() bool IsTLSEnabled() bool CountConnections() int GetProxyForConnection(conn *ConnWrapper) (IProxy, bool) RemoveConnectionFromMap(conn *ConnWrapper) }
type LoadBalancerStrategy ¶ added in v0.9.7
type LoadBalancerStrategy interface {
NextProxy(conn IConnWrapper) (IProxy, *gerr.GatewayDError)
}
func NewLoadBalancerStrategy ¶ added in v0.9.7
func NewLoadBalancerStrategy(server *Server) (LoadBalancerStrategy, *gerr.GatewayDError)
NewLoadBalancerStrategy returns a LoadBalancerStrategy based on the server's load balancer strategy name. If the server's load balancer strategy is weighted round-robin, it selects a load balancer rule before returning the strategy. Returns an error if the strategy is not found or if there are no load balancer rules when required.
type Proxy ¶
type Proxy struct { GroupName string BlockName string AvailableConnections pool.IPool Logger zerolog.Logger PluginRegistry *plugin.Registry PluginTimeout time.Duration HealthCheckPeriod time.Duration // ClientConfig is used for reconnection ClientConfig *config.Client // contains filtered or unexported fields }
func (*Proxy) AvailableConnectionsString ¶ added in v0.9.4
AvailableConnectionsString returns a list of available connections. This list enumerates the local addresses of the outgoing connections to the server.
func (*Proxy) BusyConnectionsString ¶ added in v0.9.4
BusyConnectionsString returns a list of busy connections. This list enumerates the remote addresses of the incoming connections from a database client like psql.
func (*Proxy) Connect ¶
func (pr *Proxy) Connect(conn *ConnWrapper) *gerr.GatewayDError
Connect maps a server connection from the available connection pool to a incoming connection. It returns an error if the pool is exhausted.
func (*Proxy) Disconnect ¶
func (pr *Proxy) Disconnect(conn *ConnWrapper) *gerr.GatewayDError
Disconnect removes the client from the busy connection pool and tries to recycle the server connection.
func (*Proxy) GetBlockName ¶ added in v0.9.9
func (*Proxy) GetGroupName ¶ added in v0.9.9
func (*Proxy) IsExhausted ¶ added in v0.1.0
IsExhausted checks if the available connection pool is exhausted.
func (*Proxy) IsHealthy ¶ added in v0.8.3
func (pr *Proxy) IsHealthy(client *Client) (*Client, *gerr.GatewayDError)
IsHealthy checks if the pool is exhausted or the client is disconnected.
func (*Proxy) PassThroughToClient ¶ added in v0.8.3
func (pr *Proxy) PassThroughToClient(conn *ConnWrapper, stack *Stack) *gerr.GatewayDError
PassThroughToClient sends the data from the server to the client.
func (*Proxy) PassThroughToServer ¶ added in v0.8.3
func (pr *Proxy) PassThroughToServer(conn *ConnWrapper, stack *Stack) *gerr.GatewayDError
PassThroughToServer sends the data from the client to the server.
type Random ¶ added in v0.9.7
type Random struct {
// contains filtered or unexported fields
}
Random is a struct that holds a list of proxies and a mutex for thread safety.
func NewRandom ¶ added in v0.9.7
NewRandom creates a new Random instance with the given server's proxies.
func (*Random) NextProxy ¶ added in v0.9.7
func (r *Random) NextProxy(_ IConnWrapper) (IProxy, *gerr.GatewayDError)
NextProxy returns a random proxy from the list.
type Retry ¶ added in v0.8.6
type RetryCallback ¶ added in v0.8.6
type RoundRobin ¶ added in v0.9.7
type RoundRobin struct {
// contains filtered or unexported fields
}
func NewRoundRobin ¶ added in v0.9.7
func NewRoundRobin(server *Server) *RoundRobin
func (*RoundRobin) NextProxy ¶ added in v0.9.7
func (r *RoundRobin) NextProxy(_ IConnWrapper) (IProxy, *gerr.GatewayDError)
type Server ¶
type Server struct { Proxies []IProxy Logger zerolog.Logger PluginRegistry *plugin.Registry PluginTimeout time.Duration GroupName string Network string // tcp/udp/unix Address string Options Option Status config.Status TickInterval time.Duration // TLS config EnableTLS bool CertFile string KeyFile string HandshakeTimeout time.Duration LoadbalancerStrategyName string LoadbalancerRules []config.LoadBalancingRule LoadbalancerConsistentHash *config.ConsistentHash RaftNode *raft.Node ProxyByBlock map[string]IProxy // contains filtered or unexported fields }
func (*Server) CountConnections ¶ added in v0.8.9
CountConnections returns the current number of connections.
func (*Server) GetProxyForConnection ¶ added in v0.9.7
func (s *Server) GetProxyForConnection(conn *ConnWrapper) (IProxy, bool)
GetProxyForConnection returns the proxy associated with the given connection.
func (*Server) IsTLSEnabled ¶ added in v0.10.0
IsTLSEnabled returns true if TLS is enabled.
func (*Server) OnBoot ¶
OnBoot is called when the server is booted. It calls the OnBooting and OnBooted hooks. It also sets the status to running, which is used to determine if the server should be running or shutdown.
func (*Server) OnClose ¶
func (s *Server) OnClose(conn *ConnWrapper, err error) Action
OnClose is called when a connection is closed. It calls the OnClosing and OnClosed hooks. It also recycles the connection back to the available connection pool.
func (*Server) OnOpen ¶
func (s *Server) OnOpen(conn *ConnWrapper) ([]byte, Action)
OnOpen is called when a new connection is opened. It calls the OnOpening and OnOpened hooks. It also checks if the server is at the soft or hard limit and closes the connection if it is.
func (*Server) OnShutdown ¶
func (s *Server) OnShutdown()
OnShutdown is called when the server is shutting down. It calls the OnShutdown hooks.
func (*Server) OnTraffic ¶
func (s *Server) OnTraffic(conn *ConnWrapper, stopConnection chan struct{}) Action
OnTraffic is called when data is received from the client. It calls the OnTraffic hooks. It then passes the traffic to the proxied connection.
func (*Server) RemoveConnectionFromMap ¶ added in v0.9.7
func (s *Server) RemoveConnectionFromMap(conn *ConnWrapper)
RemoveConnectionFromMap removes the given connection from the connection-to-proxy map.
func (*Server) Run ¶
func (s *Server) Run() *gerr.GatewayDError
Run starts the server and blocks until the server is stopped. It calls the OnRun hooks.
type Stack ¶ added in v0.8.3
type Stack struct {
// contains filtered or unexported fields
}
func (*Stack) GetLastRequest ¶ added in v0.8.3
func (*Stack) PopLastRequest ¶ added in v0.8.3
func (*Stack) UpdateLastRequest ¶ added in v0.8.3
type UpgraderFunc ¶ added in v0.8.5
UpgraderFunc is a function that upgrades a connection to TLS. For example, this function can be used to upgrade a Postgres connection to TLS. Postgres initially sends a SSLRequest message, and the server responds with a 'S' message to indicate that it supports TLS. The client then upgrades the connection to TLS. See https://www.postgresql.org/docs/current/protocol-flow.html
type WeightedRoundRobin ¶ added in v0.9.7
type WeightedRoundRobin struct {
// contains filtered or unexported fields
}
func NewWeightedRoundRobin ¶ added in v0.9.7
func NewWeightedRoundRobin(server *Server, loadbalancerRule config.LoadBalancingRule) *WeightedRoundRobin
NewWeightedRoundRobin creates a new WeightedRoundRobin load balancer. It initializes the weighted proxies based on the distribution rules defined in the load balancer configuration.
func (*WeightedRoundRobin) NextProxy ¶ added in v0.9.7
func (r *WeightedRoundRobin) NextProxy(_ IConnWrapper) (IProxy, *gerr.GatewayDError)
NextProxy selects the next proxy based on the weighted round-robin algorithm.
It adjusts the current weight of each proxy based on its effective weight and selects the proxy with the highest current weight. The selected proxy's current weight is then decreased by the total weight of all proxies to ensure balanced distribution over time.