Правка условия гонки и оптимизация

Этот коммит содержится в:
Softonik 2024-02-28 17:00:03 +03:00 коммит произвёл Nobody
родитель 2803240615
коммит 3772187d95

Просмотреть файл

@ -108,19 +108,26 @@ func (s *Storage) createNewWatcher() (*fsnotify.Watcher, error) {
} }
func (s *Storage) watcherListener() { func (s *Storage) watcherListener() {
for { s.lock.RLock()
select { if s.watcher == nil {
case event, ok := <-s.watcher.Events: s.lock.RUnlock()
if !ok { s.reCreateWatcher()
return return
} }
s.handleWatcherEvent(event)
case err, ok := <-s.watcher.Errors: defer s.lock.RUnlock()
if !ok {
return select {
} case event, ok := <-s.watcher.Events:
log.Println("error:", err) if !ok {
return
} }
s.handleWatcherEvent(event)
case err, ok := <-s.watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
} }
} }
func (s *Storage) handleWatcherEvent(event fsnotify.Event) { func (s *Storage) handleWatcherEvent(event fsnotify.Event) {
@ -162,8 +169,13 @@ func (s *Storage) Close() {
s.closeWatcher() s.closeWatcher()
} }
func (s *Storage) closeWatcher() { func (s *Storage) closeWatcher() {
s.lock.RLock()
if s.watcher != nil { if s.watcher != nil {
s.watcher.Close() s.watcher.Close()
s.watcher = nil
} }
s.lock.RUnlock()
s.lock.Lock()
defer s.lock.Unlock()
s.watcher = nil
} }