v2ray-core/common/signal/notifier.go

27 lines
640 B
Go
Raw Normal View History

package signal
2018-04-02 07:52:16 +00:00
// 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 {
2018-07-01 10:38:40 +00:00
c chan struct{}
}
2018-02-11 22:28:42 +00:00
// NewNotifier creates a new Notifier.
func NewNotifier() *Notifier {
2018-07-01 10:38:40 +00:00
return &Notifier{
c: make(chan struct{}, 1),
}
}
2018-02-11 22:28:42 +00:00
// Signal signals a change, usually by producer. This method never blocks.
func (n *Notifier) Signal() {
2018-07-01 10:38:40 +00:00
select {
case n.c <- struct{}{}:
default:
}
}
2018-07-01 10:38:40 +00:00
// Wait returns a channel for waiting for changes. The returned channel never gets closed.
2018-02-08 14:39:46 +00:00
func (n *Notifier) Wait() <-chan struct{} {
2018-07-01 10:38:40 +00:00
return n.c
}