2015-01-19 18:29:42 -05:00
|
|
|
package pubsub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-02-17 14:28:59 -05:00
|
|
|
var wgPool = sync.Pool{New: func() interface{} { return new(sync.WaitGroup) }}
|
|
|
|
|
2015-01-19 18:29:42 -05:00
|
|
|
// NewPublisher creates a new pub/sub publisher to broadcast messages.
|
|
|
|
// The duration is used as the send timeout as to not block the publisher publishing
|
|
|
|
// messages to other clients if one client is slow or unresponsive.
|
|
|
|
// The buffer is used when creating new channels for subscribers.
|
|
|
|
func NewPublisher(publishTimeout time.Duration, buffer int) *Publisher {
|
|
|
|
return &Publisher{
|
|
|
|
buffer: buffer,
|
|
|
|
timeout: publishTimeout,
|
2015-11-25 21:03:10 -05:00
|
|
|
subscribers: make(map[subscriber]topicFunc),
|
2015-01-19 18:29:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type subscriber chan interface{}
|
2015-11-25 21:03:10 -05:00
|
|
|
type topicFunc func(v interface{}) bool
|
2015-01-19 18:29:42 -05:00
|
|
|
|
2015-07-14 12:10:14 -04:00
|
|
|
// Publisher is basic pub/sub structure. Allows to send events and subscribe
|
|
|
|
// to them. Can be safely used from multiple goroutines.
|
2015-01-19 18:29:42 -05:00
|
|
|
type Publisher struct {
|
|
|
|
m sync.RWMutex
|
|
|
|
buffer int
|
|
|
|
timeout time.Duration
|
2015-11-25 21:03:10 -05:00
|
|
|
subscribers map[subscriber]topicFunc
|
2015-01-19 18:29:42 -05:00
|
|
|
}
|
|
|
|
|
2015-01-20 14:37:50 -05:00
|
|
|
// Len returns the number of subscribers for the publisher
|
|
|
|
func (p *Publisher) Len() int {
|
|
|
|
p.m.RLock()
|
|
|
|
i := len(p.subscribers)
|
|
|
|
p.m.RUnlock()
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2015-01-19 18:29:42 -05:00
|
|
|
// Subscribe adds a new subscriber to the publisher returning the channel.
|
|
|
|
func (p *Publisher) Subscribe() chan interface{} {
|
2015-11-25 21:03:10 -05:00
|
|
|
return p.SubscribeTopic(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeTopic adds a new subscriber that filters messages sent by a topic.
|
|
|
|
func (p *Publisher) SubscribeTopic(topic topicFunc) chan interface{} {
|
2015-01-19 18:29:42 -05:00
|
|
|
ch := make(chan interface{}, p.buffer)
|
|
|
|
p.m.Lock()
|
2015-11-25 21:03:10 -05:00
|
|
|
p.subscribers[ch] = topic
|
2015-01-19 18:29:42 -05:00
|
|
|
p.m.Unlock()
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2017-06-07 13:07:01 -04:00
|
|
|
// SubscribeTopicWithBuffer adds a new subscriber that filters messages sent by a topic.
|
|
|
|
// The returned channel has a buffer of the specified size.
|
|
|
|
func (p *Publisher) SubscribeTopicWithBuffer(topic topicFunc, buffer int) chan interface{} {
|
|
|
|
ch := make(chan interface{}, buffer)
|
|
|
|
p.m.Lock()
|
|
|
|
p.subscribers[ch] = topic
|
|
|
|
p.m.Unlock()
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2015-01-19 18:29:42 -05:00
|
|
|
// Evict removes the specified subscriber from receiving any more messages.
|
|
|
|
func (p *Publisher) Evict(sub chan interface{}) {
|
|
|
|
p.m.Lock()
|
2016-02-23 21:07:38 -05:00
|
|
|
delete(p.subscribers, sub)
|
|
|
|
close(sub)
|
2015-01-19 18:29:42 -05:00
|
|
|
p.m.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish sends the data in v to all subscribers currently registered with the publisher.
|
|
|
|
func (p *Publisher) Publish(v interface{}) {
|
|
|
|
p.m.RLock()
|
2016-01-29 00:08:20 -05:00
|
|
|
if len(p.subscribers) == 0 {
|
|
|
|
p.m.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-17 14:28:59 -05:00
|
|
|
wg := wgPool.Get().(*sync.WaitGroup)
|
2015-11-25 21:03:10 -05:00
|
|
|
for sub, topic := range p.subscribers {
|
|
|
|
wg.Add(1)
|
|
|
|
go p.sendTopic(sub, topic, v, wg)
|
2015-01-19 18:29:42 -05:00
|
|
|
}
|
2015-11-25 21:03:10 -05:00
|
|
|
wg.Wait()
|
2016-02-17 14:28:59 -05:00
|
|
|
wgPool.Put(wg)
|
2015-01-19 18:29:42 -05:00
|
|
|
p.m.RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the channels to all subscribers registered with the publisher.
|
|
|
|
func (p *Publisher) Close() {
|
|
|
|
p.m.Lock()
|
|
|
|
for sub := range p.subscribers {
|
2015-06-12 03:42:34 -04:00
|
|
|
delete(p.subscribers, sub)
|
2015-01-19 18:29:42 -05:00
|
|
|
close(sub)
|
|
|
|
}
|
|
|
|
p.m.Unlock()
|
|
|
|
}
|
2015-11-25 21:03:10 -05:00
|
|
|
|
|
|
|
func (p *Publisher) sendTopic(sub subscriber, topic topicFunc, v interface{}, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
if topic != nil && !topic(v) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// send under a select as to not block if the receiver is unavailable
|
|
|
|
if p.timeout > 0 {
|
|
|
|
select {
|
|
|
|
case sub <- v:
|
|
|
|
case <-time.After(p.timeout):
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case sub <- v:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|