micro/broker/options.go

182 lines
3.7 KiB
Go
Raw Normal View History

2015-12-23 22:07:26 +03:00
package broker
import (
2018-03-03 14:53:52 +03:00
"context"
2016-01-17 02:39:47 +03:00
"crypto/tls"
"github.com/unistack-org/micro/v3/codec"
"github.com/unistack-org/micro/v3/logger"
"github.com/unistack-org/micro/v3/registry"
)
type Options struct {
Addrs []string
Secure bool
Codec codec.Marshaler
// Logger
Logger logger.Logger
// Handler executed when errors occur processing messages
ErrorHandler Handler
2016-01-17 02:39:47 +03:00
TLSConfig *tls.Config
// Registry used for clustering
Registry registry.Registry
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
}
2015-12-23 22:07:26 +03:00
func NewOptions(opts ...Option) Options {
options := Options{
Registry: registry.DefaultRegistry,
Logger: logger.DefaultLogger,
Context: context.Background(),
}
for _, o := range opts {
o(&options)
}
return options
}
type PublishOptions struct {
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
}
2015-12-23 22:07:26 +03:00
type SubscribeOptions struct {
// AutoAck ack messages if handler returns nil err
AutoAck bool
// Handler executed when errors occur processing messages
ErrorHandler Handler
// Subscribers with the same group name
2015-12-23 23:05:47 +03:00
// will create a shared subscription where each
// receives a subset of messages.
Group string
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
2015-12-23 22:07:26 +03:00
}
type Option func(*Options)
type PublishOption func(*PublishOptions)
// PublishContext set context
func PublishContext(ctx context.Context) PublishOption {
return func(o *PublishOptions) {
o.Context = ctx
}
}
2015-12-23 22:07:26 +03:00
type SubscribeOption func(*SubscribeOptions)
2018-11-30 20:32:48 +03:00
func NewSubscribeOptions(opts ...SubscribeOption) SubscribeOptions {
opt := SubscribeOptions{
AutoAck: true,
Context: context.Background(),
}
2016-01-17 01:13:02 +03:00
for _, o := range opts {
o(&opt)
}
return opt
}
2016-03-16 01:12:28 +03:00
// Addrs sets the host addresses to be used by the broker
func Addrs(addrs ...string) Option {
return func(o *Options) {
o.Addrs = addrs
}
}
2016-12-06 21:59:41 +03:00
// Codec sets the codec used for encoding/decoding used where
// a broker does not support headers
2019-01-10 12:42:02 +03:00
func Codec(c codec.Marshaler) Option {
2016-12-06 21:59:41 +03:00
return func(o *Options) {
o.Codec = c
}
}
func DisableAutoAck() SubscribeOption {
return func(o *SubscribeOptions) {
o.AutoAck = false
}
}
// SubscribeAutoAck will disable auto acking of messages
// after they have been handled.
func SubscribeAutoAck(b bool) SubscribeOption {
return func(o *SubscribeOptions) {
o.AutoAck = b
}
}
// ErrorHandler will catch all broker errors that cant be handled
// in normal way, for example Codec errors
func ErrorHandler(h Handler) Option {
return func(o *Options) {
o.ErrorHandler = h
}
}
// SubscribeErrorHandler will catch all broker errors that cant be handled
// in normal way, for example Codec errors
func SubscribeErrorHandler(h Handler) SubscribeOption {
return func(o *SubscribeOptions) {
o.ErrorHandler = h
}
}
func Queue(name string) SubscribeOption {
return func(o *SubscribeOptions) {
o.Group = name
}
}
// SubscribeGroup sets the name of the queue to share messages on
func SubscribeGroup(name string) SubscribeOption {
2015-12-23 23:05:47 +03:00
return func(o *SubscribeOptions) {
o.Group = name
2015-12-23 23:05:47 +03:00
}
}
2016-01-20 18:22:44 +03:00
func Registry(r registry.Registry) Option {
return func(o *Options) {
o.Registry = r
2016-01-20 18:22:44 +03:00
}
}
2016-01-17 01:13:02 +03:00
// Secure communication with the broker
func Secure(b bool) Option {
return func(o *Options) {
o.Secure = b
2015-12-23 22:07:26 +03:00
}
}
2016-01-17 02:39:47 +03:00
// Specify TLS Config
func TLSConfig(t *tls.Config) Option {
return func(o *Options) {
o.TLSConfig = t
}
}
// Logger sets the logger
func Logger(l logger.Logger) Option {
return func(o *Options) {
o.Logger = l
}
}
// SubscribeContext set context
func SubscribeContext(ctx context.Context) SubscribeOption {
return func(o *SubscribeOptions) {
o.Context = ctx
}
}