From 4ed26f31165f9b346c2d5be3bfb08c5593aa46f9 Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Fri, 9 Mar 2018 14:43:16 -0800 Subject: [PATCH] pkg/store: use a sync.Once to synchronize channel close Signed-off-by: Stephen J Day --- pkg/store/util.go | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/pkg/store/util.go b/pkg/store/util.go index 8ea173e0d..74401edf1 100644 --- a/pkg/store/util.go +++ b/pkg/store/util.go @@ -20,28 +20,20 @@ import "sync" // StopCh is used to propagate the stop information of a container. type StopCh struct { - mu sync.Mutex - ch chan struct{} - closed bool + ch chan struct{} + once sync.Once } // NewStopCh creates a stop channel. The channel is open by default. func NewStopCh() *StopCh { - return &StopCh{ - ch: make(chan struct{}), - closed: false, - } + return &StopCh{ch: make(chan struct{})} } // Stop close stopCh of the container. func (s *StopCh) Stop() { - s.mu.Lock() - defer s.mu.Unlock() - if s.closed { - return - } - close(s.ch) - s.closed = true + s.once.Do(func() { + close(s.ch) + }) } // Stopped return the stopCh of the container as a readonly channel.