2017-12-28 18:14:33 +01:00
|
|
|
package stopOnce
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
|
|
|
type Stopper struct {
|
|
|
|
ch chan struct{}
|
|
|
|
once sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() *Stopper {
|
|
|
|
s := Stopper{}
|
|
|
|
s.ch = make(chan struct{})
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
2018-03-09 01:27:54 +01:00
|
|
|
func (s *Stopper) Chan() <-chan struct{} {
|
2017-12-28 18:14:33 +01:00
|
|
|
return s.ch
|
|
|
|
}
|
|
|
|
|
2018-03-09 01:27:54 +01:00
|
|
|
func (s *Stopper) Stop() {
|
2017-12-28 18:14:33 +01:00
|
|
|
s.once.Do(func() {
|
|
|
|
close(s.ch)
|
|
|
|
})
|
|
|
|
}
|