2015-12-18 16:48:32 +00:00
|
|
|
package chanotify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Notifier can effectively notify you about receiving from particular channels.
|
|
|
|
// It operates with pairs <-chan struct{} <-> string which is notification
|
|
|
|
// channel and its identificator respectively.
|
|
|
|
// Notification channel is <-chan struc{}, each send to which is spawn
|
|
|
|
// notification from Notifier, close doesn't spawn anything and removes channel
|
|
|
|
// from Notifier.
|
|
|
|
type Notifier struct {
|
2016-01-20 21:42:29 +00:00
|
|
|
c chan string
|
|
|
|
|
|
|
|
m sync.Mutex // guards doneCh
|
|
|
|
doneCh map[string]chan struct{}
|
2015-12-18 16:48:32 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 21:42:29 +00:00
|
|
|
// New returns a new *Notifier.
|
2015-12-18 16:48:32 +00:00
|
|
|
func New() *Notifier {
|
|
|
|
s := &Notifier{
|
2016-01-20 21:42:29 +00:00
|
|
|
c: make(chan string),
|
|
|
|
doneCh: make(map[string]chan struct{}),
|
2015-12-18 16:48:32 +00:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chan returns channel on which client listen for notifications.
|
2016-01-20 21:42:29 +00:00
|
|
|
// IDs of notifications is sent to the returned channel.
|
2015-12-18 16:48:32 +00:00
|
|
|
func (s *Notifier) Chan() <-chan string {
|
|
|
|
return s.c
|
|
|
|
}
|
|
|
|
|
2016-01-20 21:42:29 +00:00
|
|
|
func (s *Notifier) killWorker(id string, done chan struct{}) {
|
2015-12-18 16:48:32 +00:00
|
|
|
s.m.Lock()
|
2016-01-20 21:42:29 +00:00
|
|
|
delete(s.doneCh, id)
|
2015-12-18 16:48:32 +00:00
|
|
|
s.m.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-01-20 21:42:29 +00:00
|
|
|
// Add adds new notification channel to Notifier.
|
|
|
|
func (s *Notifier) Add(ch <-chan struct{}, id string) {
|
|
|
|
done := make(chan struct{})
|
|
|
|
s.m.Lock()
|
|
|
|
s.doneCh[id] = done
|
|
|
|
s.m.Unlock()
|
2015-12-18 16:48:32 +00:00
|
|
|
|
2016-01-20 21:42:29 +00:00
|
|
|
go func(ch <-chan struct{}, id string, done chan struct{}) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case _, ok := <-ch:
|
|
|
|
if !ok {
|
|
|
|
// If the channel is closed, we don't need the goroutine
|
|
|
|
// or the done channel mechanism running anymore.
|
|
|
|
s.killWorker(id, done)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.c <- id
|
|
|
|
case <-done:
|
|
|
|
// We don't need this goroutine running anymore, return.
|
|
|
|
s.killWorker(id, done)
|
|
|
|
return
|
2015-12-21 23:01:31 +00:00
|
|
|
}
|
2015-12-18 16:48:32 +00:00
|
|
|
}
|
2016-01-20 21:42:29 +00:00
|
|
|
}(ch, id, done)
|
2015-12-18 16:48:32 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 21:42:29 +00:00
|
|
|
// Close closes the notifier and releases its underlying resources.
|
|
|
|
func (s *Notifier) Close() {
|
2015-12-18 16:48:32 +00:00
|
|
|
s.m.Lock()
|
2016-01-20 21:42:29 +00:00
|
|
|
defer s.m.Unlock()
|
|
|
|
for _, done := range s.doneCh {
|
|
|
|
close(done)
|
2015-12-18 16:48:32 +00:00
|
|
|
}
|
2016-01-20 21:42:29 +00:00
|
|
|
close(s.c)
|
|
|
|
// TODO(jbd): Don't allow Add after Close returns.
|
2015-12-18 16:48:32 +00:00
|
|
|
}
|