server

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2022 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClosing        = errors.New("server is closing")
	ErrClosed         = errors.New("server is closed")
	ErrAlreadyRunning = errors.New("server is already running")
	ErrNotRunning     = errors.New("server is not running")
)

Functions

This section is empty.

Types

type BroadcastResult

type BroadcastResult struct {
	Size     int
	Duration time.Duration
}

BroadcastResult represents the size and duration of each sent message during a broadcast operation. It provides information to the caller code.

type Cfg

type Cfg struct {
	ListenURL string

	TLSConfig             *tls.Config
	MaxConcurrency        int
	ReadBufferSize        int
	WriteBufferSize       int
	HTTPWriteTimeout      time.Duration
	HTTPReadTimeout       time.Duration
	HTTPReadHeaderTimeout time.Duration
	HandshakeTimeout      time.Duration
	EnableCompression     bool
	// contains filtered or unexported fields
}

type MeteredServer

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

func NewMetered

func NewMetered(m *metrics.ServerMetrics, opts ...Option) (*MeteredServer, error)

func (*MeteredServer) Broadcast added in v0.2.0

func (s *MeteredServer) Broadcast(ctx context.Context, msg *message.Message) (brResult []BroadcastResult, err error)

func (*MeteredServer) Handle

func (s *MeteredServer) Handle(msg proto.Message, handler message.Handler)

func (*MeteredServer) Middleware

func (s *MeteredServer) Middleware(m message.Middleware)

func (*MeteredServer) Publish added in v0.2.0

func (s *MeteredServer) Publish(topic string, msg *message.Message) error

func (*MeteredServer) Run

func (s *MeteredServer) Run() (err error)

func (*MeteredServer) Shutdown

func (s *MeteredServer) Shutdown(ctx context.Context) (err error)

type Option

type Option func(cfg *Cfg)

func WithCompressionEnabled

func WithCompressionEnabled(b bool) Option

