@@ -39,10 +39,10 @@ import (
39
39
type Watcher struct {
40
40
path string
41
41
deprecatedPath string
42
- stopCh chan interface {}
42
+ stopCh chan struct {}
43
+ stopped chan struct {}
43
44
fs utilfs.Filesystem
44
45
fsWatcher * fsnotify.Watcher
45
- wg sync.WaitGroup
46
46
47
47
mutex sync.Mutex
48
48
handlers map [string ]PluginHandler
@@ -88,7 +88,8 @@ func (w *Watcher) getHandler(pluginType string) (PluginHandler, bool) {
88
88
// Start watches for the creation of plugin sockets at the path
89
89
func (w * Watcher ) Start () error {
90
90
klog .V (2 ).Infof ("Plugin Watcher Start at %s" , w .path )
91
- w .stopCh = make (chan interface {})
91
+ w .stopCh = make (chan struct {})
92
+ w .stopped = make (chan struct {})
92
93
93
94
// Creating the directory to be watched if it doesn't exist yet,
94
95
// and walks through the directory to discover the existing plugins.
@@ -104,22 +105,20 @@ func (w *Watcher) Start() error {
104
105
105
106
// Traverse plugin dir and add filesystem watchers before starting the plugin processing goroutine.
106
107
if err := w .traversePluginDir (w .path ); err != nil {
107
- w .Stop ()
108
+ w .fsWatcher . Close ()
108
109
return fmt .Errorf ("failed to traverse plugin socket path %q, err: %v" , w .path , err )
109
110
}
110
111
111
112
// Traverse deprecated plugin dir, if specified.
112
113
if len (w .deprecatedPath ) != 0 {
113
114
if err := w .traversePluginDir (w .deprecatedPath ); err != nil {
114
- w .Stop ()
115
+ w .fsWatcher . Close ()
115
116
return fmt .Errorf ("failed to traverse deprecated plugin socket path %q, err: %v" , w .deprecatedPath , err )
116
117
}
117
118
}
118
119
119
- w .wg .Add (1 )
120
- go func (fsWatcher * fsnotify.Watcher ) {
121
- defer w .wg .Done ()
122
-
120
+ go func () {
121
+ defer close (w .stopped )
123
122
for {
124
123
select {
125
124
case event := <- fsWatcher .Events :
@@ -135,17 +134,15 @@ func (w *Watcher) Start() error {
135
134
klog .Errorf ("error %v when handling delete event: %s" , err , event )
136
135
}
137
136
}
138
- continue
139
137
case err := <- fsWatcher .Errors :
140
138
if err != nil {
141
139
klog .Errorf ("fsWatcher received error: %v" , err )
142
140
}
143
- continue
144
141
case <- w .stopCh :
145
142
return
146
143
}
147
144
}
148
- }(fsWatcher )
145
+ }()
149
146
150
147
return nil
151
148
}
@@ -154,18 +151,9 @@ func (w *Watcher) Start() error {
154
151
func (w * Watcher ) Stop () error {
155
152
close (w .stopCh )
156
153
157
- c := make (chan struct {})
158
- var once sync.Once
159
- closeFunc := func () { close (c ) }
160
- go func () {
161
- defer once .Do (closeFunc )
162
- w .wg .Wait ()
163
- }()
164
-
165
154
select {
166
- case <- c :
155
+ case <- w . stopped :
167
156
case <- time .After (11 * time .Second ):
168
- once .Do (closeFunc )
169
157
return fmt .Errorf ("timeout on stopping watcher" )
170
158
}
171
159
0 commit comments