broker

package
v1.0.23 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package broker is an interface used for asynchronous messaging

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect() error

func Disconnect

func Disconnect() error

func Init

func Init(opts ...Option) error

func Publish

func Publish(ctx context.Context, topic string, msg *Message, opts ...PublishOption) error

func String

func String() string

String returns the name of the Broker.

Types

type Broker

type Broker interface {
	Init(...Option) error
	Options() Options
	Address() string
	Connect() error
	Disconnect() error
	Publish(ctx context.Context, topic string, m *Message, opts ...PublishOption) error
	Subscribe(topic string, h Handler, opts ...SubscribeOption) (Subscriber, error)
	String() string
}

Broker is an interface used for asynchronous messaging.

var (
	// DefaultBroker is the default Broker.
	DefaultBroker Broker = NewMemoryBroker()
)

func NewMemoryBroker

func NewMemoryBroker(opts ...Option) Broker

type Event

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

Event is given to a subscription handler for processing.

type Handler

type Handler func(context.Context, 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 a message send/received from the broker.

type Option

type Option func(*Options)

func Addrs

func Addrs(addrs ...string) Option

Addrs sets the host addresses to be used by the broker.

func Codec

func Codec(c encoding.Codec) Option

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

func DisableAutoAck

func DisableAutoAck() Option

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

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 Logger

func Logger(l logger.Logger) Option

Logger sets the underline logger.

func Queue

func Queue(name string) Option

Queue sets the name of the queue to share messages on.

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 Wrap added in v1.0.5

func Wrap(w ...Wrapper) Option

Wrap a broker as a wrapper

type Options

type Options struct {
	Codec encoding.Codec

	// Logger is the underlying logger
	Logger logger.Logger

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

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

	TLSConfig *tls.Config
	Addrs     []string
	Secure    bool

	PublishOptions   PublishOptions
	SubscribeOptions SubscribeOptions

	// Middleware for client
	Wrappers []Wrapper
}

func NewOptions

func NewOptions(opts ...Option) *Options

type PublishOption

type PublishOption func(*PublishOptions)

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
}

type SubscribeOption

type SubscribeOption func(*SubscribeOptions)

func SubscribeContext

func SubscribeContext(ctx context.Context) SubscribeOption

SubscribeContext set context.

func SubscribeDisableAutoAck

func SubscribeDisableAutoAck() SubscribeOption

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

func SubscribeQueue

func SubscribeQueue(name string) SubscribeOption

SubscribeQueue sets the name of the queue to share messages on.

type SubscribeOptions

type SubscribeOptions struct {
	// Subscribers with the same queue name
	// will create a shared subscription where each
	// receives a subset of messages.
	Queue string

	// AutoAck defaults to true. When a handler returns
	// with a nil error the message is acked.
	AutoAck bool
	// Other options for implementations of the interface
	// can be stored in a context
	Context context.Context
}

func NewSubscribeOptions

func NewSubscribeOptions(opts ...SubscribeOption) SubscribeOptions

type Subscriber

type Subscriber interface {
	Options() SubscribeOptions
	Topic() string
	Unsubscribe() error
}

Subscriber is a convenience return type for the Subscribe method.

func Subscribe

func Subscribe(topic string, handler Handler, opts ...SubscribeOption) (Subscriber, error)

type Wrapper added in v1.0.5

type Wrapper func(Broker) Broker

Wrapper wraps a Broker and returns a Broker

func Chain added in v1.0.5

func Chain(m ...Wrapper) Wrapper

Chain returns a Broker that specifies the chained handler for endpoint.

Jump to

Keyboard shortcuts

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