2021-04-18 16:39:15 +00:00
|
|
|
package pubsub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2022-02-25 19:08:30 +00:00
|
|
|
"sync/atomic"
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
coretypes "github.com/tendermint/tendermint/rpc/core/types"
|
|
|
|
)
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
type UnsubscribeFunc func()
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
type EventBus interface {
|
|
|
|
AddTopic(name string, src <-chan coretypes.ResultEvent) error
|
|
|
|
RemoveTopic(name string)
|
2022-02-25 19:08:30 +00:00
|
|
|
Subscribe(name string) (<-chan coretypes.ResultEvent, UnsubscribeFunc, error)
|
2021-04-18 16:39:15 +00:00
|
|
|
Topics() []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type memEventBus struct {
|
2022-02-25 19:08:30 +00:00
|
|
|
topics map[string]<-chan coretypes.ResultEvent
|
|
|
|
topicsMux *sync.RWMutex
|
|
|
|
subscribers map[string]map[uint64]chan<- coretypes.ResultEvent
|
|
|
|
subscribersMux *sync.RWMutex
|
|
|
|
currentUniqueID uint64
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewEventBus() EventBus {
|
|
|
|
return &memEventBus{
|
|
|
|
topics: make(map[string]<-chan coretypes.ResultEvent),
|
|
|
|
topicsMux: new(sync.RWMutex),
|
2022-02-25 19:08:30 +00:00
|
|
|
subscribers: make(map[string]map[uint64]chan<- coretypes.ResultEvent),
|
2021-04-18 16:39:15 +00:00
|
|
|
subscribersMux: new(sync.RWMutex),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
func (m *memEventBus) GenUniqueID() uint64 {
|
|
|
|
return atomic.AddUint64(&m.currentUniqueID, 1)
|
|
|
|
}
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
func (m *memEventBus) Topics() (topics []string) {
|
|
|
|
m.topicsMux.RLock()
|
|
|
|
defer m.topicsMux.RUnlock()
|
|
|
|
|
|
|
|
topics = make([]string, 0, len(m.topics))
|
|
|
|
for topicName := range m.topics {
|
|
|
|
topics = append(topics, topicName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return topics
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memEventBus) AddTopic(name string, src <-chan coretypes.ResultEvent) error {
|
|
|
|
m.topicsMux.RLock()
|
|
|
|
_, ok := m.topics[name]
|
|
|
|
m.topicsMux.RUnlock()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return errors.New("topic already registered")
|
|
|
|
}
|
|
|
|
|
|
|
|
m.topicsMux.Lock()
|
|
|
|
m.topics[name] = src
|
|
|
|
m.topicsMux.Unlock()
|
|
|
|
|
|
|
|
go m.publishTopic(name, src)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memEventBus) RemoveTopic(name string) {
|
|
|
|
m.topicsMux.Lock()
|
|
|
|
delete(m.topics, name)
|
|
|
|
m.topicsMux.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
func (m *memEventBus) Subscribe(name string) (<-chan coretypes.ResultEvent, UnsubscribeFunc, error) {
|
2021-04-18 16:39:15 +00:00
|
|
|
m.topicsMux.RLock()
|
|
|
|
_, ok := m.topics[name]
|
|
|
|
m.topicsMux.RUnlock()
|
|
|
|
|
|
|
|
if !ok {
|
2022-02-25 19:08:30 +00:00
|
|
|
return nil, nil, errors.Errorf("topic not found: %s", name)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan coretypes.ResultEvent)
|
|
|
|
m.subscribersMux.Lock()
|
|
|
|
defer m.subscribersMux.Unlock()
|
|
|
|
|
2022-02-25 19:08:30 +00:00
|
|
|
id := m.GenUniqueID()
|
|
|
|
if _, ok := m.subscribers[name]; !ok {
|
|
|
|
m.subscribers[name] = make(map[uint64]chan<- coretypes.ResultEvent)
|
|
|
|
}
|
|
|
|
m.subscribers[name][id] = ch
|
|
|
|
|
|
|
|
unsubscribe := func() {
|
|
|
|
m.subscribersMux.Lock()
|
|
|
|
defer m.subscribersMux.Unlock()
|
|
|
|
delete(m.subscribers[name], id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ch, unsubscribe, nil
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memEventBus) publishTopic(name string, src <-chan coretypes.ResultEvent) {
|
|
|
|
for {
|
2021-06-08 07:07:11 +00:00
|
|
|
msg, ok := <-src
|
|
|
|
if !ok {
|
|
|
|
m.closeAllSubscribers(name)
|
|
|
|
m.topicsMux.Lock()
|
|
|
|
delete(m.topics, name)
|
|
|
|
m.topicsMux.Unlock()
|
|
|
|
return
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
2021-06-08 07:07:11 +00:00
|
|
|
m.publishAllSubscribers(name, msg)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memEventBus) closeAllSubscribers(name string) {
|
|
|
|
m.subscribersMux.Lock()
|
|
|
|
defer m.subscribersMux.Unlock()
|
|
|
|
|
|
|
|
subsribers := m.subscribers[name]
|
|
|
|
delete(m.subscribers, name)
|
|
|
|
|
|
|
|
for _, sub := range subsribers {
|
|
|
|
close(sub)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memEventBus) publishAllSubscribers(name string, msg coretypes.ResultEvent) {
|
|
|
|
m.subscribersMux.RLock()
|
|
|
|
subsribers := m.subscribers[name]
|
|
|
|
m.subscribersMux.RUnlock()
|
|
|
|
|
|
|
|
for _, sub := range subsribers {
|
|
|
|
select {
|
|
|
|
case sub <- msg:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|