github.com/ethereum/go-ethereum@v1.14.3/accounts/keystore/watch.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  //go:build (darwin && !ios && cgo) || freebsd || (linux && !arm64) || netbsd || solaris
    18  // +build darwin,!ios,cgo freebsd linux,!arm64 netbsd solaris
    19  
    20  package keystore
    21  
    22  import (
    23  	"os"
    24  	"time"
    25  
    26  	"github.com/ethereum/go-ethereum/log"
    27  	"github.com/fsnotify/fsnotify"
    28  )
    29  
    30  type watcher struct {
    31  	ac       *accountCache
    32  	running  bool // set to true when runloop begins
    33  	runEnded bool // set to true when runloop ends
    34  	starting bool // set to true prior to runloop starting
    35  	quit     chan struct{}
    36  }
    37  
    38  func newWatcher(ac *accountCache) *watcher {
    39  	return &watcher{
    40  		ac:   ac,
    41  		quit: make(chan struct{}),
    42  	}
    43  }
    44  
    45  // enabled returns false on systems not supported.
    46  func (*watcher) enabled() bool { return true }
    47  
    48  // starts the watcher loop in the background.
    49  // Start a watcher in the background if that's not already in progress.
    50  // The caller must hold w.ac.mu.
    51  func (w *watcher) start() {
    52  	if w.starting || w.running {
    53  		return
    54  	}
    55  	w.starting = true
    56  	go w.loop()
    57  }
    58  
    59  func (w *watcher) close() {
    60  	close(w.quit)
    61  }
    62  
    63  func (w *watcher) loop() {
    64  	defer func() {
    65  		w.ac.mu.Lock()
    66  		w.running = false
    67  		w.starting = false
    68  		w.runEnded = true
    69  		w.ac.mu.Unlock()
    70  	}()
    71  	logger := log.New("path", w.ac.keydir)
    72  
    73  	// Create new watcher.
    74  	watcher, err := fsnotify.NewWatcher()
    75  	if err != nil {
    76  		log.Error("Failed to start filesystem watcher", "err", err)
    77  		return
    78  	}
    79  	defer watcher.Close()
    80  	if err := watcher.Add(w.ac.keydir); err != nil {
    81  		if !os.IsNotExist(err) {
    82  			logger.Warn("Failed to watch keystore folder", "err", err)
    83  		}
    84  		return
    85  	}
    86  
    87  	logger.Trace("Started watching keystore folder", "folder", w.ac.keydir)
    88  	defer logger.Trace("Stopped watching keystore folder")
    89  
    90  	w.ac.mu.Lock()
    91  	w.running = true
    92  	w.ac.mu.Unlock()
    93  
    94  	// Wait for file system events and reload.
    95  	// When an event occurs, the reload call is delayed a bit so that
    96  	// multiple events arriving quickly only cause a single reload.
    97  	var (
    98  		debounceDuration = 500 * time.Millisecond
    99  		rescanTriggered  = false
   100  		debounce         = time.NewTimer(0)
   101  	)
   102  	// Ignore initial trigger
   103  	if !debounce.Stop() {
   104  		<-debounce.C
   105  	}
   106  	defer debounce.Stop()
   107  	for {
   108  		select {
   109  		case <-w.quit:
   110  			return
   111  		case _, ok := <-watcher.Events:
   112  			if !ok {
   113  				return
   114  			}
   115  			// Trigger the scan (with delay), if not already triggered
   116  			if !rescanTriggered {
   117  				debounce.Reset(debounceDuration)
   118  				rescanTriggered = true
   119  			}
   120  			// The fsnotify library does provide more granular event-info, it
   121  			// would be possible to refresh individual affected files instead
   122  			// of scheduling a full rescan. For most cases though, the
   123  			// full rescan is quick and obviously simplest.
   124  		case err, ok := <-watcher.Errors:
   125  			if !ok {
   126  				return
   127  			}
   128  			log.Info("Filesystem watcher error", "err", err)
   129  		case <-debounce.C:
   130  			w.ac.scanAccounts()
   131  			rescanTriggered = false
   132  		}
   133  	}
   134  }