2015-01-19 23:29:42 +00:00
|
|
|
package pubsub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-02-17 19:28:59 +00:00
|
|
|
var wgPool = sync.Pool{New: func() interface{} { return new(sync.WaitGroup) }}
|
|
|
|
|
2015-01-19 23:29:42 +00: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-26 02:03:10 +00:00
|
|
|
subscribers: make(map[subscriber]topicFunc),
|
2015-01-19 23:29:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type subscriber chan interface{}
|
2015-11-26 02:03:10 +00:00
|
|
|
type topicFunc func(v interface{}) bool
|
2015-01-19 23:29:42 +00:00
|
|
|
|
2015-07-14 16:10:14 +00: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 23:29:42 +00:00
|
|
|
type Publisher struct {
|
|
|
|
m sync.RWMutex
|
|
|
|
buffer int
|
|
|
|
timeout time.Duration
|
2015-11-26 02:03:10 +00:00
|
|
|
subscribers map[subscriber]topicFunc
|
2015-01-19 23:29:42 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 19:37:50 +00: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 23:29:42 +00:00
|
|
|
// Subscribe adds a new subscriber to the publisher returning the channel.
|
|
|
|
func (p *Publisher) Subscribe() chan interface{} {
|
2015-11-26 02:03:10 +00: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 23:29:42 +00:00
|
|
|
ch := make(chan interface{}, p.buffer)
|
|
|
|
p.m.Lock()
|
2015-11-26 02:03:10 +00:00
|
|
|
p.subscribers[ch] = topic
|
2015-01-19 23:29:42 +00:00
|
|
|
p.m.Unlock()
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evict removes the specified subscriber from receiving any more messages.
|
|
|
|
func (p *Publisher) Evict(sub chan interface{}) {
|
|
|
|
p.m.Lock()
|
2016-02-24 02:07:38 +00:00
|
|
|
delete(p.subscribers, sub)
|
|
|
|
close(sub)
|
2015-01-19 23:29:42 +00: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 05:08:20 +00:00
|
|
|
if len(p.subscribers) == 0 {
|
|
|
|
p.m.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-17 19:28:59 +00:00
|
|
|
wg := wgPool.Get().(*sync.WaitGroup)
|
2015-11-26 02:03:10 +00:00
|
|
|
for sub, topic := range p.subscribers {
|
|
|
|
wg.Add(1)
|
|
|
|
go p.sendTopic(sub, topic, v, wg)
|
2015-01-19 23:29:42 +00:00
|
|
|
}
|
2015-11-26 02:03:10 +00:00
|
|
|
wg.Wait()
|
2016-02-17 19:28:59 +00:00
|
|
|
wgPool.Put(wg)
|
2015-01-19 23:29:42 +00: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 07:42:34 +00:00
|
|
|
delete(p.subscribers, sub)
|
2015-01-19 23:29:42 +00:00
|
|
|
close(sub)
|
|
|
|
}
|
|
|
|
p.m.Unlock()
|
|
|
|
}
|
2015-11-26 02:03:10 +00: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:
|
|
|
|
}
|
|
|
|
}
|