nats

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package nats provide a message broker using NATS.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMissingEncoder report the encoder is missing.
	ErrMissingEncoder = errors.New("nats: missing encoder")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Addrs    string        `envconfig:"NATS_ADDRS" default:"nats:4222"`
	Encoder  string        `envconfig:"NATS_ENCODER" 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.

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) Close added in v0.1.2

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

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

func (*Nats) Connect added in v0.1.2

func (n *Nats) Connect() error

Connect connect to target server.

func (*Nats) HealthCheck

func (n *Nats) HealthCheck() health.CheckFunc

HealthCheck return a health check func.

func (*Nats) Publish

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

Publish implements broker.Broker interface.

func (*Nats) Subscribe

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

Subscribe implements broker.Broker interface.

type Option added in v0.0.9

type Option func(*Nats)

Option is an optional configuration.

func Address added in v0.1.0

func Address(addrs string) Option

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

func Encoder added in v0.0.9

func Encoder(encoder broker.Encoder) Option

Encoder is an option to provide a custom encoder.

func FromConfig added in v0.1.0

func FromConfig(conf Config) Option

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

func FromEnv added in v0.0.9

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

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

func Options added in v0.0.9

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