options

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 11 Imported by: 3

Documentation

Overview

options package is a package that stores publisher's configuration and has convenience functions to configure it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeNats

func MakeNats(name, urls, userCreds, nkey, jwt, caCert, clientCert, clientKey string) (*nats.Conn, error)

MakeNats is a convenience function to create NATS connection from various parameters.

func Param

func Param[T any](o Options, key string, dflt T) T

Param is a generic helper function that will return a parameter stored in Options. It will return a default if no key is found. Default is also used for type inference.

Types

type Codec

type Codec interface {
	Encode(nmsg []byte, msg any) ([]byte, error)
	Decode(nmsg []byte, msg any) error
}

Codec represents a message encoder and decoder

type ContextKeyType

type ContextKeyType string
const (
	CancelFuncKey ContextKeyType = "cancel"
	GroupFuncKey  ContextKeyType = "group"
)

type NatsConn

type NatsConn interface {
	Subscribe(subj string, cb nats.MsgHandler) (*nats.Subscription, error)
	QueueSubscribe(subj, queue string, cb nats.MsgHandler) (*nats.Subscription, error)
	PublishMsg(m *nats.Msg) error
	RequestMsgWithContext(ctx context.Context, msg *nats.Msg) (*nats.Msg, error)
	Flush() error
}

type Option

type Option func(*Options)

type Options

type Options struct {
	// The global context
	Context context.Context
	// The global error group that should be used to start internal goroutines.
	// .Close() methods will wait on this group until all goroutines have exited.
	// It will cancel the Context if any of the goroutines exit with an error.
	Group *errgroup.Group
	// Cancel function is used to initiate the shutdown of the service.
	Cancel context.CancelCauseFunc

	// Include verbose logging
	VerboseLog bool
	Logger     *slog.Logger
	// Determines how often to send telemetry messages.
	TelemetryPeriod time.Duration

	// The size of the publish queue
	PublishQueueSize int
	// The size of the subscriber's queue
	SubscribeQueueSize int

	// Publishing NATS connection
	PubNats NatsConn
	// Subscribing NATS connection
	SubNats NatsConn

	// The private key for this publisher.
	// This key is used to sign the messages and is used to derive the identity.
	PrivateKey crypto.PrivateKey
	// Identity is used internally by publishers and is generated from the private key
	Identity string

	// A collection of known public keys from which we are allowed to accept messages from.
	KnownPublicKeys map[string]ed25519.PublicKey

	// Subject prefix for publishing.
	Prefix string
	// Name of the NATS queue
	QueueName string
	// Name of the publisher
	Name string
	// Codec is used to marshal published messages to whire format.
	// Can also be used to unmarshal received messages.
	Codec Codec

	// Other generic parameters that can be obtained using generic Param function.
	Params map[string]any
}

Options struct used by the publisher. This structure shouldn't be modified by the user, however, it is open for modification if needed.

func (Options) Param

func (o Options) Param(key string, defaults ...any) any

Param will return a parameter if it was stored by the provided key. Otherwise it will return a default(first optional argument) or nil.

func (*Options) Parse

func (o *Options) Parse(opts ...Option) error

Parse initializes the Options with default values and applies any functional option modifiers. Generally this must be called in `New` function that creates a publisher.

func (*Options) SetContext

func (o *Options) SetContext(ctxMain context.Context)

func (Options) Subject

func (o Options) Subject(suffixes ...string) string

Subject returns a constructed subject from base prefix and suffixes (subtopics) like so: prefix := {Prefix}.{Name} subject := {prefix}.{suffix1}.{suffix2}...

Jump to

Keyboard shortcuts

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