watcher.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //go:build !nowatcher
  2. package watcher
  3. // #cgo LDFLAGS: -lwatcher-c -lstdc++
  4. // #include <stdint.h>
  5. // #include <stdlib.h>
  6. // #include "watcher.h"
  7. import "C"
  8. import (
  9. "errors"
  10. "runtime/cgo"
  11. "sync"
  12. "time"
  13. "unsafe"
  14. "go.uber.org/zap"
  15. )
  16. type watcher struct {
  17. sessions []C.uintptr_t
  18. callback func()
  19. trigger chan struct{}
  20. stop chan struct{}
  21. }
  22. // duration to wait before triggering a reload after a file change
  23. const debounceDuration = 150 * time.Millisecond
  24. var (
  25. // the currently active file watcher
  26. activeWatcher *watcher
  27. // after stopping the watcher we will wait for eventual reloads to finish
  28. reloadWaitGroup sync.WaitGroup
  29. // we are passing the logger from the main package to the watcher
  30. logger *zap.Logger
  31. AlreadyStartedError = errors.New("the watcher is already running")
  32. UnableToStartWatching = errors.New("unable to start the watcher")
  33. )
  34. func InitWatcher(filePatterns []string, callback func(), zapLogger *zap.Logger) error {
  35. if len(filePatterns) == 0 {
  36. return nil
  37. }
  38. if activeWatcher != nil {
  39. return AlreadyStartedError
  40. }
  41. logger = zapLogger
  42. activeWatcher = &watcher{callback: callback}
  43. err := activeWatcher.startWatching(filePatterns)
  44. if err != nil {
  45. return err
  46. }
  47. reloadWaitGroup = sync.WaitGroup{}
  48. return nil
  49. }
  50. func DrainWatcher() {
  51. if activeWatcher == nil {
  52. return
  53. }
  54. logger.Debug("stopping watcher")
  55. activeWatcher.stopWatching()
  56. reloadWaitGroup.Wait()
  57. activeWatcher = nil
  58. }
  59. func (w *watcher) startWatching(filePatterns []string) error {
  60. w.trigger = make(chan struct{})
  61. w.stop = make(chan struct{})
  62. w.sessions = make([]C.uintptr_t, len(filePatterns))
  63. watchPatterns, err := parseFilePatterns(filePatterns)
  64. if err != nil {
  65. return err
  66. }
  67. for i, watchPattern := range watchPatterns {
  68. watchPattern.trigger = w.trigger
  69. session, err := startSession(watchPattern)
  70. if err != nil {
  71. return err
  72. }
  73. w.sessions[i] = session
  74. }
  75. go listenForFileEvents(w.trigger, w.stop)
  76. return nil
  77. }
  78. func (w *watcher) stopWatching() {
  79. close(w.stop)
  80. for _, session := range w.sessions {
  81. stopSession(session)
  82. }
  83. }
  84. func startSession(w *watchPattern) (C.uintptr_t, error) {
  85. handle := cgo.NewHandle(w)
  86. cDir := C.CString(w.dir)
  87. defer C.free(unsafe.Pointer(cDir))
  88. watchSession := C.start_new_watcher(cDir, C.uintptr_t(handle))
  89. if watchSession != 0 {
  90. logger.Debug("watching", zap.String("dir", w.dir), zap.Strings("patterns", w.patterns))
  91. return watchSession, nil
  92. }
  93. logger.Error("couldn't start watching", zap.String("dir", w.dir))
  94. return watchSession, UnableToStartWatching
  95. }
  96. func stopSession(session C.uintptr_t) {
  97. success := C.stop_watcher(session)
  98. if success == 0 {
  99. logger.Warn("couldn't close the watcher")
  100. }
  101. }
  102. //export go_handle_file_watcher_event
  103. func go_handle_file_watcher_event(path *C.char, eventType C.int, pathType C.int, handle C.uintptr_t) {
  104. watchPattern := cgo.Handle(handle).Value().(*watchPattern)
  105. if watchPattern.allowReload(C.GoString(path), int(eventType), int(pathType)) {
  106. watchPattern.trigger <- struct{}{}
  107. }
  108. }
  109. func listenForFileEvents(triggerWatcher chan struct{}, stopWatcher chan struct{}) {
  110. timer := time.NewTimer(debounceDuration)
  111. timer.Stop()
  112. defer timer.Stop()
  113. for {
  114. select {
  115. case <-stopWatcher:
  116. break
  117. case <-triggerWatcher:
  118. timer.Reset(debounceDuration)
  119. case <-timer.C:
  120. timer.Stop()
  121. scheduleReload()
  122. }
  123. }
  124. }
  125. func scheduleReload() {
  126. logger.Info("filesystem change detected")
  127. reloadWaitGroup.Add(1)
  128. activeWatcher.callback()
  129. reloadWaitGroup.Done()
  130. }