github.com/murrekatt/go-ethereum@v1.5.8-0.20170123175102-fc52f2c007fb/accounts/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 // +build darwin,!ios freebsd linux,!arm64 netbsd solaris 18 19 package accounts 20 21 import ( 22 "time" 23 24 "github.com/ethereum/go-ethereum/logger" 25 "github.com/ethereum/go-ethereum/logger/glog" 26 "github.com/rjeczalik/notify" 27 ) 28 29 type watcher struct { 30 ac *addrCache 31 starting bool 32 running bool 33 ev chan notify.EventInfo 34 quit chan struct{} 35 } 36 37 func newWatcher(ac *addrCache) *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 68 err := notify.Watch(w.ac.keydir, w.ev, notify.All) 69 if err != nil { 70 glog.V(logger.Detail).Infof("can't watch %s: %v", w.ac.keydir, err) 71 return 72 } 73 defer notify.Stop(w.ev) 74 glog.V(logger.Detail).Infof("now watching %s", w.ac.keydir) 75 defer glog.V(logger.Detail).Infof("no longer watching %s", w.ac.keydir) 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 debounce = time.NewTimer(0) 86 debounceDuration = 500 * time.Millisecond 87 inCycle, hadEvent bool 88 ) 89 defer debounce.Stop() 90 for { 91 select { 92 case <-w.quit: 93 return 94 case <-w.ev: 95 if !inCycle { 96 debounce.Reset(debounceDuration) 97 inCycle = true 98 } else { 99 hadEvent = true 100 } 101 case <-debounce.C: 102 w.ac.mu.Lock() 103 w.ac.reload() 104 w.ac.mu.Unlock() 105 if hadEvent { 106 debounce.Reset(debounceDuration) 107 inCycle, hadEvent = true, false 108 } else { 109 inCycle, hadEvent = false, false 110 } 111 } 112 } 113 }