github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/blockchain/pseudohsm/watch.go (about) 1 // +build darwin,!ios freebsd linux,!arm64 netbsd solaris windows 2 3 package pseudohsm 4 5 //"fmt" 6 //"github.com/rjeczalik/notify" 7 //"time" 8 9 type watcher struct { 10 kc *keyCache 11 starting bool 12 running bool 13 //ev chan notify.EventInfo 14 quit chan struct{} 15 } 16 17 func newWatcher(kc *keyCache) *watcher { 18 return &watcher{ 19 kc: kc, 20 //ev: make(chan notify.EventInfo, 10), 21 quit: make(chan struct{}), 22 } 23 } 24 25 // starts the watcher loop in the background. 26 // Start a watcher in the background if that's not already in progress. 27 // The caller must hold w.kc.mu. 28 func (w *watcher) start() { 29 if w.starting || w.running { 30 return 31 } 32 w.starting = true 33 go w.loop() 34 } 35 36 func (w *watcher) close() { 37 close(w.quit) 38 } 39 40 func (w *watcher) loop() { 41 defer func() { 42 w.kc.mu.Lock() 43 w.running = false 44 w.starting = false 45 w.kc.mu.Unlock() 46 }() 47 48 /* 49 err := notify.Watch(w.kc.keydir, w.ev, notify.All) 50 if err != nil { 51 fmt.Printf("can't watch %s: %v", w.kc.keydir, err) 52 return 53 } 54 defer notify.Stop(w.ev) 55 fmt.Printf("now watching %s", w.kc.keydir) 56 defer fmt.Printf("no longer watching %s", w.kc.keydir) 57 58 w.kc.mu.Lock() 59 w.running = true 60 w.kc.mu.Unlock() 61 62 // Wait for file system events and reload. 63 // When an event occurs, the reload call is delayed a bit so that 64 // multiple events arriving quickly only cause a single reload. 65 var ( 66 debounce = time.NewTimer(0) 67 debounceDuration = 500 * time.Millisecond 68 inCycle, hadEvent bool 69 ) 70 defer debounce.Stop() 71 for { 72 select { 73 case <-w.quit: 74 return 75 case <-w.ev: 76 if !inCycle { 77 debounce.Reset(debounceDuration) 78 inCycle = true 79 } else { 80 hadEvent = true 81 } 82 case <-debounce.C: 83 w.kc.mu.Lock() 84 w.kc.reload() 85 w.kc.mu.Unlock() 86 if hadEvent { 87 debounce.Reset(debounceDuration) 88 inCycle, hadEvent = true, false 89 } else { 90 inCycle, hadEvent = false, false 91 } 92 } 93 } 94 */ 95 }