1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-07-14 05:00:13 -04:00

simplify classic dns server

This commit is contained in:
Darien Raymond
2018-06-26 15:04:47 +02:00
parent 4f33540b19
commit 9cfb2bfd51
5 changed files with 282 additions and 284 deletions

View File

@@ -1,26 +1,34 @@
package signal
import "sync"
// Notifier is a utility for notifying changes. The change producer may notify changes multiple time, and the consumer may get notified asynchronously.
type Notifier struct {
c chan struct{}
sync.Mutex
waiters []chan struct{}
}
// NewNotifier creates a new Notifier.
func NewNotifier() *Notifier {
return &Notifier{
c: make(chan struct{}, 1),
}
return &Notifier{}
}
// Signal signals a change, usually by producer. This method never blocks.
func (n *Notifier) Signal() {
select {
case n.c <- struct{}{}:
default:
n.Lock()
for _, w := range n.waiters {
close(w)
}
n.waiters = make([]chan struct{}, 0, 8)
n.Unlock()
}
// Wait returns a channel for waiting for changes. The returned channel never gets closed.
func (n *Notifier) Wait() <-chan struct{} {
return n.c
n.Lock()
defer n.Unlock()
w := make(chan struct{})
n.waiters = append(n.waiters, w)
return w
}