server

package
v0.0.0-...-d5c0da9 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrServerClosed is returned by the Server's Start, Serve and ServeConn methods after a call to Close.
	ErrServerClosed = errors.New("server: Server closed")

	// ErrServerAlreadyStarted is returned by the Server's Start if its already have been called.
	ErrServerAlreadyStarted = errors.New("server: Server already started")
)
View Source
var (
	// ErrConfigDistributorClosed is returned by the ConfigDistributor's Get and Subscribe methods after a call to Close.
	ErrConfigDistributorClosed = errors.New("config_distributor: ConfigDistributor is closed")
)
View Source
var (
	// ErrSessionClosed is returned by the Server's Start after a call to Close.
	ErrSessionClosed = errors.New("session: Session closed")
)

Functions

func Run

func Run(configChan <-chan *Config) error

Run starts the PG proxy server.

Types

type Config

type Config struct {
	// Local IP address and port on which Gevolut will listen for client connections.
	Listen string

	// Database connection string for the proxied PostgreSQL server.
	DatabaseURL string `toml:"database-url"`
}

Config contains configuration parameters for the server package. cli package use this to unmarshall the gevulot.toml.

type ConfigChangedCheckFn

type ConfigChangedCheckFn func(oldConfig, newConfig *Config) bool

ConfigChangedCheckFn is a predicate function that must return true when previous config differs from the new one. We use this to notify config subscribers only about relevant changes.

type ConfigDistributor

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

ConfigDistributor is an implementation of ConfigStore. It takes a channel that receives config updates and multiplexes it into multiple subscribed channels.

func NewConfigDistributor

func NewConfigDistributor(configChan <-chan *Config) *ConfigDistributor

NewConfigDistributor initializes a new ConfigDistributor.

func (*ConfigDistributor) Close

func (p *ConfigDistributor) Close()

Close stops the monitoring goroutine and cancels all subscriptions.

func (*ConfigDistributor) Get

func (p *ConfigDistributor) Get() (*Config, error)

Get returns current active configuration. Get will block execution until new configuration arrive.

func (*ConfigDistributor) Subscribe

func (p *ConfigDistributor) Subscribe(ch chan<- *Config, checks ...ConfigChangedCheckFn) error

Subscribe subscribes given channel to configuration updates. If checks are specified, new config will be sent to the channel only if one of them returns true.

func (*ConfigDistributor) Unsubscribe

func (p *ConfigDistributor) Unsubscribe(ch chan<- *Config)

Unsubscribe cancels subscription for the given channel.

type ConfigStore

type ConfigStore interface {
	// Get returns current active configuration.
	Get() (*Config, error)

	// Subscribe subscribes given channel to configuration updates.
	// If checks are specified, new config will be sent to the channel only if one of them returns true.
	Subscribe(ch chan<- *Config, checks ...ConfigChangedCheckFn) error

	// Unsubscribe cancels subscription for the given channel.
	Unsubscribe(ch chan<- *Config)
}

ConfigStore represents API that server package using to get configuration parameters.

type Event

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

Event represents a one-time event that may occur in the future. Borrowed from gRPC.

func NewEvent

func NewEvent() *Event

NewEvent returns a new, ready-to-use Event.

func (*Event) Done

func (e *Event) Done() <-chan struct{}

Done returns a channel that will be closed when Fire is called.

func (*Event) Fire

func (e *Event) Fire() bool

Fire causes event to complete. It is safe to call multiple times, and concurrently. It returns true if this call to Fire caused the signaling channel returned by Done to close.

func (*Event) HasFired

func (e *Event) HasFired() bool

HasFired returns true if Fire has been called.

type Server

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

Server is a masking PostgreSQL proxy server.

func NewServer

func NewServer(config ConfigStore) *Server

NewServer initializes a new Server instance.

func (*Server) Close

func (srv *Server) Close() error

Close immediately closes the Server's listener and all active sessions. Close returns any error returned from closing the listener.

Once Close has been called on a server, it may not be reused; future calls to methods such as Serve or Start will return ErrServerClosed.

func (*Server) Serve

func (srv *Server) Serve(ln net.Listener) error

Serve sets the Server listener (closing existing one if set) and then calls ServeConn for every accepted client connection.

Serve blocks until the listener returns a non-nil error. The caller typically invokes Serve in a go statement.

func (*Server) ServeConn

func (srv *Server) ServeConn(conn net.Conn) error

ServeConn proxies given connection to the PostgreSQL database specified in the Server's config.

ServeConn blocks, serving the connection until the client or the database hangs up. The caller typically invokes ServeConn in a go statement.

func (*Server) Start

func (srv *Server) Start() error

Start listens on the TCP network address specified in the Server's config and then calls Serve to handle incoming connections from the clients to the proxied database.

When Server's configuration changed (e.g., when listen port has been modified in the configuration file), Start automatically changes the listener.

Start can only be called once per Server instance.

Start always returns a non-nil error. After Close, the returned error is ErrServerClosed; after consequent Start, the returned error is ErrServerAlreadyStarted.

type Session

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

Session represents a proxied PostgreSQL database session.

func NewSession

func NewSession(client net.Conn, config ConfigStore) *Session

NewSession initializes a new Session.

func (*Session) Close

func (s *Session) Close() (err error)

Close immediately closes Session's underlying network connections. Close returns any error returned from closing db/client connections.

Once Close has been called on a Session, it may not be reused.

func (*Session) Start

func (s *Session) Start() error

Start starts the session between a client and the database. Start blocks, serving the connection until the client or the database hangs up. The caller typically invokes Start in a go statement.

Jump to

Keyboard shortcuts

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