v1.2.11 Latest Latest

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

Go to latest
Published: Jun 27, 2024 License: MIT Imports: 12 Imported by: 0



Package nats provides a NATS Conn



This section is empty.


This section is empty.


This section is empty.


type Conn

type Conn interface {
	Init(...Option) error
	Options() Options
	Address() string
	Connect() error
	Disconnect() error
	Publish(topic string, m []byte, opts ...PublishOption) error
	Subscribe(topic string, h Handler, opts ...SubscribeOption) (Subscriber, error)
	Request(topic string, data []byte, timeout time.Duration) (*Message, error)
	RequestWithContext(ctx context.Context, topic string, data []byte) (*Message, error)
	String() string
	GetConn() *nats.Conn

Conn is an interface used for asynchronous messaging.

func NewConn

func NewConn(opts ...Option) Conn

NewConn .

type Event

type Event interface {
	Topic() string
	Message() *Message
	Reply() string
	Ack() error
	Error() error

Event is given to a subscription handler for processing

type EventCallback

type EventCallback func(context.Context, Event) error

type Handler

type Handler func(Event) error

Handler is used to process messages via a subscription of a topic. The handler is passed a publication interface which contains the message and optional Ack method to acknowledge receipt of the message.

type Message

type Message struct {
	Header map[string]string
	Body   []byte

Message is data

type Option

type Option func(*Options)

Option is conn option

func Addrs

func Addrs(addrs ...string) Option

Addrs sets the host addresses to be used by the broker

func Codec

func Codec(c codec.Marshaler) Option

Codec sets the codec used for encoding/decoding used where a broker does not support headers

func DrainConnection

func DrainConnection() Option

DrainConnection will drain subscription on close

func ErrorHandler

func ErrorHandler(h Handler) Option

ErrorHandler will catch all broker errors that cant be handled in normal way, for example Codec errors

func Secure

func Secure(b bool) Option

Secure communication with the broker

func TLSConfig

func TLSConfig(t *tls.Config) Option

TLSConfig Specify TLS Config

func Trace

func Trace(enable bool) Option

type Options

type Options struct {
	Addrs  []string
	Secure bool
	Codec  codec.Marshaler

	// Handler executed when error happens in broker mesage
	// processing
	ErrorHandler Handler

	TLSConfig *tls.Config
	// Registry used for clustering
	// Other options for implementations of the interface
	// can be stored in a context
	Context context.Context
	Trace   bool

Options .

type Publication

type Publication struct {
	// contains filtered or unexported fields

Publication .

func (*Publication) Ack

func (p *Publication) Ack() error

Ack .

func (*Publication) Error

func (p *Publication) Error() error

func (*Publication) Message

func (p *Publication) Message() *Message

Message .

func (*Publication) Reply

func (p *Publication) Reply() string

Reply .

func (*Publication) Topic

func (p *Publication) Topic() string

Topic .

type PublishOption

type PublishOption func(*PublishOptions)

PublishOption is Publish Option callbcak

func PublishContext

func PublishContext(ctx context.Context) PublishOption

PublishContext set context

type PublishOptions

type PublishOptions struct {
	// Other options for implementations of the interface
	// can be stored in a context
	Context context.Context

PublishOptions is Publish meesage options

type SubscribeOption

type SubscribeOption func(*SubscribeOptions)

SubscribeOption is subscribe option

func DisableAutoAck

func DisableAutoAck() SubscribeOption

DisableAutoAck will disable auto acking of messages after they have been handled.

func DrainSubscription

func DrainSubscription() SubscribeOption

DrainSubscription will drain pending messages when unsubscribe

func Queue

func Queue(name string) SubscribeOption

Queue sets the name of the queue to share messages on

func SubscribeContext

func SubscribeContext(ctx context.Context) SubscribeOption

SubscribeContext set context

type SubscribeOptions

type SubscribeOptions struct {
	// AutoAck defaults to true. When a handler returns
	// with a nil error the message is acked.
	AutoAck bool
	// Subscribers with the same queue name
	// will create a shared subscription where each
	// receives a subset of messages.
	Queue string

	// Other options for implementations of the interface
	// can be stored in a context
	Context context.Context

SubscribeOptions is Subscribe options

func NewSubscribeOptions

func NewSubscribeOptions(opts ...SubscribeOption) SubscribeOptions

NewSubscribeOptions new subscribe options

type Subscriber

type Subscriber interface {
	Options() SubscribeOptions
	Topic() string
	Unsubscribe() error
	SetPendingLimits(msgLimit, bytesLimit int) error

Subscriber is a convenience return type for the Subscribe method

Jump to

Keyboard shortcuts

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