watch.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 accounts
  18. import (
  19. "time"
  20. "github.com/ethereum/go-ethereum/logger"
  21. "github.com/ethereum/go-ethereum/logger/glog"
  22. "github.com/rjeczalik/notify"
  23. )
  24. type watcher struct {
  25. ac *addrCache
  26. starting bool
  27. running bool
  28. ev chan notify.EventInfo
  29. quit chan struct{}
  30. }
  31. func newWatcher(ac *addrCache) *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. err := notify.Watch(w.ac.keydir, w.ev, notify.All)
  59. if err != nil {
  60. glog.V(logger.Detail).Infof("can't watch %s: %v", w.ac.keydir, err)
  61. return
  62. }
  63. defer notify.Stop(w.ev)
  64. glog.V(logger.Detail).Infof("now watching %s", w.ac.keydir)
  65. defer glog.V(logger.Detail).Infof("no longer watching %s", w.ac.keydir)
  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. debounce = time.NewTimer(0)
  74. debounceDuration = 500 * time.Millisecond
  75. inCycle, hadEvent bool
  76. )
  77. defer debounce.Stop()
  78. for {
  79. select {
  80. case <-w.quit:
  81. return
  82. case <-w.ev:
  83. if !inCycle {
  84. debounce.Reset(debounceDuration)
  85. inCycle = true
  86. } else {
  87. hadEvent = true
  88. }
  89. case <-debounce.C:
  90. w.ac.mu.Lock()
  91. w.ac.reload()
  92. w.ac.mu.Unlock()
  93. if hadEvent {
  94. debounce.Reset(debounceDuration)
  95. inCycle, hadEvent = true, false
  96. } else {
  97. inCycle, hadEvent = false, false
  98. }
  99. }
  100. }
  101. }