2015-12-23 22:07:26 +03:00
|
|
|
package broker
|
|
|
|
|
2016-01-06 19:25:12 +03:00
|
|
|
import (
|
2018-03-03 14:53:52 +03:00
|
|
|
"context"
|
2016-01-17 02:39:47 +03:00
|
|
|
"crypto/tls"
|
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
2020-08-29 17:44:49 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/registry"
|
2016-01-06 19:25:12 +03:00
|
|
|
)
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// Options struct
|
2015-12-31 21:11:46 +03:00
|
|
|
type Options struct {
|
2020-03-07 00:25:16 +03:00
|
|
|
Addrs []string
|
|
|
|
Secure bool
|
|
|
|
Codec codec.Marshaler
|
|
|
|
|
2020-08-29 17:44:49 +03:00
|
|
|
// Logger
|
|
|
|
Logger logger.Logger
|
2020-08-25 13:44:41 +03:00
|
|
|
// Handler executed when errors occur processing messages
|
|
|
|
ErrorHandler Handler
|
|
|
|
|
2016-01-17 02:39:47 +03:00
|
|
|
TLSConfig *tls.Config
|
2020-01-14 16:23:16 +03:00
|
|
|
// Registry used for clustering
|
|
|
|
Registry registry.Registry
|
2016-01-06 19:25:12 +03:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2015-12-31 21:11:46 +03:00
|
|
|
}
|
2015-12-23 22:07:26 +03:00
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// NewOptions create new Options
|
2020-09-05 02:11:29 +03:00
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
|
|
|
Registry: registry.DefaultRegistry,
|
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Context: context.Background(),
|
2020-08-27 11:18:02 +03:00
|
|
|
}
|
2020-09-05 02:11:29 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
2020-08-27 11:18:02 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// Context sets the context option
|
2020-10-16 09:38:57 +03:00
|
|
|
func Context(ctx context.Context) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// PublishOptions struct
|
2015-12-31 21:14:40 +03:00
|
|
|
type PublishOptions struct {
|
2016-01-06 19:25:12 +03:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2015-12-31 21:14:40 +03:00
|
|
|
}
|
2015-12-23 22:07:26 +03:00
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// NewPublishOptions creates PublishOptions struct
|
2020-10-10 00:47:09 +03:00
|
|
|
func NewPublishOptions(opts ...PublishOption) PublishOptions {
|
2020-11-03 01:08:23 +03:00
|
|
|
options := PublishOptions{
|
2020-10-10 00:47:09 +03:00
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
2020-11-03 01:08:23 +03:00
|
|
|
o(&options)
|
2020-10-10 00:47:09 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
return options
|
2020-10-10 00:47:09 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// SubscribeOptions struct
|
2015-12-23 22:07:26 +03:00
|
|
|
type SubscribeOptions struct {
|
2020-08-25 13:44:41 +03:00
|
|
|
// AutoAck ack messages if handler returns nil err
|
|
|
|
AutoAck bool
|
|
|
|
|
2020-08-18 16:00:51 +03:00
|
|
|
// Handler executed when errors occur processing messages
|
2020-08-25 13:44:41 +03:00
|
|
|
ErrorHandler Handler
|
2020-08-18 16:00:51 +03:00
|
|
|
|
2020-08-25 13:44:41 +03:00
|
|
|
// 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.
|
2020-08-25 13:44:41 +03:00
|
|
|
Group string
|
2015-12-31 21:14:40 +03:00
|
|
|
|
2016-01-06 19:25:12 +03:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2015-12-23 22:07:26 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// Option func
|
2015-12-23 22:07:26 +03:00
|
|
|
type Option func(*Options)
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// PublishOption func
|
2015-12-23 22:07:26 +03:00
|
|
|
type PublishOption func(*PublishOptions)
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// PublishContext sets the context
|
2020-04-28 19:29:00 +03:00
|
|
|
func PublishContext(ctx context.Context) PublishOption {
|
|
|
|
return func(o *PublishOptions) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// SubscribeOption func
|
2015-12-23 22:07:26 +03:00
|
|
|
type SubscribeOption func(*SubscribeOptions)
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// NewSubscribeOptions creates new SubscribeOptions
|
2018-11-30 20:32:48 +03:00
|
|
|
func NewSubscribeOptions(opts ...SubscribeOption) SubscribeOptions {
|
2020-11-03 01:08:23 +03:00
|
|
|
options := SubscribeOptions{
|
2020-08-27 11:18:02 +03:00
|
|
|
AutoAck: true,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
2016-01-17 01:13:02 +03:00
|
|
|
|
|
|
|
for _, o := range opts {
|
2020-11-03 01:08:23 +03:00
|
|
|
o(&options)
|
2016-01-17 01:13:02 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
return options
|
2016-01-17 01:13:02 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// DisableAutoAck disables auto ack
|
2020-08-31 16:51:49 +03:00
|
|
|
func DisableAutoAck() SubscribeOption {
|
|
|
|
return func(o *SubscribeOptions) {
|
|
|
|
o.AutoAck = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 11:18:02 +03:00
|
|
|
// SubscribeAutoAck will disable auto acking of messages
|
|
|
|
// after they have been handled.
|
|
|
|
func SubscribeAutoAck(b bool) SubscribeOption {
|
|
|
|
return func(o *SubscribeOptions) {
|
|
|
|
o.AutoAck = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 00:25:16 +03:00
|
|
|
// ErrorHandler will catch all broker errors that cant be handled
|
|
|
|
// in normal way, for example Codec errors
|
2020-08-25 13:44:41 +03:00
|
|
|
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 {
|
2020-08-18 16:00:51 +03:00
|
|
|
return func(o *SubscribeOptions) {
|
2020-03-07 00:25:16 +03:00
|
|
|
o.ErrorHandler = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// Queue sets the subscribers sueue
|
2020-08-31 16:48:29 +03:00
|
|
|
func Queue(name string) SubscribeOption {
|
|
|
|
return func(o *SubscribeOptions) {
|
|
|
|
o.Group = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 13:44:41 +03:00
|
|
|
// 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) {
|
2020-08-25 13:44:41 +03:00
|
|
|
o.Group = name
|
2015-12-23 23:05:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// Registry sets registry option
|
2016-01-20 18:22:44 +03:00
|
|
|
func Registry(r registry.Registry) Option {
|
|
|
|
return func(o *Options) {
|
2020-01-14 16:23:16 +03:00
|
|
|
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
|
|
|
|
2020-11-03 02:02:32 +03:00
|
|
|
// TLSConfig sets the TLS Config
|
2016-01-17 02:39:47 +03:00
|
|
|
func TLSConfig(t *tls.Config) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.TLSConfig = t
|
|
|
|
}
|
|
|
|
}
|
2019-01-24 00:46:26 +03:00
|
|
|
|
2020-08-29 17:44:49 +03:00
|
|
|
// Logger sets the logger
|
|
|
|
func Logger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 00:46:26 +03:00
|
|
|
// SubscribeContext set context
|
|
|
|
func SubscribeContext(ctx context.Context) SubscribeOption {
|
|
|
|
return func(o *SubscribeOptions) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|