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