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"
|
2021-07-24 16:16:18 +03:00
|
|
|
"time"
|
2016-01-17 02:39:47 +03:00
|
|
|
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/codec"
|
|
|
|
"go.unistack.org/micro/v4/logger"
|
2023-05-09 20:04:15 +03:00
|
|
|
"go.unistack.org/micro/v4/metadata"
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/meter"
|
2023-07-29 00:40:58 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/register"
|
|
|
|
"go.unistack.org/micro/v4/tracer"
|
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 {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Tracer used for tracing
|
|
|
|
Tracer tracer.Tracer
|
|
|
|
// Register can be used for clustering
|
|
|
|
Register register.Register
|
2023-05-09 20:04:15 +03:00
|
|
|
// Codecs holds the codec for marshal/unmarshal
|
|
|
|
Codecs map[string]codec.Codec
|
2021-03-06 19:45:13 +03:00
|
|
|
// Logger used for logging
|
2020-08-29 17:44:49 +03:00
|
|
|
Logger logger.Logger
|
2021-03-06 19:45:13 +03:00
|
|
|
// Meter used for metrics
|
2021-01-22 23:32:33 +03:00
|
|
|
Meter meter.Meter
|
2021-03-06 19:45:13 +03:00
|
|
|
// Context holds external options
|
2016-01-06 19:25:12 +03:00
|
|
|
Context context.Context
|
2021-03-06 19:45:13 +03:00
|
|
|
// TLSConfig holds tls.TLSConfig options
|
|
|
|
TLSConfig *tls.Config
|
2023-07-29 00:40:58 +03:00
|
|
|
// ErrorHandler used when broker have error while processing message
|
|
|
|
ErrorHandler interface{}
|
2021-03-06 19:45:13 +03:00
|
|
|
// Name holds the broker name
|
|
|
|
Name string
|
2023-07-29 00:40:58 +03:00
|
|
|
// Address holds the broker address
|
|
|
|
Address []string
|
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
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewOptions(opts ...options.Option) Options {
|
2020-09-05 02:11:29 +03:00
|
|
|
options := Options{
|
2021-01-29 13:17:32 +03:00
|
|
|
Register: register.DefaultRegister,
|
2020-09-05 02:11:29 +03:00
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Context: context.Background(),
|
2021-01-22 23:32:33 +03:00
|
|
|
Meter: meter.DefaultMeter,
|
2023-05-09 20:04:15 +03:00
|
|
|
Codecs: make(map[string]codec.Codec),
|
2021-01-22 23:32:33 +03:00
|
|
|
Tracer: tracer.DefaultTracer,
|
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
|
|
|
// PublishOptions struct
|
2015-12-31 21:14:40 +03:00
|
|
|
type PublishOptions struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Context holds external options
|
2016-01-06 19:25:12 +03:00
|
|
|
Context context.Context
|
2023-07-29 00:40:58 +03:00
|
|
|
// Message metadata usually passed as message headers
|
|
|
|
Metadata metadata.Metadata
|
|
|
|
// Content-Type of message for marshal
|
|
|
|
ContentType string
|
|
|
|
// Topic destination
|
|
|
|
Topic string
|
2024-03-09 23:35:13 +03:00
|
|
|
// BodyOnly flag says the message contains raw body bytes
|
|
|
|
BodyOnly bool
|
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
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewPublishOptions(opts ...options.Option) 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
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
// PublishTopic pass topic for messages
|
|
|
|
func PublishTopic(t string) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, t, ".Topic")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// SubscribeOptions struct
|
2015-12-23 22:07:26 +03:00
|
|
|
type SubscribeOptions struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Context holds external options
|
|
|
|
Context context.Context
|
2023-07-29 00:40:58 +03:00
|
|
|
// ErrorHandler used when broker have error while processing message
|
|
|
|
ErrorHandler interface{}
|
2023-05-09 20:04:15 +03:00
|
|
|
// QueueGroup holds consumer group
|
|
|
|
QueueGroup string
|
2021-03-06 19:45:13 +03:00
|
|
|
// AutoAck flag specifies auto ack of incoming message when no error happens
|
|
|
|
AutoAck bool
|
|
|
|
// BodyOnly flag specifies that message contains only body bytes without header
|
2020-11-25 08:33:29 +03:00
|
|
|
BodyOnly bool
|
2021-07-24 16:16:18 +03:00
|
|
|
// BatchSize flag specifies max batch size
|
|
|
|
BatchSize int
|
|
|
|
// BatchWait flag specifies max wait time for batch filling
|
|
|
|
BatchWait time.Duration
|
2015-12-23 22:07:26 +03:00
|
|
|
}
|
|
|
|
|
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
|
2023-07-29 00:40:58 +03:00
|
|
|
func ErrorHandler(h interface{}) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, h, ".ErrorHandler")
|
2016-01-17 02:39:47 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-24 00:46:26 +03:00
|
|
|
|
2023-05-09 20:04:15 +03:00
|
|
|
// NewSubscribeOptions creates new SubscribeOptions
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewSubscribeOptions(opts ...options.Option) SubscribeOptions {
|
2023-05-09 20:04:15 +03:00
|
|
|
options := SubscribeOptions{
|
|
|
|
AutoAck: true,
|
|
|
|
Context: context.Background(),
|
2019-01-24 00:46:26 +03:00
|
|
|
}
|
2023-05-09 20:04:15 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
2019-01-24 00:46:26 +03:00
|
|
|
}
|
2021-07-24 16:16:18 +03:00
|
|
|
|
|
|
|
// SubscribeAutoAck contol auto acking of messages
|
|
|
|
// after they have been handled.
|
2023-07-29 00:40:58 +03:00
|
|
|
func SubscribeAutoAck(b bool) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, b, ".AutoAck")
|
2021-07-24 16:16:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
// BodyOnly transfer only body without
|
|
|
|
func BodyOnly(b bool) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, b, ".BodyOnly")
|
2021-07-24 16:16:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeBatchSize specifies max batch size
|
2023-07-29 00:40:58 +03:00
|
|
|
func SubscribeBatchSize(n int) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, n, ".BatchSize")
|
2021-07-24 16:16:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeBatchWait specifies max batch wait time
|
2023-07-29 00:40:58 +03:00
|
|
|
func SubscribeBatchWait(td time.Duration) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, td, ".BatchWait")
|
2021-07-24 16:16:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 20:04:15 +03:00
|
|
|
// SubscribeQueueGroup sets the shared queue name distributed messages across subscribers
|
2023-07-29 00:40:58 +03:00
|
|
|
func SubscribeQueueGroup(n string) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, n, ".QueueGroup")
|
2021-07-24 16:16:18 +03:00
|
|
|
}
|
|
|
|
}
|