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