github.com/luckypickle/go-ethereum-vet@v1.14.2/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) || freebsd || (linux && !arm64) || netbsd || solaris 18 // +build darwin,!ios freebsd linux,!arm64 netbsd solaris 19 20 package keystore 21 22 import ( 23 "time" 24 25 "github.com/luckypickle/go-ethereum-vet/log" 26 "github.com/rjeczalik/notify" 27 ) 28 29 type watcher struct { 30 ac *accountCache 31 starting bool 32 running bool 33 ev chan notify.EventInfo 34 quit chan struct{} 35 } 36 37 func newWatcher(ac *accountCache) *watcher { 38 return &watcher{ 39 ac: ac, 40 ev: make(chan notify.EventInfo, 10), 41 quit: make(chan struct{}), 42 } 43 } 44 45 // starts the watcher loop in the background. 46 // Start a watcher in the background if that's not already in progress. 47 // The caller must hold w.ac.mu. 48 func (w *watcher) start() { 49 if w.starting || w.running { 50 return 51 } 52 w.starting = true 53 go w.loop() 54 } 55 56 func (w *watcher) close() { 57 close(w.quit) 58 } 59 60 func (w *watcher) loop() { 61 defer func() { 62 w.ac.mu.Lock() 63 w.running = false 64 w.starting = false 65 w.ac.mu.Unlock() 66 }() 67 logger := log.New("path", w.ac.keydir) 68 69 if err := notify.Watch(w.ac.keydir, w.ev, notify.All); err != nil { 70 logger.Trace("Failed to watch keystore folder", "err", err) 71 return 72 } 73 defer notify.Stop(w.ev) 74 logger.Trace("Started watching keystore folder") 75 defer logger.Trace("Stopped watching keystore folder") 76 77 w.ac.mu.Lock() 78 w.running = true 79 w.ac.mu.Unlock() 80 81 // Wait for file system events and reload. 82 // When an event occurs, the reload call is delayed a bit so that 83 // multiple events arriving quickly only cause a single reload. 84 var ( 85 debounceDuration = 500 * time.Millisecond 86 rescanTriggered = false 87 debounce = time.NewTimer(0) 88 ) 89 // Ignore initial trigger 90 if !debounce.Stop() { 91 <-debounce.C 92 } 93 defer debounce.Stop() 94 for { 95 select { 96 case <-w.quit: 97 return 98 case <-w.ev: 99 // Trigger the scan (with delay), if not already triggered 100 if !rescanTriggered { 101 debounce.Reset(debounceDuration) 102 rescanTriggered = true 103 } 104 case <-debounce.C: 105 w.ac.scanAccounts() 106 rescanTriggered = false 107 } 108 } 109 }