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"
|
|
|
|
"go.unistack.org/micro/v4/meter"
|
|
|
|
"go.unistack.org/micro/v4/register"
|
|
|
|
"go.unistack.org/micro/v4/tracer"
|
2016-01-06 19:25:12 +03:00
|
|
|
)
|
|
|
|
|
2023-04-27 15:30:55 +03:00
|
|
|
/*
|
|
|
|
|
|
|
|
func (w *wrapper) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
|
|
|
|
endpoint := p.Topic()
|
|
|
|
|
|
|
|
labels := make([]string, 0, 4)
|
|
|
|
labels = append(labels, labelEndpoint, endpoint)
|
|
|
|
|
|
|
|
w.opts.Meter.Counter(PublishMessageInflight, labels...).Inc()
|
|
|
|
ts := time.Now()
|
|
|
|
err := w.Client.Publish(ctx, p, opts...)
|
|
|
|
te := time.Since(ts)
|
|
|
|
w.opts.Meter.Counter(PublishMessageInflight, labels...).Dec()
|
|
|
|
|
|
|
|
w.opts.Meter.Summary(PublishMessageLatencyMicroseconds, labels...).Update(te.Seconds())
|
|
|
|
w.opts.Meter.Histogram(PublishMessageDurationSeconds, labels...).Update(te.Seconds())
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
labels = append(labels, labelStatus, labelSuccess)
|
|
|
|
} else {
|
|
|
|
labels = append(labels, labelStatus, labelFailure)
|
|
|
|
}
|
|
|
|
w.opts.Meter.Counter(PublishMessageTotal, labels...).Inc()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
var (
|
|
|
|
// PublishMessageDurationSeconds specifies meter metric name
|
|
|
|
PublishMessageDurationSeconds = "publish_message_duration_seconds"
|
|
|
|
// PublishMessageLatencyMicroseconds specifies meter metric name
|
|
|
|
PublishMessageLatencyMicroseconds = "publish_message_latency_microseconds"
|
|
|
|
// PublishMessageTotal specifies meter metric name
|
|
|
|
PublishMessageTotal = "publish_message_total"
|
|
|
|
// PublishMessageInflight specifies meter metric name
|
|
|
|
PublishMessageInflight = "publish_message_inflight"
|
|
|
|
// SubscribeMessageDurationSeconds specifies meter metric name
|
|
|
|
SubscribeMessageDurationSeconds = "subscribe_message_duration_seconds"
|
|
|
|
// SubscribeMessageLatencyMicroseconds specifies meter metric name
|
|
|
|
SubscribeMessageLatencyMicroseconds = "subscribe_message_latency_microseconds"
|
|
|
|
// SubscribeMessageTotal specifies meter metric name
|
|
|
|
SubscribeMessageTotal = "subscribe_message_total"
|
|
|
|
// SubscribeMessageInflight specifies meter metric name
|
|
|
|
SubscribeMessageInflight = "subscribe_message_inflight"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
// Codec holds the codec for marshal/unmarshal
|
2020-12-09 12:10:25 +03:00
|
|
|
Codec 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
|
|
|
|
// ErrorHandler used when broker can't unmarshal incoming message
|
|
|
|
ErrorHandler Handler
|
2021-07-22 22:53:44 +03:00
|
|
|
// BatchErrorHandler used when broker can't unmashal incoming messages
|
|
|
|
BatchErrorHandler BatchHandler
|
2021-03-06 19:45:13 +03:00
|
|
|
// Name holds the broker name
|
|
|
|
Name string
|
|
|
|
// Addrs holds the broker address
|
|
|
|
Addrs []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
|
2020-09-05 02:11:29 +03:00
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
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,
|
|
|
|
Codec: codec.DefaultCodec,
|
|
|
|
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
|
|
|
// 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 {
|
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
|
|
|
// 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
|
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 {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Context holds external options
|
|
|
|
Context context.Context
|
|
|
|
// ErrorHandler used when broker can't unmarshal incoming message
|
2020-08-25 13:44:41 +03:00
|
|
|
ErrorHandler Handler
|
2021-07-22 22:53:44 +03:00
|
|
|
// BatchErrorHandler used when broker can't unmashal incoming messages
|
|
|
|
BatchErrorHandler BatchHandler
|
2021-03-06 19:45:13 +03:00
|
|
|
// Group holds consumer group
|
2020-08-25 13:44:41 +03:00
|
|
|
Group 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-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-25 08:33:29 +03:00
|
|
|
// PublishBodyOnly publish only body of the message
|
|
|
|
func PublishBodyOnly(b bool) PublishOption {
|
|
|
|
return func(o *PublishOptions) {
|
|
|
|
o.BodyOnly = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2020-11-23 16:18:47 +03:00
|
|
|
func Codec(c codec.Codec) Option {
|
2016-12-06 21:59:41 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Codec = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 22:53:44 +03:00
|
|
|
// BatchErrorHandler will catch all broker errors that cant be handled
|
|
|
|
// in normal way, for example Codec errors
|
|
|
|
func BatchErrorHandler(h BatchHandler) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.BatchErrorHandler = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 13:44:41 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 22:53:44 +03:00
|
|
|
// SubscribeBatchErrorHandler will catch all broker errors that cant be handled
|
|
|
|
// in normal way, for example Codec errors
|
|
|
|
func SubscribeBatchErrorHandler(h BatchHandler) SubscribeOption {
|
|
|
|
return func(o *SubscribeOptions) {
|
|
|
|
o.BatchErrorHandler = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// Queue sets the subscribers queue
|
|
|
|
// Deprecated
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// Register sets register option
|
|
|
|
func Register(r register.Register) Option {
|
2016-01-20 18:22:44 +03:00
|
|
|
return func(o *Options) {
|
2021-01-29 13:17:32 +03:00
|
|
|
o.Register = r
|
2016-01-20 18:22:44 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:32:33 +03:00
|
|
|
// Tracer to be used for tracing
|
|
|
|
func Tracer(t tracer.Tracer) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Tracer = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Meter sets the meter
|
|
|
|
func Meter(m meter.Meter) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Meter = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// Name sets the name
|
|
|
|
func Name(n string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 00:46:26 +03:00
|
|
|
// SubscribeContext set context
|
|
|
|
func SubscribeContext(ctx context.Context) SubscribeOption {
|
|
|
|
return func(o *SubscribeOptions) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
2021-07-24 16:16:18 +03:00
|
|
|
|
|
|
|
// DisableAutoAck disables auto ack
|
2021-07-27 23:58:06 +03:00
|
|
|
// Deprecated
|
2021-07-24 16:16:18 +03:00
|
|
|
func DisableAutoAck() SubscribeOption {
|
|
|
|
return func(o *SubscribeOptions) {
|
|
|
|
o.AutoAck = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeAutoAck contol auto acking of messages
|
|
|
|
// after they have been handled.
|
|
|
|
func SubscribeAutoAck(b bool) SubscribeOption {
|
|
|
|
return func(o *SubscribeOptions) {
|
|
|
|
o.AutoAck = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeBodyOnly consumes only body of the message
|
|
|
|
func SubscribeBodyOnly(b bool) SubscribeOption {
|
|
|
|
return func(o *SubscribeOptions) {
|
|
|
|
o.BodyOnly = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeBatchSize specifies max batch size
|
|
|
|
func SubscribeBatchSize(n int) SubscribeOption {
|
|
|
|
return func(o *SubscribeOptions) {
|
|
|
|
o.BatchSize = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeBatchWait specifies max batch wait time
|
|
|
|
func SubscribeBatchWait(td time.Duration) SubscribeOption {
|
|
|
|
return func(o *SubscribeOptions) {
|
|
|
|
o.BatchWait = td
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeOption func
|
|
|
|
type SubscribeOption func(*SubscribeOptions)
|
|
|
|
|
|
|
|
// NewSubscribeOptions creates new SubscribeOptions
|
|
|
|
func NewSubscribeOptions(opts ...SubscribeOption) SubscribeOptions {
|
|
|
|
options := SubscribeOptions{
|
|
|
|
AutoAck: true,
|
|
|
|
Context: context.Background(),
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|