Merge pull request #654 from stevvooe/simplify-stopch
pkg/store: use a sync.Once to synchronize channel close
This commit is contained in:
commit
80b2f751d3
@ -20,28 +20,20 @@ import "sync"
|
|||||||
|
|
||||||
// StopCh is used to propagate the stop information of a container.
|
// StopCh is used to propagate the stop information of a container.
|
||||||
type StopCh struct {
|
type StopCh struct {
|
||||||
mu sync.Mutex
|
|
||||||
ch chan struct{}
|
ch chan struct{}
|
||||||
closed bool
|
once sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStopCh creates a stop channel. The channel is open by default.
|
// NewStopCh creates a stop channel. The channel is open by default.
|
||||||
func NewStopCh() *StopCh {
|
func NewStopCh() *StopCh {
|
||||||
return &StopCh{
|
return &StopCh{ch: make(chan struct{})}
|
||||||
ch: make(chan struct{}),
|
|
||||||
closed: false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop close stopCh of the container.
|
// Stop close stopCh of the container.
|
||||||
func (s *StopCh) Stop() {
|
func (s *StopCh) Stop() {
|
||||||
s.mu.Lock()
|
s.once.Do(func() {
|
||||||
defer s.mu.Unlock()
|
|
||||||
if s.closed {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
close(s.ch)
|
close(s.ch)
|
||||||
s.closed = true
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stopped return the stopCh of the container as a readonly channel.
|
// Stopped return the stopCh of the container as a readonly channel.
|
||||||
|
Loading…
Reference in New Issue
Block a user