Fix a potential panic

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu
2018-02-22 03:16:41 +00:00
parent 7f5687c801
commit f5390d01d6
3 changed files with 29 additions and 9 deletions

View File

@@ -16,15 +16,35 @@ limitations under the License.
package store
import "sync"
// StopCh is used to propagate the stop information of a container.
type StopCh chan struct{}
type StopCh struct {
mu sync.Mutex
ch chan struct{}
closed bool
}
// NewStopCh creates a stop channel. The channel is open by default.
func NewStopCh() *StopCh {
return &StopCh{
ch: make(chan struct{}),
closed: false,
}
}
// Stop close stopCh of the container.
func (ch StopCh) Stop() {
close(ch)
func (s *StopCh) Stop() {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return
}
close(s.ch)
s.closed = true
}
// Stopped return the stopCh of the container as a readonly channel.
func (ch StopCh) Stopped() <-chan struct{} {
return ch
func (s *StopCh) Stopped() <-chan struct{} {
return s.ch
}