micro/broker/memory/memory.go

183 lines
2.9 KiB
Go
Raw Normal View History

2019-01-14 15:27:25 +00:00
// Package memory provides a memory broker
package memory
2016-03-17 22:46:20 +00:00
import (
"errors"
"sync"
"github.com/google/uuid"
2016-03-17 22:46:20 +00:00
"github.com/micro/go-micro/broker"
)
2019-01-14 15:27:25 +00:00
type memoryBroker struct {
2016-03-17 22:46:20 +00:00
opts broker.Options
sync.RWMutex
connected bool
2019-01-14 15:27:25 +00:00
Subscribers map[string][]*memorySubscriber
2016-03-17 22:46:20 +00:00
}
2019-07-07 12:44:09 +01:00
type memoryEvent struct {
2016-03-17 22:46:20 +00:00
topic string
message *broker.Message
}
2019-01-14 15:27:25 +00:00
type memorySubscriber struct {
2016-03-17 22:46:20 +00:00
id string
topic string
exit chan bool
handler broker.Handler
opts broker.SubscribeOptions
}
2019-01-14 15:27:25 +00:00
func (m *memoryBroker) Options() broker.Options {
2016-03-17 22:46:20 +00:00
return m.opts
}
2019-01-14 15:27:25 +00:00
func (m *memoryBroker) Address() string {
2016-03-17 22:46:20 +00:00
return ""
}
2019-01-14 15:27:25 +00:00
func (m *memoryBroker) Connect() error {
2016-03-17 22:46:20 +00:00
m.Lock()
defer m.Unlock()
if m.connected {
return nil
}
m.connected = true
return nil
}
2019-01-14 15:27:25 +00:00
func (m *memoryBroker) Disconnect() error {
2016-03-17 22:46:20 +00:00
m.Lock()
defer m.Unlock()
if !m.connected {
return nil
}
m.connected = false
return nil
}
2019-01-14 15:27:25 +00:00
func (m *memoryBroker) Init(opts ...broker.Option) error {
2016-03-17 22:46:20 +00:00
for _, o := range opts {
o(&m.opts)
}
return nil
}
2019-01-14 15:27:25 +00:00
func (m *memoryBroker) Publish(topic string, message *broker.Message, opts ...broker.PublishOption) error {
m.RLock()
2016-03-17 22:46:20 +00:00
if !m.connected {
m.RUnlock()
2016-03-17 22:46:20 +00:00
return errors.New("not connected")
}
subs, ok := m.Subscribers[topic]
m.RUnlock()
2016-03-17 22:46:20 +00:00
if !ok {
return nil
}
2019-07-07 12:44:09 +01:00
p := &memoryEvent{
2016-03-17 22:46:20 +00:00
topic: topic,
message: message,
}
for _, sub := range subs {
if err := sub.handler(p); err != nil {
return err
}
}
return nil
}
2019-01-14 15:27:25 +00:00
func (m *memoryBroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
m.RLock()
2016-03-17 22:46:20 +00:00
if !m.connected {
m.RUnlock()
2016-03-17 22:46:20 +00:00
return nil, errors.New("not connected")
}
m.RUnlock()
2016-03-17 22:46:20 +00:00
var options broker.SubscribeOptions
for _, o := range opts {
o(&options)
}
2019-01-14 15:27:25 +00:00
sub := &memorySubscriber{
2016-03-17 22:46:20 +00:00
exit: make(chan bool, 1),
id: uuid.New().String(),
2016-03-17 22:46:20 +00:00
topic: topic,
handler: handler,
opts: options,
}
m.Lock()
2016-03-17 22:46:20 +00:00
m.Subscribers[topic] = append(m.Subscribers[topic], sub)
m.Unlock()
2016-03-17 22:46:20 +00:00
go func() {
<-sub.exit
m.Lock()
2019-01-14 15:27:25 +00:00
var newSubscribers []*memorySubscriber
2016-03-17 22:46:20 +00:00
for _, sb := range m.Subscribers[topic] {
if sb.id == sub.id {
continue
}
newSubscribers = append(newSubscribers, sb)
}
m.Subscribers[topic] = newSubscribers
m.Unlock()
}()
return sub, nil
}
2019-01-14 15:27:25 +00:00
func (m *memoryBroker) String() string {
return "memory"
2016-03-17 22:46:20 +00:00
}
2019-07-07 12:44:09 +01:00
func (m *memoryEvent) Topic() string {
2016-03-17 22:46:20 +00:00
return m.topic
}
2019-07-07 12:44:09 +01:00
func (m *memoryEvent) Message() *broker.Message {
2016-03-17 22:46:20 +00:00
return m.message
}
2019-07-07 12:44:09 +01:00
func (m *memoryEvent) Ack() error {
2016-03-17 22:46:20 +00:00
return nil
}
2019-01-14 15:27:25 +00:00
func (m *memorySubscriber) Options() broker.SubscribeOptions {
2016-03-17 22:46:20 +00:00
return m.opts
}
2019-01-14 15:27:25 +00:00
func (m *memorySubscriber) Topic() string {
2016-03-17 22:46:20 +00:00
return m.topic
}
2019-01-14 15:27:25 +00:00
func (m *memorySubscriber) Unsubscribe() error {
2016-03-17 22:46:20 +00:00
m.exit <- true
return nil
}
func NewBroker(opts ...broker.Option) broker.Broker {
var options broker.Options
for _, o := range opts {
o(&options)
}
2019-01-14 15:27:25 +00:00
return &memoryBroker{
2016-03-17 22:46:20 +00:00
opts: options,
2019-01-14 15:27:25 +00:00
Subscribers: make(map[string][]*memorySubscriber),
2016-03-17 22:46:20 +00:00
}
}