listeners

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: MIT, MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidMessage indicates that a message payload was not valid.
	ErrInvalidMessage = errors.New("message type not binary")
)

Functions

func MockCloser

func MockCloser(id string)

MockCloser is a function signature which can be used in testing.

func MockEstablisher

func MockEstablisher(id string, c net.Conn) error

MockEstablisher is a function signature which can be used in testing.

Types

type CloseFn

type CloseFn func(id string)

CloseFn is a callback function for closing all listener clients.

type Config

type Config struct {
	// TLSConfig is a tls.Config configuration to be used with the listener.
	// See examples folder for basic and mutual-tls use.
	TLSConfig *tls.Config
}

Config contains configuration values for a listener.

type EstablishFn

type EstablishFn func(id string, c net.Conn) error

EstablishFn is a callback function for establishing new clients.

type HTTPHealthCheck

type HTTPHealthCheck struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

HTTPHealthCheck is a listener for providing an HTTP healthcheck endpoint.

func NewHTTPHealthCheck

func NewHTTPHealthCheck(id, address string, config *Config) *HTTPHealthCheck

NewHTTPHealthCheck initialises and returns a new HTTP listener, listening on an address.

func (*HTTPHealthCheck) Address

func (l *HTTPHealthCheck) Address() string

Address returns the address of the listener.

func (*HTTPHealthCheck) Close

func (l *HTTPHealthCheck) Close(closeClients CloseFn)

Close closes the listener and any client connections.

func (*HTTPHealthCheck) ID

func (l *HTTPHealthCheck) ID() string

ID returns the id of the listener.

func (*HTTPHealthCheck) Init

func (l *HTTPHealthCheck) Init(_ *slog.Logger) error

Init initializes the listener.

func (*HTTPHealthCheck) Protocol

func (l *HTTPHealthCheck) Protocol() string

Protocol returns the address of the listener.

func (*HTTPHealthCheck) Serve

func (l *HTTPHealthCheck) Serve(establish EstablishFn)

Serve starts listening for new connections and serving responses.

type HTTPStats

type HTTPStats struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

HTTPStats is a listener for presenting the server $SYS stats on a JSON http endpoint.

func NewHTTPStats

func NewHTTPStats(id, address string, config *Config, sysInfo *system.Info) *HTTPStats

NewHTTPStats initialises and returns a new HTTP listener, listening on an address.

func (*HTTPStats) Address

func (l *HTTPStats) Address() string

Address returns the address of the listener.

func (*HTTPStats) Close

func (l *HTTPStats) Close(closeClients CloseFn)

Close closes the listener and any client connections.

func (*HTTPStats) ID

func (l *HTTPStats) ID() string

ID returns the id of the listener.

func (*HTTPStats) Init

func (l *HTTPStats) Init(log *slog.Logger) error

Init initializes the listener.

func (*HTTPStats) Protocol

func (l *HTTPStats) Protocol() string

Protocol returns the address of the listener.

func (*HTTPStats) Serve

func (l *HTTPStats) Serve(establish EstablishFn)

Serve starts listening for new connections and serving responses.

type Listener

type Listener interface {
	Init(*slog.Logger) error // open the network address
	Serve(EstablishFn)       // starting actively listening for new connections
	ID() string              // return the id of the listener
	Address() string         // the address of the listener
	Protocol() string        // the protocol in use by the listener
	Close(CloseFn)           // stop and close the listener
}

Listener is an interface for network listeners. A network listener listens for incoming client connections and adds them to the server.

type Listeners

type Listeners struct {
	ClientsWg sync.WaitGroup // a waitgroup that waits for all clients in all listeners to finish.

	sync.RWMutex
	// contains filtered or unexported fields
}

Listeners contains the network listeners for the broker.

func New

func New() *Listeners

New returns a new instance of Listeners.

func (*Listeners) Add

func (l *Listeners) Add(val Listener)

Add adds a new listener to the listeners map, keyed on id.

func (*Listeners) Close

func (l *Listeners) Close(id string, closer CloseFn)

Close stops a listener from the internal map.

func (*Listeners) CloseAll

func (l *Listeners) CloseAll(closer CloseFn)

CloseAll iterates and closes all registered listeners.

func (*Listeners) Delete

func (l *Listeners) Delete(id string)

Delete removes a listener from the internal map.

func (*Listeners) Get

func (l *Listeners) Get(id string) (Listener, bool)

Get returns the value of a listener if it exists.

func (*Listeners) Len

func (l *Listeners) Len() int

Len returns the length of the listeners map.

func (*Listeners) Serve

func (l *Listeners) Serve(id string, establisher EstablishFn)

Serve starts a listener serving from the internal map.

func (*Listeners) ServeAll

func (l *Listeners) ServeAll(establisher EstablishFn)

ServeAll starts all listeners serving from the internal map.

type MockListener

