broker: refactor (#396)
* remove subscribe from server * remove publish from client * broker package refactoring Co-authored-by: vtolstov <vtolstov@users.noreply.github.com> Reviewed-on: #396 Co-authored-by: Vasiliy Tolstov <v.tolstov@unistack.org> Co-committed-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
104
broker/noop.go
104
broker/noop.go
@@ -3,24 +3,37 @@ package broker
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"go.unistack.org/micro/v4/codec"
|
||||
"go.unistack.org/micro/v4/metadata"
|
||||
"go.unistack.org/micro/v4/options"
|
||||
)
|
||||
|
||||
type NoopBroker struct {
|
||||
funcPublish FuncPublish
|
||||
funcBatchPublish FuncBatchPublish
|
||||
funcSubscribe FuncSubscribe
|
||||
funcBatchSubscribe FuncBatchSubscribe
|
||||
opts Options
|
||||
funcPublish FuncPublish
|
||||
funcSubscribe FuncSubscribe
|
||||
opts Options
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func (b *NoopBroker) newCodec(ct string) (codec.Codec, error) {
|
||||
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
||||
ct = ct[:idx]
|
||||
}
|
||||
b.RLock()
|
||||
c, ok := b.opts.Codecs[ct]
|
||||
b.RUnlock()
|
||||
if ok {
|
||||
return c, nil
|
||||
}
|
||||
return nil, codec.ErrUnknownContentType
|
||||
}
|
||||
|
||||
func NewBroker(opts ...Option) *NoopBroker {
|
||||
b := &NoopBroker{opts: NewOptions(opts...)}
|
||||
b.funcPublish = b.fnPublish
|
||||
b.funcBatchPublish = b.fnBatchPublish
|
||||
b.funcSubscribe = b.fnSubscribe
|
||||
b.funcBatchSubscribe = b.fnBatchSubscribe
|
||||
|
||||
return b
|
||||
}
|
||||
@@ -55,20 +68,14 @@ func (b *NoopBroker) Init(opts ...Option) error {
|
||||
}
|
||||
|
||||
b.funcPublish = b.fnPublish
|
||||
b.funcBatchPublish = b.fnBatchPublish
|
||||
b.funcSubscribe = b.fnSubscribe
|
||||
b.funcBatchSubscribe = b.fnBatchSubscribe
|
||||
|
||||
b.opts.Hooks.EachPrev(func(hook options.Hook) {
|
||||
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)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -87,43 +94,72 @@ func (b *NoopBroker) Address() string {
|
||||
return strings.Join(b.opts.Addrs, ",")
|
||||
}
|
||||
|
||||
func (b *NoopBroker) fnBatchPublish(_ context.Context, _ []*Message, _ ...PublishOption) error {
|
||||
type noopMessage struct {
|
||||
c codec.Codec
|
||||
ctx context.Context
|
||||
body []byte
|
||||
hdr metadata.Metadata
|
||||
opts PublishOptions
|
||||
}
|
||||
|
||||
func (m *noopMessage) Ack() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *NoopBroker) BatchPublish(ctx context.Context, msgs []*Message, opts ...PublishOption) error {
|
||||
return b.funcBatchPublish(ctx, msgs, opts...)
|
||||
func (m *noopMessage) Body() []byte {
|
||||
return m.body
|
||||
}
|
||||
|
||||
func (b *NoopBroker) fnPublish(_ context.Context, _ string, _ *Message, _ ...PublishOption) error {
|
||||
func (m *noopMessage) Header() metadata.Metadata {
|
||||
return m.hdr
|
||||
}
|
||||
|
||||
func (m *noopMessage) Context() context.Context {
|
||||
return m.ctx
|
||||
}
|
||||
|
||||
func (m *noopMessage) Topic() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *noopMessage) Unmarshal(dst interface{}, opts ...codec.Option) error {
|
||||
return m.c.Unmarshal(m.body, dst)
|
||||
}
|
||||
|
||||
func (b *NoopBroker) NewMessage(ctx context.Context, hdr metadata.Metadata, body interface{}, opts ...PublishOption) (Message, error) {
|
||||
options := NewPublishOptions(opts...)
|
||||
m := &noopMessage{ctx: ctx, hdr: hdr, opts: options}
|
||||
c, err := b.newCodec(m.opts.ContentType)
|
||||
if err == nil {
|
||||
m.body, err = c.Marshal(body)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (b *NoopBroker) fnPublish(_ context.Context, _ string, _ ...Message) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *NoopBroker) Publish(ctx context.Context, topic string, msg *Message, opts ...PublishOption) error {
|
||||
return b.funcPublish(ctx, topic, msg, opts...)
|
||||
func (b *NoopBroker) Publish(ctx context.Context, topic string, msg ...Message) error {
|
||||
return b.funcPublish(ctx, topic, msg...)
|
||||
}
|
||||
|
||||
type NoopSubscriber struct {
|
||||
ctx context.Context
|
||||
topic string
|
||||
handler Handler
|
||||
batchHandler BatchHandler
|
||||
opts SubscribeOptions
|
||||
ctx context.Context
|
||||
topic string
|
||||
handler interface{}
|
||||
opts SubscribeOptions
|
||||
}
|
||||
|
||||
func (b *NoopBroker) fnBatchSubscribe(ctx context.Context, topic string, handler BatchHandler, opts ...SubscribeOption) (Subscriber, error) {
|
||||
return &NoopSubscriber{ctx: ctx, topic: topic, opts: NewSubscribeOptions(opts...), batchHandler: handler}, nil
|
||||
}
|
||||
|
||||
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) {
|
||||
func (b *NoopBroker) fnSubscribe(ctx context.Context, topic string, handler interface{}, opts ...SubscribeOption) (Subscriber, error) {
|
||||
return &NoopSubscriber{ctx: ctx, topic: topic, opts: NewSubscribeOptions(opts...), handler: handler}, nil
|
||||
}
|
||||
|
||||
func (b *NoopBroker) Subscribe(ctx context.Context, topic string, handler Handler, opts ...SubscribeOption) (Subscriber, error) {
|
||||
func (b *NoopBroker) Subscribe(ctx context.Context, topic string, handler interface{}, opts ...SubscribeOption) (Subscriber, error) {
|
||||
return b.funcSubscribe(ctx, topic, handler, opts...)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user