WithCompressionEnabled specifies if the server should try to negotiate compression (see https://datatracker.ietf.org/doc/html/rfc7692). Enabling this does not guarantee its success.

func WithHTTPReadHeaderTimeout

func WithHTTPReadHeaderTimeout(t time.Duration) Option

WithHTTPReadHeaderTimeout corresponds to the http.Server.ReadHeaderTimeout for this websocket server.

func WithHTTPReadTimeout

func WithHTTPReadTimeout(t time.Duration) Option

WithHTTPReadTimeout correspond to the http.Server.ReadTimeout configuration for this websocket server.

func WithHTTPWriteTimeout

func WithHTTPWriteTimeout(t time.Duration) Option

WithHTTPWriteTimeout corresponds to the http.Server.WriteTimeout configuration for this websocket server.

func WithHandShakeTimeout

func WithHandShakeTimeout(d time.Duration) Option

WithHandShakeTimeout set the time limit in which the websocket handshake should take place.

func WithListenAddr

func WithListenAddr(addr string) Option

WithListenAddr the address in which the server will listen to client connections.

func WithMaxConcurrency

func WithMaxConcurrency(n int) Option

WithMaxConcurrency sets the concurrency level for handler execution. Values <= 1 means no concurrency, which means no goroutine scheduling takes place. Defaults to 10.

func WithOnBroadcastHook added in v0.2.0

func WithOnBroadcastHook(f func(fqdn string, result []BroadcastResult, duration time.Duration)) Option

WithOnBroadcastHook allows the user to inject a hook which will be executed on each successfully broadcast operation.

func WithOnClientBroadcastHook added in v0.2.0

func WithOnClientBroadcastHook(f func(fqdn string)) Option

WithOnClientBroadcastHook allows the user to inject a hook which will be executed each time a broadcast command arrives from the client.

func WithOnCloseHook

func WithOnCloseHook(h func()) Option

WithOnCloseHook allows the user registering function hooks for when the server reaches the ws.StatusClosed status.

Sending multiple times this option in the constructor will lead to function concatenation, so multiple hooks can be configured.

IMPORTANT NOTE: There's no panic catcher implementation here, ensure to implement it or ensure the underlying code does not panic.

func WithOnConfiguration

func WithOnConfiguration(h func(cfg *Cfg)) Option

WithOnConfiguration allows the user registering hook functions which will be invoked once the server is configured.

func WithOnErrorHook

func WithOnErrorHook(h func(err error)) Option

WithOnErrorHook allows the user registering function hooks for when an internal error happens, but cannot be returned in any way to the user. Like in processing loops.

IMPORTANT NOTE: There's no panic catcher implementation here, ensure to implement it or ensure the underlying code does not panic.

func WithOnPublishHook added in v0.2.0

func WithOnPublishHook(f func(topic, fqdn string)) Option

WithOnPublishHook allows the user to inject a hook which will be executed each time successfully publish operation takes place for a specific topic and message (fqdn).

func WithOnStatusChangeHook

func WithOnStatusChangeHook(h func(status uint32)) Option

WithOnStatusChangeHook allows the user registering function hooks for each status changes. Possible values for statuses can be found at go.eloylp.dev/goomerang/ws package.

Sending multiple times this option in the constructor will lead to function concatenation, so multiple hooks can be configured.

IMPORTANT NOTE: There's no panic catcher implementation here, ensure to implement it or ensure the underlying code does not panic.

func WithOnSubscribeHook added in v0.2.0

func WithOnSubscribeHook(f func(topic string)) Option

WithOnSubscribeHook allows the user to inject a hook which will be executed each successfully subscribe to a specific topic, for a message.

func WithOnUnsubscribeHook added in v0.2.0

func WithOnUnsubscribeHook(f func(topic string)) Option

WithOnUnsubscribeHook allows the user to inject a hook which will be executed each time successfully unsubscribe from a specific topic, for a message.

func WithOnWorkerEnd

func WithOnWorkerEnd(h func()) Option

WithOnWorkerEnd allows the user registering hook functions which will be invoked whenever a handler goroutine ends its operations. Only if concurrency is enabled by using WithMaxConcurrency option.

func WithOnWorkerStart

func WithOnWorkerStart(h func()) Option

WithOnWorkerStart allows the user registering hook functions which will be invoked whenever a new handler goroutine is invoked. Only if concurrency is enabled by the WithMaxConcurrency option.

func WithReadBufferSize

func WithReadBufferSize(s int) Option

WithReadBufferSize configures the size in bytes of the read buffers for each connection. Defaults on 4096 bytes.

func WithTLSConfig

func WithTLSConfig(tlsConfig *tls.Config) Option

WithTLSConfig allows the user to pass a *tls.Config full setup to the server, in which encryption and authentication could be configured, by setting up a PKI (public key infrastructure).

func WithWriteBufferSize

func WithWriteBufferSize(s int) Option

WithWriteBufferSize configures the size in bytes of the write buffers for each connection. Defaults on 4096 bytes.

type Server

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

Server will hold all subsystem and orchestration elements.

func New

func New(opts ...Option) (*Server, error)

New creates a server instance, check all available options.

func (*Server) Addr

func (s *Server) Addr() string

Addr provides the listener address. It will return an empty string if it's not possible to determine the IP information.

func (*Server) Broadcast added in v0.2.0

func (s *Server) Broadcast(ctx context.Context, msg *message.Message) (brResult []BroadcastResult, err error)

Broadcast will try to send the provided message to all connected clients.

In case the provided context is canceled, the operation could be partially completed. It's recommended to always pass a context.WithTimeout().

The send operation will happen in parallel for each client.

In case of errors, it will continue the operation, keeping the first 100 ones and returning them in a multi-error type.

In case of success, the BroadcastResult return type will provide feedback, such as time distribution.

Calling this method it's intended to be thread safe.

func (*Server) Handle

func (s *Server) Handle(msg proto.Message, handler message.Handler)

Handle registers a message handler in the server. It will panic if the server its already running.

func (*Server) Middleware

func (s *Server) Middleware(m message.Middleware)

Middleware registers a middleware in the server. It will panic if the server its already running.

func (*Server) Publish added in v0.2.0

func (s *Server) Publish(topic string, msg *message.Message) error

Publish enables the server to send a message to all subscribed clients in a topic.

func (*Server) RegisterMessage added in v0.2.0

func (s *Server) RegisterMessage(msg proto.Message)

RegisterMessage will make the server aware of a specific kind of protocol buffer message. This is specially needed when the user sends messages with methods like SendSync(), as the client needs to know how to decode the incoming reply.

If the kind of message it's already registered with the Handle() method, then the user can omit this registration.

This is specially needed in order to make the server aware of the messages published in pub/sub patterns.

func (*Server) Registry

func (s *Server) Registry() message.Registry

func (*Server) Router added in v0.3.0

func (s *Server) Router() *http.ServeMux

Router provides access to the underlying HTTP router. The routes /ws and /wss are reserved for library operations.

func (*Server) Run

func (s *Server) Run() (err error)

Run will start the server with all the previously provided options.

The server instance it's not reusable. This method can be only executed once in the lifecycle of a server.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) (err error)

Shutdown will start the graceful shutdown of the server.

The call to this function will block till the operation completes or the provided context expires.

A closing signal will be sent to all connected clients in parallel. Clients should then individually reply to this signal with another closing one. The server will wait a grace period of 5 seconds per each connection, before closing them.

This function will also wait for all the inflight message handlers and subsystems.

type SyncSender

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

func (*SyncSender) ConnSlot added in v0.2.0

func (s *SyncSender) ConnSlot() *conn.Slot

func (*SyncSender) Send

func (s *SyncSender) Send(msg *message.Message) (int, error)

Jump to

Keyboard shortcuts

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