2017-12-28 18:14:33 +01:00
|
|
|
package stopOnce
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
|
|
|
type Stopper struct {
|
|
|
|
ch chan struct{}
|
2018-03-29 02:01:44 +02:00
|
|
|
once *sync.Once
|
2017-12-28 18:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func New() *Stopper {
|
2018-03-29 02:01:44 +02:00
|
|
|
s := &Stopper{}
|
2017-12-28 18:14:33 +01:00
|
|
|
s.ch = make(chan struct{})
|
2018-03-29 02:01:44 +02:00
|
|
|
s.once = &sync.Once{}
|
|
|
|
return s
|
2017-12-28 18:14:33 +01:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|