lbry.go/stopOnce/stopOnce.go

26 lines
324 B
Go
Raw Normal View History

package stopOnce
import "sync"
type Stopper struct {
ch chan struct{}
2018-03-29 02:01:44 +02:00
once *sync.Once
}
func New() *Stopper {
2018-03-29 02:01:44 +02:00
s := &Stopper{}
s.ch = make(chan struct{})
2018-03-29 02:01:44 +02:00
s.once = &sync.Once{}
return s
}
func (s *Stopper) Chan() <-chan struct{} {
return s.ch
}
func (s *Stopper) Stop() {
s.once.Do(func() {
close(s.ch)
})
}