github.com/electroneum/electroneum-sc@v0.0.0-20230105223411-3bc1d078281e/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 "time" 24 25 "github.com/electroneum/electroneum-sc/log" 26 "github.com/fsnotify/fsnotify" 27 ) 28 29 type watcher struct { 30 ac *accountCache 31 starting bool 32 running bool 33 quit chan struct{} 34 } 35 36 func newWatcher(ac *accountCache) *watcher { 37 return &watcher{ 38 ac: ac, 39 quit: make(chan struct{}), 40 } 41 } 42 43 // starts the watcher loop in the background. 44 // Start a watcher in the background if that's not already in progress. 45 // The caller must hold w.ac.mu. 46 func (w *watcher) start() { 47 if w.starting || w.running { 48 return 49 } 50 w.starting = true 51 go w.loop() 52 } 53 54 func (w *watcher) close() { 55 close(w.quit) 56 } 57 58 func (w *watcher) loop() { 59 defer func() { 60 w.ac.mu.Lock() 61 w.running = false 62 w.starting = false 63 w.ac.mu.Unlock() 64 }() 65 logger := log.New("path", w.ac.keydir) 66 67 // Create new watcher. 68 watcher, err := fsnotify.NewWatcher() 69 if err != nil { 70 log.Error("Failed to start filesystem watcher", "err", err) 71 return 72 } 73 defer watcher.Close() 74 if err := watcher.Add(w.ac.keydir); err != nil { 75 logger.Warn("Failed to watch keystore folder", "err", err) 76 return 77 } 78 79 logger.Trace("Started watching keystore folder", "folder", w.ac.keydir) 80 defer logger.Trace("Stopped watching keystore folder") 81 82 w.ac.mu.Lock() 83 w.running = true 84 w.ac.mu.Unlock() 85 86 // Wait for file system events and reload. 87 // When an event occurs, the reload call is delayed a bit so that 88 // multiple events arriving quickly only cause a single reload. 89 var ( 90 debounceDuration = 500 * time.Millisecond 91 rescanTriggered = false 92 debounce = time.NewTimer(0) 93 ) 94 // Ignore initial trigger 95 if !debounce.Stop() { 96 <-debounce.C 97 } 98 defer debounce.Stop() 99 for { 100 select { 101 case <-w.quit: 102 return 103 case _, ok := <-watcher.Events: 104 if !ok { 105 return 106 } 107 // Trigger the scan (with delay), if not already triggered 108 if !rescanTriggered { 109 debounce.Reset(debounceDuration) 110 rescanTriggered = true 111 } 112 // The fsnotify library does provide more granular event-info, it 113 // would be possible to refresh individual affected files instead 114 // of scheduling a full rescan. For most cases though, the 115 // full rescan is quick and obviously simplest. 116 case err, ok := <-watcher.Errors: 117 if !ok { 118 return 119 } 120 log.Info("Filsystem watcher error", "err", err) 121 case <-debounce.C: 122 w.ac.scanAccounts() 123 rescanTriggered = false 124 } 125 } 126 }