2024-03-04 01:05:40 +03:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
2024-04-22 08:47:50 +03:00
|
|
|
|
|
|
|
"go.unistack.org/micro/v3/options"
|
2024-03-04 01:05:40 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type NoopBroker struct {
|
2024-04-22 08:47:50 +03:00
|
|
|
funcPublish FuncPublish
|
|
|
|
funcBatchPublish FuncBatchPublish
|
|
|
|
funcSubscribe FuncSubscribe
|
|
|
|
funcBatchSubscribe FuncBatchSubscribe
|
|
|
|
opts Options
|
2024-03-04 01:05:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBroker(opts ...Option) *NoopBroker {
|
|
|
|
b := &NoopBroker{opts: NewOptions(opts...)}
|
2024-04-22 08:47:50 +03:00
|
|
|
b.funcPublish = b.fnPublish
|
|
|
|
b.funcBatchPublish = b.fnBatchPublish
|
|
|
|
b.funcSubscribe = b.fnSubscribe
|
|
|
|
b.funcBatchSubscribe = b.fnBatchSubscribe
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2024-12-02 13:20:13 +03:00
|
|
|
func (b *NoopBroker) Health() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *NoopBroker) Live() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *NoopBroker) Ready() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
func (b *NoopBroker) Name() string {
|
|
|
|
return b.opts.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *NoopBroker) String() string {
|
|
|
|
return "noop"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *NoopBroker) Options() Options {
|
|
|
|
return b.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *NoopBroker) Init(opts ...Option) error {
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&b.opts)
|
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
|
|
|
|
b.funcPublish = b.fnPublish
|
|
|
|
b.funcBatchPublish = b.fnBatchPublish
|
|
|
|
b.funcSubscribe = b.fnSubscribe
|
|
|
|
b.funcBatchSubscribe = b.fnBatchSubscribe
|
|
|
|
|
2024-12-17 15:35:04 +03:00
|
|
|
b.opts.Hooks.EachPrev(func(hook options.Hook) {
|
2024-04-22 08:47:50 +03:00
|
|
|
switch h := hook.(type) {
|
|
|
|
case HookPublish:
|
|
|
|
b.funcPublish = h(b.funcPublish)
|
|
|
|
case HookBatchPublish:
|
|
|
|
b.funcBatchPublish = h(b.funcBatchPublish)
|
|
|
|
case HookSubscribe:
|
|
|
|
b.funcSubscribe = h(b.funcSubscribe)
|
|
|
|
case HookBatchSubscribe:
|
|
|
|
b.funcBatchSubscribe = h(b.funcBatchSubscribe)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *NoopBroker) Connect(_ context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *NoopBroker) Disconnect(_ context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *NoopBroker) Address() string {
|
|
|
|
return strings.Join(b.opts.Addrs, ",")
|
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
func (b *NoopBroker) fnBatchPublish(_ context.Context, _ []*Message, _ ...PublishOption) error {
|
2024-03-04 01:05:40 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
func (b *NoopBroker) BatchPublish(ctx context.Context, msgs []*Message, opts ...PublishOption) error {
|
|
|
|
return b.funcBatchPublish(ctx, msgs, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *NoopBroker) fnPublish(_ context.Context, _ string, _ *Message, _ ...PublishOption) error {
|
2024-03-04 01:05:40 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
func (b *NoopBroker) Publish(ctx context.Context, topic string, msg *Message, opts ...PublishOption) error {
|
|
|
|
return b.funcPublish(ctx, topic, msg, opts...)
|
|
|
|
}
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
type NoopSubscriber struct {
|
|
|
|
ctx context.Context
|
|
|
|
topic string
|
|
|
|
handler Handler
|
|
|
|
batchHandler BatchHandler
|
|
|
|
opts SubscribeOptions
|
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
func (b *NoopBroker) fnBatchSubscribe(ctx context.Context, topic string, handler BatchHandler, opts ...SubscribeOption) (Subscriber, error) {
|
2024-03-04 01:05:40 +03:00
|
|
|
return &NoopSubscriber{ctx: ctx, topic: topic, opts: NewSubscribeOptions(opts...), batchHandler: handler}, nil
|
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
func (b *NoopBroker) BatchSubscribe(ctx context.Context, topic string, handler BatchHandler, opts ...SubscribeOption) (Subscriber, error) {
|
|
|
|
return b.funcBatchSubscribe(ctx, topic, handler, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *NoopBroker) fnSubscribe(ctx context.Context, topic string, handler Handler, opts ...SubscribeOption) (Subscriber, error) {
|
2024-03-04 01:05:40 +03:00
|
|
|
return &NoopSubscriber{ctx: ctx, topic: topic, opts: NewSubscribeOptions(opts...), handler: handler}, nil
|
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
func (b *NoopBroker) Subscribe(ctx context.Context, topic string, handler Handler, opts ...SubscribeOption) (Subscriber, error) {
|
|
|
|
return b.funcSubscribe(ctx, topic, handler, opts...)
|
|
|
|
}
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
func (s *NoopSubscriber) Options() SubscribeOptions {
|
|
|
|
return s.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *NoopSubscriber) Topic() string {
|
|
|
|
return s.topic
|
|
|
|
}
|
|
|
|
|
2024-04-22 08:47:50 +03:00
|
|
|
func (s *NoopSubscriber) Unsubscribe(_ context.Context) error {
|
2024-03-04 01:05:40 +03:00
|
|
|
return nil
|
|
|
|
}
|