github.com/number571/tendermint@v0.34.11-gost/internal/libs/sync/waker.go (about) 1 package sync 2 3 // Waker is used to wake up a sleeper when some event occurs. It debounces 4 // multiple wakeup calls occurring between each sleep, and wakeups are 5 // non-blocking to avoid having to coordinate goroutines. 6 type Waker struct { 7 wakeCh chan struct{} 8 } 9 10 // NewWaker creates a new Waker. 11 func NewWaker() *Waker { 12 return &Waker{ 13 wakeCh: make(chan struct{}, 1), // buffer used for debouncing 14 } 15 } 16 17 // Sleep returns a channel that blocks until Wake() is called. 18 func (w *Waker) Sleep() <-chan struct{} { 19 return w.wakeCh 20 } 21 22 // Wake wakes up the sleeper. 23 func (w *Waker) Wake() { 24 // A non-blocking send with a size 1 buffer ensures that we never block, and 25 // that we queue up at most a single wakeup call between each Sleep(). 26 select { 27 case w.wakeCh <- struct{}{}: 28 default: 29 } 30 }