From 531a50c776f149be1c9dd58272bd9ed1aee5aba8 Mon Sep 17 00:00:00 2001 From: Mike Danese Date: Tue, 14 May 2019 18:21:48 -0700 Subject: [PATCH] simplify pluginwatcher closing --- .../util/pluginwatcher/plugin_watcher.go | 32 ++++++------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/pkg/kubelet/util/pluginwatcher/plugin_watcher.go b/pkg/kubelet/util/pluginwatcher/plugin_watcher.go index 9dad92db84..4e12716c57 100644 --- a/pkg/kubelet/util/pluginwatcher/plugin_watcher.go +++ b/pkg/kubelet/util/pluginwatcher/plugin_watcher.go @@ -39,10 +39,10 @@ import ( type Watcher struct { path string deprecatedPath string - stopCh chan interface{} + stopCh chan struct{} + stopped chan struct{} fs utilfs.Filesystem fsWatcher *fsnotify.Watcher - wg sync.WaitGroup mutex sync.Mutex handlers map[string]PluginHandler @@ -88,7 +88,8 @@ func (w *Watcher) getHandler(pluginType string) (PluginHandler, bool) { // Start watches for the creation of plugin sockets at the path func (w *Watcher) Start() error { klog.V(2).Infof("Plugin Watcher Start at %s", w.path) - w.stopCh = make(chan interface{}) + w.stopCh = make(chan struct{}) + w.stopped = make(chan struct{}) // Creating the directory to be watched if it doesn't exist yet, // and walks through the directory to discover the existing plugins. @@ -104,22 +105,20 @@ func (w *Watcher) Start() error { // Traverse plugin dir and add filesystem watchers before starting the plugin processing goroutine. if err := w.traversePluginDir(w.path); err != nil { - w.Stop() + w.fsWatcher.Close() return fmt.Errorf("failed to traverse plugin socket path %q, err: %v", w.path, err) } // Traverse deprecated plugin dir, if specified. if len(w.deprecatedPath) != 0 { if err := w.traversePluginDir(w.deprecatedPath); err != nil { - w.Stop() + w.fsWatcher.Close() return fmt.Errorf("failed to traverse deprecated plugin socket path %q, err: %v", w.deprecatedPath, err) } } - w.wg.Add(1) - go func(fsWatcher *fsnotify.Watcher) { - defer w.wg.Done() - + go func() { + defer close(w.stopped) for { select { case event := <-fsWatcher.Events: @@ -135,17 +134,15 @@ func (w *Watcher) Start() error { klog.Errorf("error %v when handling delete event: %s", err, event) } } - continue case err := <-fsWatcher.Errors: if err != nil { klog.Errorf("fsWatcher received error: %v", err) } - continue case <-w.stopCh: return } } - }(fsWatcher) + }() return nil } @@ -154,18 +151,9 @@ func (w *Watcher) Start() error { func (w *Watcher) Stop() error { close(w.stopCh) - c := make(chan struct{}) - var once sync.Once - closeFunc := func() { close(c) } - go func() { - defer once.Do(closeFunc) - w.wg.Wait() - }() - select { - case <-c: + case <-w.stopped: case <-time.After(11 * time.Second): - once.Do(closeFunc) return fmt.Errorf("timeout on stopping watcher") }