2014-02-04 20:21:30 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NotifyGroup is used to allow a simple notification mechanism.
|
|
|
|
// Channels can be marked as waiting, and when notify is invoked,
|
|
|
|
// all the waiting channels get a message and are cleared from the
|
|
|
|
// notify list.
|
|
|
|
type NotifyGroup struct {
|
|
|
|
l sync.Mutex
|
2015-05-15 00:30:30 +00:00
|
|
|
notify map[chan struct{}]struct{}
|
2014-02-04 20:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify will do a non-blocking send to all waiting channels, and
|
|
|
|
// clear the notify list
|
|
|
|
func (n *NotifyGroup) Notify() {
|
|
|
|
n.l.Lock()
|
|
|
|
defer n.l.Unlock()
|
2015-05-15 00:30:30 +00:00
|
|
|
for ch, _ := range n.notify {
|
2014-02-04 20:21:30 +00:00
|
|
|
select {
|
|
|
|
case ch <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 00:30:30 +00:00
|
|
|
n.notify = nil
|
2014-02-04 20:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait adds a channel to the notify group
|
|
|
|
func (n *NotifyGroup) Wait(ch chan struct{}) {
|
|
|
|
n.l.Lock()
|
|
|
|
defer n.l.Unlock()
|
2015-05-15 00:30:30 +00:00
|
|
|
if n.notify == nil {
|
|
|
|
n.notify = make(map[chan struct{}]struct{})
|
|
|
|
}
|
|
|
|
n.notify[ch] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear removes a channel from the notify group
|
|
|
|
func (n *NotifyGroup) Clear(ch chan struct{}) {
|
|
|
|
n.l.Lock()
|
|
|
|
defer n.l.Unlock()
|
|
|
|
if n.notify == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delete(n.notify, ch)
|
2014-02-04 20:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WaitCh allocates a channel that is subscribed to notifications
|
|
|
|
func (n *NotifyGroup) WaitCh() chan struct{} {
|
|
|
|
ch := make(chan struct{}, 1)
|
|
|
|
n.Wait(ch)
|
|
|
|
return ch
|
|
|
|
}
|