2021-02-12 16:33:16 +03:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-29 00:40:58 +03:00
|
|
|
"fmt"
|
2021-02-12 16:33:16 +03:00
|
|
|
"sync"
|
2023-04-27 15:30:55 +03:00
|
|
|
"time"
|
2021-02-12 16:33:16 +03:00
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
"go.unistack.org/micro/v4/codec"
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/logger"
|
2023-07-29 00:40:58 +03:00
|
|
|
"go.unistack.org/micro/v4/metadata"
|
|
|
|
"go.unistack.org/micro/v4/options"
|
|
|
|
"go.unistack.org/micro/v4/semconv"
|
2023-04-11 22:20:37 +03:00
|
|
|
maddr "go.unistack.org/micro/v4/util/addr"
|
|
|
|
"go.unistack.org/micro/v4/util/id"
|
|
|
|
mnet "go.unistack.org/micro/v4/util/net"
|
|
|
|
"go.unistack.org/micro/v4/util/rand"
|
2021-02-12 16:33:16 +03:00
|
|
|
)
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
type MemoryBroker struct {
|
2021-07-23 15:12:20 +03:00
|
|
|
subscribers map[string][]*memorySubscriber
|
|
|
|
addr string
|
|
|
|
opts Options
|
2021-03-06 19:45:13 +03:00
|
|
|
sync.RWMutex
|
|
|
|
connected bool
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (m *MemoryBroker) Options() Options {
|
2021-02-12 16:33:16 +03:00
|
|
|
return m.opts
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (m *MemoryBroker) Address() string {
|
2021-02-12 16:33:16 +03:00
|
|
|
return m.addr
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (m *MemoryBroker) Connect(ctx context.Context) error {
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (m *MemoryBroker) Disconnect(ctx context.Context) error {
|
2021-02-12 16:33:16 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
if m.connected {
|
|
|
|
m.connected = false
|
|
|
|
}
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (m *MemoryBroker) Init(opts ...options.Option) error {
|
|
|
|
var err error
|
2021-02-12 16:33:16 +03:00
|
|
|
for _, o := range opts {
|
2023-07-29 00:40:58 +03:00
|
|
|
if err = o(&m.opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (m *MemoryBroker) Publish(ctx context.Context, message interface{}, opts ...options.Option) error {
|
2021-07-23 15:06:10 +03:00
|
|
|
m.RLock()
|
|
|
|
if !m.connected {
|
|
|
|
m.RUnlock()
|
|
|
|
return 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:
|
|
|
|
options := NewPublishOptions(opts...)
|
2023-07-29 00:40:58 +03:00
|
|
|
var msgs []Message
|
2023-05-09 20:04:15 +03:00
|
|
|
switch v := message.(type) {
|
2023-07-29 00:40:58 +03:00
|
|
|
case []Message:
|
2023-05-09 20:04:15 +03:00
|
|
|
msgs = v
|
2023-07-29 00:40:58 +03:00
|
|
|
case Message:
|
|
|
|
msgs = append(msgs, v)
|
2023-05-09 20:04:15 +03:00
|
|
|
default:
|
|
|
|
return ErrInvalidMessage
|
|
|
|
}
|
|
|
|
msgTopicMap := make(map[string][]*memoryMessage)
|
|
|
|
for _, msg := range msgs {
|
|
|
|
p := &memoryMessage{opts: options}
|
2023-07-29 00:40:58 +03:00
|
|
|
p.topic, _ = msg.Header().Get(metadata.HeaderTopic)
|
|
|
|
if v, ok := msg.Body().(*codec.Frame); ok {
|
|
|
|
p.body = msg.Body()
|
|
|
|
} else if len(m.opts.Codecs) == 0 {
|
|
|
|
p.body = msg.Body()
|
|
|
|
} else {
|
|
|
|
cf, ok := m.opts.Codecs[options.ContentType]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%s: %s", codec.ErrUnknownContentType, options.ContentType)
|
2021-09-30 20:32:59 +03:00
|
|
|
}
|
2023-07-29 00:40:58 +03:00
|
|
|
p.body, err = cf.Marshal(v)
|
|
|
|
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
|
|
|
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 {
|
2023-04-27 15:30:55 +03:00
|
|
|
ts := time.Now()
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
m.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", t).Add(len(ms))
|
|
|
|
m.opts.Meter.Counter(semconv.SubscribeMessageInflight, "endpoint", t).Add(len(ms))
|
2023-04-27 15:30:55 +03:00
|
|
|
|
2021-09-30 20:32:59 +03:00
|
|
|
m.RLock()
|
|
|
|
subs, ok := m.subscribers[t]
|
|
|
|
m.RUnlock()
|
|
|
|
if !ok {
|
2023-07-29 00:40:58 +03:00
|
|
|
m.opts.Meter.Counter(semconv.PublishMessageTotal, "endpoint", t, "status", "failure").Add(len(ms))
|
|
|
|
m.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", t).Add(-len(ms))
|
|
|
|
m.opts.Meter.Counter(semconv.SubscribeMessageInflight, "endpoint", t).Add(-len(ms))
|
2021-09-30 20:32:59 +03:00
|
|
|
continue
|
2021-09-29 13:10:11 +03:00
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
m.opts.Meter.Counter(semconv.PublishMessageTotal, "endpoint", t, "status", "success").Add(len(ms))
|
2021-09-30 20:32:59 +03:00
|
|
|
for _, sub := range subs {
|
|
|
|
if sub.opts.ErrorHandler != nil {
|
|
|
|
eh = sub.opts.ErrorHandler
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
switch mh := sub.handler.(type) {
|
|
|
|
case MessagesHandler:
|
|
|
|
mhs := make([]Message, 0, len(ms))
|
|
|
|
for _, m := range ms {
|
|
|
|
mhs = append(mhs, m)
|
|
|
|
}
|
|
|
|
if err = mh(mhs); err != nil {
|
|
|
|
m.opts.Meter.Counter(semconv.SubscribeMessageTotal, "endpoint", t, "status", "failure").Add(len(ms))
|
2023-05-09 20:04:15 +03:00
|
|
|
if eh != nil {
|
2023-07-29 00:40:58 +03:00
|
|
|
switch meh := eh.(type) {
|
|
|
|
case MessagesHandler:
|
|
|
|
_ = meh(mhs)
|
|
|
|
case MessageHandler:
|
|
|
|
for _, me := range mhs {
|
|
|
|
_ = meh(me)
|
|
|
|
}
|
|
|
|
}
|
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())
|
|
|
|
}
|
2023-07-29 00:40:58 +03:00
|
|
|
}
|
|
|
|
case MessageHandler:
|
|
|
|
for _, p := range ms {
|
|
|
|
if err = mh(p); err != nil {
|
|
|
|
m.opts.Meter.Counter(semconv.SubscribeMessageTotal, "endpoint", t, "status", "failure").Inc()
|
|
|
|
if eh != nil {
|
|
|
|
switch meh := eh.(type) {
|
|
|
|
case MessageHandler:
|
|
|
|
_ = meh(p)
|
|
|
|
case MessagesHandler:
|
|
|
|
_ = meh([]Message{p})
|
|
|
|
}
|
|
|
|
} else if m.opts.Logger.V(logger.ErrorLevel) {
|
|
|
|
m.opts.Logger.Error(m.opts.Context, err.Error())
|
2023-04-27 15:30:55 +03:00
|
|
|
}
|
|
|
|
} else {
|
2023-07-29 00:40:58 +03:00
|
|
|
if sub.opts.AutoAck {
|
|
|
|
if err = p.Ack(); err != nil {
|
2023-10-14 18:59:27 +03:00
|
|
|
m.opts.Logger.Error(m.opts.Context, "ack failed: "+err.Error())
|
2023-07-29 00:40:58 +03:00
|
|
|
m.opts.Meter.Counter(semconv.SubscribeMessageTotal, "endpoint", t, "status", "failure").Inc()
|
|
|
|
} else {
|
|
|
|
m.opts.Meter.Counter(semconv.SubscribeMessageTotal, "endpoint", t, "status", "success").Inc()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m.opts.Meter.Counter(semconv.SubscribeMessageTotal, "endpoint", t, "status", "success").Inc()
|
|
|
|
}
|
2021-07-23 15:12:20 +03:00
|
|
|
}
|
2023-07-29 00:40:58 +03:00
|
|
|
m.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", t).Add(-1)
|
|
|
|
m.opts.Meter.Counter(semconv.SubscribeMessageInflight, "endpoint", t).Add(-1)
|
2021-07-23 15:12:20 +03:00
|
|
|
}
|
2021-07-22 22:53:44 +03:00
|
|
|
}
|
|
|
|
}
|
2023-07-29 00:40:58 +03:00
|
|
|
|
2023-04-27 15:30:55 +03:00
|
|
|
te := time.Since(ts)
|
2023-07-29 00:40:58 +03:00
|
|
|
m.opts.Meter.Summary(semconv.PublishMessageLatencyMicroseconds, "endpoint", t).Update(te.Seconds())
|
|
|
|
m.opts.Meter.Histogram(semconv.PublishMessageDurationSeconds, "endpoint", t).Update(te.Seconds())
|
|
|
|
m.opts.Meter.Summary(semconv.SubscribeMessageLatencyMicroseconds, "endpoint", t).Update(te.Seconds())
|
|
|
|
m.opts.Meter.Histogram(semconv.SubscribeMessageDurationSeconds, "endpoint", t).Update(te.Seconds())
|
2021-07-22 22:53:44 +03:00
|
|
|
}
|
2023-04-27 15:30:55 +03:00
|
|
|
|
2021-07-22 22:53:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (m *MemoryBroker) Subscribe(ctx context.Context, topic string, handler interface{}, opts ...options.Option) (Subscriber, error) {
|
2021-02-12 16:33:16 +03:00
|
|
|
m.RLock()
|
|
|
|
if !m.connected {
|
|
|
|
m.RUnlock()
|
2021-07-22 22:53:44 +03:00
|
|
|
return nil, 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
|
|
|
|
}
|
|
|
|
|
2021-07-23 15:06:10 +03:00
|
|
|
options := NewSubscribeOptions(opts...)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (m *MemoryBroker) String() string {
|
2021-02-12 16:33:16 +03:00
|
|
|
return "memory"
|
|
|
|
}
|
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (m *MemoryBroker) Name() string {
|
2021-02-12 16:33:16 +03:00
|
|
|
return m.opts.Name
|
|
|
|
}
|
|
|
|
|
2023-05-09 20:04:15 +03:00
|
|
|
type memoryMessage struct {
|
2023-07-29 00:40:58 +03:00
|
|
|
err error
|
|
|
|
body interface{}
|
|
|
|
topic string
|
|
|
|
header metadata.Metadata
|
|
|
|
opts PublishOptions
|
|
|
|
ctx context.Context
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
2023-05-09 20:04:15 +03:00
|
|
|
func (m *memoryMessage) Topic() string {
|
|
|
|
return m.topic
|
|
|
|
}
|
2021-02-12 16:33:16 +03:00
|
|
|
|
2023-07-29 00:40:58 +03:00
|
|
|
func (m *memoryMessage) Header() metadata.Metadata {
|
|
|
|
return m.header
|
|
|
|
}
|
|
|
|
|
2023-05-09 20:04:15 +03:00
|
|
|
func (m *memoryMessage) Body() interface{} {
|
|
|
|
return m.body
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
2023-05-09 20:04:15 +03:00
|
|
|
func (m *memoryMessage) Ack() error {
|
2021-02-12 16:33:16 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-09 20:04:15 +03:00
|
|
|
func (m *memoryMessage) Error() error {
|
2021-02-12 16:33:16 +03:00
|
|
|
return m.err
|
|
|
|
}
|
|
|
|
|
2023-05-09 20:04:15 +03:00
|
|
|
func (m *memoryMessage) Context() context.Context {
|
|
|
|
return m.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
type memorySubscriber struct {
|
|
|
|
ctx context.Context
|
|
|
|
exit chan bool
|
|
|
|
handler interface{}
|
|
|
|
id string
|
|
|
|
topic string
|
|
|
|
opts SubscribeOptions
|
2021-07-22 22:53:44 +03:00
|
|
|
}
|
|
|
|
|
2021-02-12 16:33:16 +03:00
|
|
|
func (m *memorySubscriber) Options() SubscribeOptions {
|
|
|
|
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
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewBroker(opts ...options.Option) *MemoryBroker {
|
|
|
|
return &MemoryBroker{
|
2021-07-23 15:12:20 +03:00
|
|
|
opts: NewOptions(opts...),
|
|
|
|
subscribers: make(map[string][]*memorySubscriber),
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
}
|