nats

package module
v0.0.0-...-6a5111f Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package nats provide a message broker using NATS.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Addrs    string        `envconfig:"NATS_ADDRS" default:"nats://localhost:4222"`
	Codec    string        `envconfig:"NATS_CODEC" default:"proto"`
	Timeout  time.Duration `envconfig:"NATS_TIMEOUT" default:"10s"`
	Username string        `envconfig:"NATS_USERNAME"`
	Password string        `envconfig:"NATS_PASSWORD"`
}

Config hold common NATS configurations.

func ReadConfigFromEnv

func ReadConfigFromEnv(opts ...config.ReadOption) Config

ReadConfigFromEnv read NATS configuration from environment variables.

type Nats

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

Nats is an implementation of broker.Broker using NATS.

func New

func New(opts ...Option) *Nats

New return a new NATs message broker. If address is not set, default address "nats:4222" will be used.

func (*Nats) CheckHealth

func (n *Nats) CheckHealth(ctx context.Context) error

CheckHealth implements health.Checker.

func (*Nats) Close

func (n *Nats) Close(ctx context.Context) error

Close flush in-flight messages and close the underlying connection.

func (*Nats) Open

func (n *Nats) Open(ctx context.Context) error

Open connect to target server.

func (*Nats) Publish

func (n *Nats) Publish(ctx context.Context, topic string, m *broker.Message, opts ...broker.PublishOption) error

Publish implements broker.Broker interface.

func (*Nats) Subscribe

func (n *Nats) Subscribe(ctx context.Context, topic string, h broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error)

Subscribe implements broker.Broker interface.

type Option

type Option func(*Nats)

Option is an optional configuration.

func Address

func Address(addrs string) Option

Address is an option to set target addresses of NATS server. Multiple addresses are separated by comma.

func Codec

func Codec(codec encoding.Codec) Option

Codec is an option to provide a custom codec.

func FromConfig

func FromConfig(conf Config) Option

FromConfig is an option to create new broker from an existing config.

func FromEnv

func FromEnv(opts ...config.ReadOption) Option

FromEnv is an option to create new broker base on environment variables.

func Logger

func Logger(logger log.Logger) Option

Logger is an option to provide custom logger.

func Options

func Options(opts ...nats.Option) Option

Options is an option to provide additional nats.Option.

Jump to

Keyboard shortcuts

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