watch.go 2.7 KB

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