client

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: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotRunning     = errors.New("not running")
	ErrAlreadyRunning = errors.New("already running")
)

Functions

This section is empty.

Types

type Cfg

type Cfg struct {
	TargetServer string

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

type Client

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

Client holds are the client subsystems and dependencies.

func New

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

New creates a new client, review available options.

func (*Client) Broadcast added in v0.2.0

func (c *Client) Broadcast(msg *message.Message) (payloadSize int, err error)

func (*Client) Close

func (c *Client) Close(ctx context.Context) (err error)

Close will initiate the graceful shutdown procedure for this client. It will work in a best effort way. In case of errors, they are going to be collected and returned as a multi-error type, with the hope the maximum number of closing actions are performed.

func (*Client) ConnSlot added in v0.2.0

func (c *Client) ConnSlot() *conn.Slot

func (*Client) Connect

func (c *Client) Connect(ctx context.Context) error

Connect tries a connection to the specified server. This method can be called after a client shutdown again in order to retry the connection.

func (*Client) Handle

func (c *Client) Handle(msg proto.Message, h message.Handler)

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

func (*Client) Middleware

func (c *Client) Middleware(m message.Middleware)

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

func (*Client) Publish added in v0.2.0

func (c *Client) Publish(topic string, msg *message.Message) (payloadSize int, err error)

func (*Client) RegisterMessage

func (c *Client) RegisterMessage(msg proto.Message)

RegisterMessage will make the client 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.

Any kind of protocol buffers message that arrives to the client, and It's not registered, will be discarded.

func (*Client) Send

func (c *Client) Send(msg *message.Message) (payloadSize int, err error)

Send will write the provided *message.Message to the output buffer. This method is completely asynchronous. In case the client has closed the connection due to other reasons, ErrNotRunning will be returned.

If successful, it will return the payload size in bytes. This payload size does not include the goomerang base message (which is around 12 bytes) nor the headers.

func (*Client) SendSync

func (c *Client) SendSync(ctx context.Context, msg *message.Message) (payloadSize int, response *message.Message, err error)

SendSync will send a message to the server and wait for a reply. If the provided context is canceled, this function will return immediately and the reply message will be lost.

If successful, it will return the payload size in bytes. This payload size does not include the goomerang base message (which is around 12 bytes) nor the headers.

ErrNotRunning error will be returned in case the client was closed for any reason.

func (*Client) Subscribe added in v0.2.0

func (c *Client) Subscribe(topic string) (err error)

func (*Client) Unsubscribe added in v0.2.0

func (c *Client) Unsubscribe(topic string) (err error)

type MeteredClient

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

func NewMetered

func NewMetered(m *metrics.ClientMetrics, opts ...Option) (*MeteredClient, error)

func (*MeteredClient) Broadcast added in v0.2.0

func (c *MeteredClient) Broadcast(msg *message.Message) error

func (*MeteredClient) Close

func (c *MeteredClient) Close(ctx context.Context) (err error)

func (*MeteredClient) Connect

func (c *MeteredClient) Connect(ctx context.Context) error

func (*MeteredClient) Handle

func (c *MeteredClient) Handle(msg proto.Message, h message.Handler)

func (*MeteredClient) Middleware

func (c *MeteredClient) Middleware(m message.Middleware)

func (*MeteredClient) Publish added in v0.2.0

func (c *MeteredClient) Publish(topic string, msg *message.Message) error

func (*MeteredClient) RegisterMessage

func (c *MeteredClient) RegisterMessage(msg proto.Message)

func (*MeteredClient) Send

func (c *MeteredClient) Send(msg *message.Message) (payloadSize int, err error)

func (*MeteredClient) SendSync

func (c *MeteredClient) SendSync(ctx context.Context, msg *message.Message) (payloadSize int, response *message.Message, err error)

func (*MeteredClient) Subscribe added in v0.2.0

func (c *MeteredClient) Subscribe(topic string) error

func (*MeteredClient) Unsubscribe added in v0.2.0

func (c *MeteredClient) Unsubscribe(topic string) error

type Option

type Option func(cfg *Cfg)

func WithCompressionEnabled

func WithCompressionEnabled(b bool) Option

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

func WithHandShakeTimeout

func WithHandShakeTimeout(d time.Duration) Option

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

func WithHeartbeatInterval

func WithHeartbeatInterval(interval time.Duration) Option

WithHeartbeatInterval introduces how often the Ping/Pong operation should take place. Right now, this is intended to keep alive connections. Defaults to 5 seconds.

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 WithOnCloseHook

func WithOnCloseHook(h func()) Option

WithOnCloseHook allows the user registering function hooks for when the client 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 client 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 client. 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 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 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 WithServerAddr

func WithServerAddr(addr string) Option

WithServerAddr configures the server to connect to.

func WithTLSConfig

func WithTLSConfig(tlsCfg *tls.Config) Option

WithTLSConfig allows the user to pass a *tls.Config full setup to the client, 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.

Jump to

Keyboard shortcuts

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