type MockListener struct {
	sync.RWMutex

	Config *Config // configuration for the listener

	Serving   bool // indicate the listener is serving
	Listening bool // indiciate the listener is listening
	ErrListen bool // throw an error on listen
	// contains filtered or unexported fields
}

MockListener is a mock listener for establishing client connections.

func NewMockListener

func NewMockListener(id, address string) *MockListener

NewMockListener returns a new instance of MockListener.

func (*MockListener) Address

func (l *MockListener) Address() string

Address returns the address of the listener.

func (*MockListener) Close

func (l *MockListener) Close(closer CloseFn)

Close closes the mock listener.

func (*MockListener) ID

func (l *MockListener) ID() string

ID returns the id of the mock listener.

func (*MockListener) Init

func (l *MockListener) Init(log *slog.Logger) error

Init initializes the listener.

func (*MockListener) IsListening

func (l *MockListener) IsListening() bool

IsListening indicates whether the mock listener is listening.

func (*MockListener) IsServing

func (l *MockListener) IsServing() bool

IsServing indicates whether the mock listener is serving.

func (*MockListener) Protocol

func (l *MockListener) Protocol() string

Protocol returns the address of the listener.

func (*MockListener) Serve

func (l *MockListener) Serve(establisher EstablishFn)

Serve serves the mock listener.

type Net

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

Net is a listener for establishing client connections on basic TCP protocol.

func NewNet

func NewNet(id string, listener net.Listener) *Net

NewNet initialises and returns a listener serving incoming connections on the given net.Listener

func (*Net) Address

func (l *Net) Address() string

Address returns the address of the listener.

func (*Net) Close

func (l *Net) Close(closeClients CloseFn)

Close closes the listener and any client connections.

func (*Net) ID

func (l *Net) ID() string

ID returns the id of the listener.

func (*Net) Init

func (l *Net) Init(log *slog.Logger) error

Init initializes the listener.

func (*Net) Protocol

func (l *Net) Protocol() string

Protocol returns the network of the listener.

func (*Net) Serve

func (l *Net) Serve(establish EstablishFn)

Serve starts waiting for new TCP connections, and calls the establish connection callback for any received.

type TCP

type TCP struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

TCP is a listener for establishing client connections on basic TCP protocol.

func NewTCP

func NewTCP(id, address string, config *Config) *TCP

NewTCP initialises and returns a new TCP listener, listening on an address.

func (*TCP) Address

func (l *TCP) Address() string

Address returns the address of the listener.

func (*TCP) Close

func (l *TCP) Close(closeClients CloseFn)

Close closes the listener and any client connections.

func (*TCP) ID

func (l *TCP) ID() string

ID returns the id of the listener.

func (*TCP) Init

func (l *TCP) Init(log *slog.Logger) error

Init initializes the listener.

func (*TCP) Protocol

func (l *TCP) Protocol() string

Protocol returns the address of the listener.

func (*TCP) Serve

func (l *TCP) Serve(establish EstablishFn)

Serve starts waiting for new TCP connections, and calls the establish connection callback for any received.

type UnixSock

type UnixSock struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

UnixSock is a listener for establishing client connections on basic UnixSock protocol.

func NewUnixSock

func NewUnixSock(id, address string) *UnixSock

NewUnixSock initialises and returns a new UnixSock listener, listening on an address.

func (*UnixSock) Address

func (l *UnixSock) Address() string

Address returns the address of the listener.

func (*UnixSock) Close

func (l *UnixSock) Close(closeClients CloseFn)

Close closes the listener and any client connections.

func (*UnixSock) ID

func (l *UnixSock) ID() string

ID returns the id of the listener.

func (*UnixSock) Init

func (l *UnixSock) Init(log *slog.Logger) error

Init initializes the listener.

func (*UnixSock) Protocol

func (l *UnixSock) Protocol() string

Protocol returns the address of the listener.

func (*UnixSock) Serve

func (l *UnixSock) Serve(establish EstablishFn)

Serve starts waiting for new UnixSock connections, and calls the establish connection callback for any received.

type Websocket

type Websocket struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Websocket is a listener for establishing websocket connections.

func NewWebsocket

func NewWebsocket(id, address string, config *Config) *Websocket

NewWebsocket initialises and returns a new Websocket listener, listening on an address.

func (*Websocket) Address

func (l *Websocket) Address() string

Address returns the address of the listener.

func (*Websocket) Close

func (l *Websocket) Close(closeClients CloseFn)

Close closes the listener and any client connections.

func (*Websocket) ID

func (l *Websocket) ID() string

ID returns the id of the listener.

func (*Websocket) Init

func (l *Websocket) Init(log *slog.Logger) error

Init initializes the listener.

func (*Websocket) Protocol

func (l *Websocket) Protocol() string

Protocol returns the address of the listener.

func (*Websocket) Serve

func (l *Websocket) Serve(establish EstablishFn)

Serve starts waiting for new Websocket connections, and calls the connection establishment callback for any received.

Jump to

Keyboard shortcuts

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