2021-02-12 16:33:16 +03:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
"go.unistack.org/micro/v3/broker"
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"go.unistack.org/micro/v3/metadata"
|
2024-04-22 08:47:50 +03:00
|
|
|
"go.unistack.org/micro/v3/options"
|
2021-10-02 19:55:07 +03:00
|
|
|
maddr "go.unistack.org/micro/v3/util/addr"
|
|
|
|
"go.unistack.org/micro/v3/util/id"
|
|
|
|
mnet "go.unistack.org/micro/v3/util/net"
|
|
|
|
"go.unistack.org/micro/v3/util/rand"
|
2021-02-12 16:33:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type memoryBroker struct {
|
2024-04-22 08:47:50 +03:00
|
|
|
funcPublish broker.FuncPublish
|
|
|
|
funcBatchPublish broker.FuncBatchPublish
|
|
|
|
funcSubscribe broker.FuncSubscribe
|
|
|
|
funcBatchSubscribe broker.FuncBatchSubscribe
|
|
|
|
subscribers map[string][]*memorySubscriber
|
|
|
|
addr string
|
|
|
|
opts broker.Options
|
2021-03-06 19:45:13 +03:00
|
|
|
sync.RWMutex
|
|
|
|
connected bool
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type memoryEvent struct {
|
|
|
|
err error
|
|
|
|
message interface{}
|
2021-03-06 19:45:13 +03:00
|
|
|
topic string
|
2024-03-04 01:05:40 +03:00
|
|
|
opts broker.Options
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type memorySubscriber struct {
|
2021-07-23 15:06:10 +03:00
|
|
|
ctx context.Context
|
|
|
|
exit chan bool
|
2024-03-04 01:05:40 +03:00
|
|
|
handler broker.Handler
|
|
|
|
batchhandler broker.BatchHandler
|
2021-07-23 15:06:10 +03:00
|
|
|
id string
|
|
|
|
topic string
|
2024-03-04 01:05:40 +03:00
|
|
|
opts broker.SubscribeOptions
|
2021-07-22 22:53:44 +03:00
|
|
|
}
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
func (m *memoryBroker) Options() broker.Options {
|
2021-02-12 16:33:16 +03:00
|
|
|
return m.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryBroker) Address() string {
|
|
|
|
return m.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryBroker) Connect(ctx context.Context) error {
|
2024-03-04 01:05:40 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2021-02-12 16:33:16 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
if m.connected {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// use 127.0.0.1 to avoid scan of all network interfaces
|
|
|
|
addr, err := maddr.Extract("127.0.0.1")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-17 22:58:55 +03:00
|
|
|
var rng rand.Rand
|
|
|
|
i := rng.Intn(20000)
|
2021-02-12 16:33:16 +03:00
|
|
|
// set addr with port
|
|
|
|
addr = mnet.HostPort(addr, 10000+i)
|
|
|
|
|
|
|
|
m.addr = addr
|
|
|
|
m.connected = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryBroker) Disconnect(ctx context.Context) error {
|
2024-03-04 01:05:40 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2021-02-12 16:33:16 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
if !m.connected {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m.connected = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
func (m *memoryBroker) Init(opts ...broker.Option) error {
|
2021-02-12 16:33:16 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&m.opts)
|
|
|
|
}
|
2024-04-22 08:47:50 +03:00
|
|
|
|
|
|
|
m.funcPublish = m.fnPublish
|
|
|
|
m.funcBatchPublish = m.fnBatchPublish
|
|
|
|
m.funcSubscribe = m.fnSubscribe
|
|
|
|
m.funcBatchSubscribe = m.fnBatchSubscribe
|
|
|
|
|
2024-12-18 20:31:07 +03:00
|
|
|
m.opts.Hooks.EachPrev(func(hook options.Hook) {
|
2024-04-22 08:47:50 +03:00
|
|
|
switch h := hook.(type) {
|
|
|
|
case broker.HookPublish:
|
|
|
|
m.funcPublish = h(m.funcPublish)
|
|
|
|
case broker.HookBatchPublish:
|
|
|
|
m.funcBatchPublish = h(m.funcBatchPublish)
|
|
|
|
case broker.HookSubscribe:
|
|
|
|
m.funcSubscribe = h(m.funcSubscribe)
|
|
|
|
case broker.HookBatchSubscribe:
|
|
|
|
m.funcBatchSubscribe = h(m.funcBatchSubscribe)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-02-12 16:33:16 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
func (m *memoryBroker) Publish(ctx context.Context, topic string, msg *broker.Message, opts ...broker.PublishOption) error {
|
2024-04-22 08:47:50 +03:00
|
|
|
return m.funcPublish(ctx, topic, msg, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryBroker) fnPublish(ctx context.Context, topic string, msg *broker.Message, opts ...broker.PublishOption) error {
|
2021-09-30 20:32:59 +03:00
|
|
|
msg.Header.Set(metadata.HeaderTopic, topic)
|
2024-03-04 01:05:40 +03:00
|
|
|
return m.publish(ctx, []*broker.Message{msg}, opts...)
|
2021-07-23 15:06:10 +03:00
|
|
|
}
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
func (m *memoryBroker) BatchPublish(ctx context.Context, msgs []*broker.Message, opts ...broker.PublishOption) error {
|
2024-04-22 08:47:50 +03:00
|
|
|
return m.funcBatchPublish(ctx, msgs, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryBroker) fnBatchPublish(ctx context.Context, msgs []*broker.Message, opts ...broker.PublishOption) error {
|
2021-09-30 20:32:59 +03:00
|
|
|
return m.publish(ctx, msgs, opts...)
|
2021-07-23 15:06:10 +03:00
|
|
|
}
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
func (m *memoryBroker) publish(ctx context.Context, msgs []*broker.Message, opts ...broker.PublishOption) error {
|
2021-07-23 15:06:10 +03:00
|
|
|
m.RLock()
|
|
|
|
if !m.connected {
|
|
|
|
m.RUnlock()
|
2024-03-04 01:05:40 +03:00
|
|
|
return broker.ErrNotConnected
|
2021-07-22 22:53:44 +03:00
|
|
|
}
|
2021-07-23 15:06:10 +03:00
|
|
|
m.RUnlock()
|
2021-07-22 22:53:44 +03:00
|
|
|
|
2021-07-23 15:12:20 +03:00
|
|
|
var err error
|
2021-07-22 22:53:44 +03:00
|
|
|
|
2021-09-30 20:32:59 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
2024-03-04 01:05:40 +03:00
|
|
|
options := broker.NewPublishOptions(opts...)
|
2021-09-30 20:32:59 +03:00
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
msgTopicMap := make(map[string]broker.Events)
|
2021-09-30 20:32:59 +03:00
|
|
|
for _, v := range msgs {
|
|
|
|
p := &memoryEvent{opts: m.opts}
|
|
|
|
|
|
|
|
if m.opts.Codec == nil || options.BodyOnly {
|
|
|
|
p.topic, _ = v.Header.Get(metadata.HeaderTopic)
|
|
|
|
p.message = v.Body
|
|
|
|
} else {
|
|
|
|
p.topic, _ = v.Header.Get(metadata.HeaderTopic)
|
2021-09-30 21:00:02 +03:00
|
|
|
p.message, err = m.opts.Codec.Marshal(v)
|
2021-09-30 20:32:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msgTopicMap[p.topic] = append(msgTopicMap[p.topic], p)
|
2021-07-22 22:53:44 +03:00
|
|
|
}
|
|
|
|
|
2021-09-30 20:32:59 +03:00
|
|
|
beh := m.opts.BatchErrorHandler
|
|
|
|
eh := m.opts.ErrorHandler
|
2021-07-23 15:12:20 +03:00
|
|
|
|
2021-09-30 20:32:59 +03:00
|
|
|
for t, ms := range msgTopicMap {
|
|
|
|
m.RLock()
|
|
|
|
subs, ok := m.subscribers[t]
|
|
|
|
m.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
continue
|
2021-09-29 13:10:11 +03:00
|
|
|
}
|
|
|
|
|
2021-09-30 20:32:59 +03:00
|
|
|
for _, sub := range subs {
|
|
|
|
if sub.opts.BatchErrorHandler != nil {
|
|
|
|
beh = sub.opts.BatchErrorHandler
|
2021-07-22 22:53:44 +03:00
|
|
|
}
|
2021-09-30 20:32:59 +03:00
|
|
|
if sub.opts.ErrorHandler != nil {
|
|
|
|
eh = sub.opts.ErrorHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
// batch processing
|
|
|
|
case sub.batchhandler != nil:
|
|
|
|
if err = sub.batchhandler(ms); err != nil {
|
|
|
|
ms.SetError(err)
|
|
|
|
if beh != nil {
|
|
|
|
_ = beh(ms)
|
2021-07-23 15:12:20 +03:00
|
|
|
} else if m.opts.Logger.V(logger.ErrorLevel) {
|
|
|
|
m.opts.Logger.Error(m.opts.Context, err.Error())
|
|
|
|
}
|
|
|
|
} else if sub.opts.AutoAck {
|
2021-09-30 20:32:59 +03:00
|
|
|
if err = ms.Ack(); err != nil {
|
2024-10-12 12:37:43 +03:00
|
|
|
m.opts.Logger.Error(m.opts.Context, "broker ack error", err)
|
2021-07-23 15:12:20 +03:00
|
|
|
}
|
|
|
|
}
|
2021-09-30 20:32:59 +03:00
|
|
|
// single processing
|
|
|
|
case sub.handler != nil:
|
|
|
|
for _, p := range ms {
|
|
|
|
if err = sub.handler(p); err != nil {
|
|
|
|
p.SetError(err)
|
|
|
|
if eh != nil {
|
|
|
|
_ = eh(p)
|
|
|
|
} else if m.opts.Logger.V(logger.ErrorLevel) {
|
2024-10-12 12:37:43 +03:00
|
|
|
m.opts.Logger.Error(m.opts.Context, "broker handler error", err)
|
2021-09-30 20:32:59 +03:00
|
|
|
}
|
|
|
|
} else if sub.opts.AutoAck {
|
|
|
|
if err = p.Ack(); err != nil {
|
2024-10-12 12:37:43 +03:00
|
|
|
m.opts.Logger.Error(m.opts.Context, "broker ack error", err)
|
2021-09-30 20:32:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 22:53:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
func (m *memoryBroker) BatchSubscribe(ctx context.Context, topic string, handler broker.BatchHandler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
2024-04-22 08:47:50 +03:00
|
|
|
return m.funcBatchSubscribe(ctx, topic, handler, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryBroker) fnBatchSubscribe(ctx context.Context, topic string, handler broker.BatchHandler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
2021-07-22 22:53:44 +03:00
|
|
|
m.RLock()
|
|
|
|
if !m.connected {
|
|
|
|
m.RUnlock()
|
2024-03-04 01:05:40 +03:00
|
|
|
return nil, broker.ErrNotConnected
|
2021-07-22 22:53:44 +03:00
|
|
|
}
|
|
|
|
m.RUnlock()
|
|
|
|
|
2021-08-20 22:40:48 +03:00
|
|
|
sid, err := id.New()
|
2021-07-22 22:53:44 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
options := broker.NewSubscribeOptions(opts...)
|
2021-07-23 15:06:10 +03:00
|
|
|
|
|
|
|
sub := &memorySubscriber{
|
|
|
|
exit: make(chan bool, 1),
|
2021-08-20 22:40:48 +03:00
|
|
|
id: sid,
|
2021-07-23 15:06:10 +03:00
|
|
|
topic: topic,
|
|
|
|
batchhandler: handler,
|
|
|
|
opts: options,
|
|
|
|
ctx: ctx,
|
2021-07-22 22:53:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
m.Lock()
|
2021-07-23 15:12:20 +03:00
|
|
|
m.subscribers[topic] = append(m.subscribers[topic], sub)
|
2021-07-22 22:53:44 +03:00
|
|
|
m.Unlock()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-sub.exit
|
|
|
|
m.Lock()
|
2021-07-23 15:12:20 +03:00
|
|
|
newSubscribers := make([]*memorySubscriber, 0, len(m.subscribers)-1)
|
|
|
|
for _, sb := range m.subscribers[topic] {
|
2021-07-22 22:53:44 +03:00
|
|
|
if sb.id == sub.id {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newSubscribers = append(newSubscribers, sb)
|
|
|
|
}
|
2021-07-23 15:12:20 +03:00
|
|
|
m.subscribers[topic] = newSubscribers
|
2021-07-22 22:53:44 +03:00
|
|
|
m.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return sub, nil
|
|
|
|
}
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
func (m *memoryBroker) Subscribe(ctx context.Context, topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
2024-04-22 08:47:50 +03:00
|
|
|
return m.funcSubscribe(ctx, topic, handler, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryBroker) fnSubscribe(ctx context.Context, topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
2021-02-12 16:33:16 +03:00
|
|
|
m.RLock()
|
|
|
|
if !m.connected {
|
|
|
|
m.RUnlock()
|
2024-03-04 01:05:40 +03:00
|
|
|
return nil, broker.ErrNotConnected
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
m.RUnlock()
|
|
|
|
|
2021-08-20 22:40:48 +03:00
|
|
|
sid, err := id.New()
|
2021-02-12 16:33:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
options := broker.NewSubscribeOptions(opts...)
|
2021-07-23 15:06:10 +03:00
|
|
|
|
2021-02-12 16:33:16 +03:00
|
|
|
sub := &memorySubscriber{
|
|
|
|
exit: make(chan bool, 1),
|
2021-08-20 22:40:48 +03:00
|
|
|
id: sid,
|
2021-02-12 16:33:16 +03:00
|
|
|
topic: topic,
|
|
|
|
handler: handler,
|
|
|
|
opts: options,
|
|
|
|
ctx: ctx,
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Lock()
|
2021-07-22 22:53:44 +03:00
|
|
|
m.subscribers[topic] = append(m.subscribers[topic], sub)
|
2021-02-12 16:33:16 +03:00
|
|
|
m.Unlock()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-sub.exit
|
|
|
|
m.Lock()
|
2021-07-23 15:06:10 +03:00
|
|
|
newSubscribers := make([]*memorySubscriber, 0, len(m.subscribers)-1)
|
2021-07-22 22:53:44 +03:00
|
|
|
for _, sb := range m.subscribers[topic] {
|
2021-02-12 16:33:16 +03:00
|
|
|
if sb.id == sub.id {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newSubscribers = append(newSubscribers, sb)
|
|
|
|
}
|
2021-07-22 22:53:44 +03:00
|
|
|
m.subscribers[topic] = newSubscribers
|
2021-02-12 16:33:16 +03:00
|
|
|
m.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return sub, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryBroker) String() string {
|
|
|
|
return "memory"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryBroker) Name() string {
|
|
|
|
return m.opts.Name
|
|
|
|
}
|
|
|
|
|
2024-12-02 13:20:13 +03:00
|
|
|
func (m *memoryBroker) Live() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryBroker) Ready() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryBroker) Health() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-12 16:33:16 +03:00
|
|
|
func (m *memoryEvent) Topic() string {
|
|
|
|
return m.topic
|
|
|
|
}
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
func (m *memoryEvent) Message() *broker.Message {
|
2021-02-12 16:33:16 +03:00
|
|
|
switch v := m.message.(type) {
|
2024-03-04 01:05:40 +03:00
|
|
|
case *broker.Message:
|
2021-02-12 16:33:16 +03:00
|
|
|
return v
|
|
|
|
case []byte:
|
2024-03-04 01:05:40 +03:00
|
|
|
msg := &broker.Message{}
|
2021-02-12 16:33:16 +03:00
|
|
|
if err := m.opts.Codec.Unmarshal(v, msg); err != nil {
|
|
|
|
if m.opts.Logger.V(logger.ErrorLevel) {
|
|
|
|
m.opts.Logger.Error(m.opts.Context, "[memory]: failed to unmarshal: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryEvent) Ack() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memoryEvent) Error() error {
|
|
|
|
return m.err
|
|
|
|
}
|
|
|
|
|
2021-07-22 22:53:44 +03:00
|
|
|
func (m *memoryEvent) SetError(err error) {
|
|
|
|
m.err = err
|
|
|
|
}
|
|
|
|
|
2024-07-01 09:47:51 +03:00
|
|
|
func (m *memoryEvent) Context() context.Context {
|
|
|
|
return m.opts.Context
|
|
|
|
}
|
|
|
|
|
2024-03-04 01:05:40 +03:00
|
|
|
func (m *memorySubscriber) Options() broker.SubscribeOptions {
|
2021-02-12 16:33:16 +03:00
|
|
|
return m.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memorySubscriber) Topic() string {
|
|
|
|
return m.topic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memorySubscriber) Unsubscribe(ctx context.Context) error {
|
|
|
|
m.exit <- true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-13 15:35:56 +03:00
|
|
|
// NewBroker return new memory broker
|
2024-03-04 01:05:40 +03:00
|
|
|
func NewBroker(opts ...broker.Option) broker.Broker {
|
2021-02-12 16:33:16 +03:00
|
|
|
return &memoryBroker{
|
2024-03-04 01:05:40 +03:00
|
|
|
opts: broker.NewOptions(opts...),
|
2021-07-23 15:12:20 +03:00
|
|
|
subscribers: make(map[string][]*memorySubscriber),
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
}
|