2015-12-23 21:07:26 +02:00
|
|
|
package broker
|
|
|
|
|
2016-01-06 18:25:12 +02:00
|
|
|
import (
|
2018-03-03 13:53:52 +02:00
|
|
|
"context"
|
2016-01-17 01:39:47 +02:00
|
|
|
"crypto/tls"
|
|
|
|
|
2021-10-12 13:55:53 +02:00
|
|
|
"go-micro.dev/v4/codec"
|
2022-09-29 16:44:53 +02:00
|
|
|
"go-micro.dev/v4/logger"
|
2021-10-12 13:55:53 +02:00
|
|
|
"go-micro.dev/v4/registry"
|
2016-01-06 18:25:12 +02:00
|
|
|
)
|
|
|
|
|
2015-12-31 20:11:46 +02:00
|
|
|
type Options struct {
|
2020-03-06 23:25:16 +02:00
|
|
|
Addrs []string
|
|
|
|
Secure bool
|
|
|
|
Codec codec.Marshaler
|
|
|
|
|
2022-09-29 16:44:53 +02:00
|
|
|
// Logger is the underlying logger
|
|
|
|
Logger logger.Logger
|
|
|
|
|
2020-03-06 23:25:16 +02:00
|
|
|
// Handler executed when error happens in broker mesage
|
|
|
|
// processing
|
|
|
|
ErrorHandler Handler
|
|
|
|
|
2016-01-17 01:39:47 +02:00
|
|
|
TLSConfig *tls.Config
|
2020-01-14 15:23:16 +02:00
|
|
|
// Registry used for clustering
|
|
|
|
Registry registry.Registry
|
2016-01-06 18:25:12 +02:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2015-12-31 20:11:46 +02:00
|
|
|
}
|
2015-12-23 21:07:26 +02:00
|
|
|
|
2015-12-31 20:14:40 +02:00
|
|
|
type PublishOptions struct {
|
2016-01-06 18:25:12 +02:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2015-12-31 20:14:40 +02:00
|
|
|
}
|
2015-12-23 21:07:26 +02:00
|
|
|
|
|
|
|
type SubscribeOptions struct {
|
2015-12-23 22:26:13 +02:00
|
|
|
// AutoAck defaults to true. When a handler returns
|
|
|
|
// with a nil error the message is acked.
|
2015-12-23 21:07:26 +02:00
|
|
|
AutoAck bool
|
2015-12-23 22:05:47 +02:00
|
|
|
// Subscribers with the same queue name
|
|
|
|
// will create a shared subscription where each
|
|
|
|
// receives a subset of messages.
|
|
|
|
Queue string
|
2015-12-31 20:14:40 +02:00
|
|
|
|
2016-01-06 18:25:12 +02:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2015-12-23 21:07:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
type PublishOption func(*PublishOptions)
|
|
|
|
|
2020-04-28 18:29:00 +02:00
|
|
|
// PublishContext set context
|
|
|
|
func PublishContext(ctx context.Context) PublishOption {
|
|
|
|
return func(o *PublishOptions) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-23 21:07:26 +02:00
|
|
|
type SubscribeOption func(*SubscribeOptions)
|
|
|
|
|
2022-09-29 16:44:53 +02:00
|
|
|
func NewOptions(opts ...Option) *Options {
|
|
|
|
options := Options{
|
|
|
|
Context: context.Background(),
|
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &options
|
|
|
|
}
|
|
|
|
|
2018-11-30 19:32:48 +02:00
|
|
|
func NewSubscribeOptions(opts ...SubscribeOption) SubscribeOptions {
|
2016-01-17 00:13:02 +02:00
|
|
|
opt := SubscribeOptions{
|
|
|
|
AutoAck: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return opt
|
|
|
|
}
|
|
|
|
|
2016-03-16 00:12:28 +02: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 20:59:41 +02:00
|
|
|
// Codec sets the codec used for encoding/decoding used where
|
|
|
|
// a broker does not support headers
|
2019-01-10 11:42:02 +02:00
|
|
|
func Codec(c codec.Marshaler) Option {
|
2016-12-06 20:59:41 +02:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Codec = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-23 21:07:26 +02:00
|
|
|
// DisableAutoAck will disable auto acking of messages
|
|
|
|
// after they have been handled.
|
|
|
|
func DisableAutoAck() SubscribeOption {
|
|
|
|
return func(o *SubscribeOptions) {
|
|
|
|
o.AutoAck = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 23:25:16 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 11:55:18 +02:00
|
|
|
// Queue sets the name of the queue to share messages on
|
|
|
|
func Queue(name string) SubscribeOption {
|
2015-12-23 22:05:47 +02:00
|
|
|
return func(o *SubscribeOptions) {
|
|
|
|
o.Queue = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 17:22:44 +02:00
|
|
|
func Registry(r registry.Registry) Option {
|
|
|
|
return func(o *Options) {
|
2020-01-14 15:23:16 +02:00
|
|
|
o.Registry = r
|
2016-01-20 17:22:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 00:13:02 +02:00
|
|
|
// Secure communication with the broker
|
|
|
|
func Secure(b bool) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Secure = b
|
2015-12-23 21:07:26 +02:00
|
|
|
}
|
|
|
|
}
|
2016-01-17 01:39:47 +02:00
|
|
|
|
|
|
|
// Specify TLS Config
|
|
|
|
func TLSConfig(t *tls.Config) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.TLSConfig = t
|
|
|
|
}
|
|
|
|
}
|
2019-01-23 23:46:26 +02:00
|
|
|
|
2022-09-29 16:44:53 +02:00
|
|
|
// Logger sets the underline logger
|
|
|
|
func Logger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 23:46:26 +02:00
|
|
|
// SubscribeContext set context
|
|
|
|
func SubscribeContext(ctx context.Context) SubscribeOption {
|
|
|
|
return func(o *SubscribeOptions) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|