network

package
v0.8.13 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2024 License: AGPL-3.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BackoffMultiplierCap = 10
	BackoffDurationCap   = time.Minute
)

Variables

This section is empty.

Functions

func CreateTLSConfig added in v0.8.5

func CreateTLSConfig(certFile, keyFile string) (*tls.Config, error)

CreateTLSConfig returns a TLS config from the given cert and key. TODO: Make this more generic and configurable.

func GetID

func GetID(network, address string, seed int, logger zerolog.Logger) string

GetID returns a unique ID (hash) for a network connection.

func IsConnClosed added in v0.1.4

func IsConnClosed(received int, err *gerr.GatewayDError) bool

IsConnClosed returns true if the connection is closed.

func IsConnTimedOut added in v0.1.4

func IsConnTimedOut(err *gerr.GatewayDError) bool

IsConnTimedOut returns true if the error is a timeout error.

func IsPostgresSSLRequest added in v0.8.5

func IsPostgresSSLRequest(data []byte) bool

IsPostgresSSLRequest returns true if the message is a SSL request. This is copied from gatewayd-plugin-sdk to avoid the dependency on CGO.

func LocalAddr added in v0.6.9

func LocalAddr(conn net.Conn) string

LocalAddr returns the local address of the connection.

func RemoteAddr added in v0.6.9

func RemoteAddr(conn net.Conn) string

RemoteAddr returns the remote address of the connection.

func Resolve added in v0.0.2

func Resolve(network, address string, logger zerolog.Logger) (string, *gerr.GatewayDError)

Resolve resolves a network address.

Types

type Action added in v0.8.3

type Action int
const (
	None Action = iota
	Close
	Shutdown
)

type Client

type Client struct {
	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) Close

func (c *Client) Close()

Close closes the connection to the server.

func (*Client) IsConnected added in v0.1.0

func (c *Client) IsConnected() bool

IsConnected checks if the client is still connected to the server.

func (*Client) LocalAddr added in v0.6.9

func (c *Client) LocalAddr() string

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) Reconnect added in v0.8.3

func (c *Client) Reconnect() error

Reconnect reconnects to the server.

func (*Client) RemoteAddr added in v0.6.9

func (c *Client) RemoteAddr() string

RemoteAddr returns the remote address of the client safely.

func (*Client) Retry added in v0.8.6

func (c *Client) Retry() *Retry

Retry returns the retry object.

func (*Client) Send

func (c *Client) Send(data []byte) (int, *gerr.GatewayDError)

Send sends data to the server.

type ConnWrapper added in v0.8.5

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

func NewConnWrapper added in v0.8.5

func NewConnWrapper(
	conn net.Conn, tlsConfig *tls.Config, handshakeTimeout time.Duration,
) *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.

func (*ConnWrapper) Write added in v0.8.5

func (cw *ConnWrapper) Write(data []byte) (int, error)

Write writes data to the connection.

type Field added in v0.2.3

type Field struct {
	Name  string
	Value []byte
}

type IClient added in v0.2.2

type IClient interface {
	Send(data []byte) (int, *gerr.GatewayDError)
	Receive() (int, []byte, *gerr.GatewayDError)
	Reconnect() error
	Close()
	IsConnected() bool
	RemoteAddr() string
	LocalAddr() string
	Retry() *Retry
}

type IConnWrapper added in v0.8.5

type IConnWrapper interface {
	Conn() net.Conn
	UpgradeToTLS(upgrader UpgraderFunc) *gerr.GatewayDError
	Close() error
	Write(data []byte) (int, error)
	Read(data []byte) (int, error)
	RemoteAddr() net.Addr
	LocalAddr() net.Addr
	IsTLSEnabled() bool
}

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()
	AvailableConnections() []string
	BusyConnections() []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
	CountConnections() int
}

type Option added in v0.8.3

type Option struct {
	EnableTicker bool
}

type Proxy

type Proxy struct {
	HealthCheckPeriod time.Duration

	// ClientConfig is used for reconnection
	ClientConfig *config.Client
	// contains filtered or unexported fields
}

func NewProxy

func NewProxy(
	ctx context.Context,
	connPool pool.IPool, pluginRegistry *plugin.Registry,
	healthCheckPeriod time.Duration,
	clientConfig *config.Client, logger zerolog.Logger,
	pluginTimeout time.Duration,
) *Proxy

NewProxy creates a new proxy.

func (*Proxy) AvailableConnections added in v0.5.0

func (pr *Proxy) AvailableConnections() []string

AvailableConnections returns a list of available connections.

func (*Proxy) BusyConnections added in v0.5.0

func (pr *Proxy) BusyConnections() []string

BusyConnections returns a list of busy connections.

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) IsExhausted added in v0.1.0

func (pr *Proxy) IsExhausted() bool

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.

func (*Proxy) Shutdown

func (pr *Proxy) Shutdown()

Shutdown closes all connections and clears the connection pools.

type Request added in v0.8.3

type Request struct {
	Data []byte
}

type Retry added in v0.8.6

type Retry struct {
	Retries            int
	Backoff            time.Duration
	BackoffMultiplier  float64
	DisableBackoffCaps bool
	// contains filtered or unexported fields
}

func NewRetry added in v0.8.6

func NewRetry(
	retries int,
	backoff time.Duration,
	backoffMultiplier float64,
	disableBackoffCaps bool,
	logger zerolog.Logger,
) *Retry

func (*Retry) Retry added in v0.8.6

func (r *Retry) Retry(callback RetryCallback) (any, error)

Retry runs the callback function and retries it if it fails. It'll wait for the duration of the backoff between retries.

type RetryCallback added in v0.8.6

type RetryCallback func() (any, error)

type Server

type Server struct {
	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
	// contains filtered or unexported fields
}

func NewServer added in v0.0.2

func NewServer(
	ctx context.Context,
	network, address string,
	tickInterval time.Duration,
	options Option,
	proxy IProxy,
	logger zerolog.Logger,
	pluginRegistry *plugin.Registry,
	pluginTimeout time.Duration,
	enableTLS bool,
	certFile, keyFile string,
	handshakeTimeout time.Duration,
) *Server

NewServer creates a new server.

func (*Server) CountConnections added in v0.8.9

func (s *Server) CountConnections() int

CountConnections returns the current number of connections.

func (*Server) IsRunning added in v0.0.2

func (s *Server) IsRunning() bool

IsRunning returns true if the server is running.

func (*Server) OnBoot

func (s *Server) OnBoot() Action

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) OnTick

func (s *Server) OnTick() (time.Duration, Action)

OnTick is called every TickInterval. It calls the OnTick 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) Run

func (s *Server) Run() *gerr.GatewayDError

Run starts the server and blocks until the server is stopped. It calls the OnRun hooks.

func (*Server) Shutdown

func (s *Server) Shutdown()

Shutdown stops the server.

type Stack added in v0.8.3

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

func NewStack added in v0.8.3

func NewStack() *Stack

func (*Stack) Clear added in v0.8.3

func (s *Stack) Clear()

func (*Stack) GetLastRequest added in v0.8.3

func (s *Stack) GetLastRequest() *Request

func (*Stack) PopLastRequest added in v0.8.3

func (s *Stack) PopLastRequest() *Request

func (*Stack) Push added in v0.8.3

func (s *Stack) Push(req *Request)

func (*Stack) UpdateLastRequest added in v0.8.3

func (s *Stack) UpdateLastRequest(req *Request)

type UpgraderFunc added in v0.8.5

type UpgraderFunc func(net.Conn)

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

Jump to

Keyboard shortcuts

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