29 lines
453 B
Go
29 lines
453 B
Go
package coalesce
|
|
|
|
import "context"
|
|
|
|
// Signal keeps at most one pending notification and never blocks producers.
|
|
type Signal struct {
|
|
ready chan struct{}
|
|
}
|
|
|
|
func NewSignal() *Signal {
|
|
return &Signal{ready: make(chan struct{}, 1)}
|
|
}
|
|
|
|
func (s *Signal) Notify() {
|
|
select {
|
|
case s.ready <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|
|
|
|
func (s *Signal) Wait(ctx context.Context) error {
|
|
select {
|
|
case <-s.ready:
|
|
return nil
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
}
|