mediamtx/internal/confwatcher/confwatcher.go

114 lines
2.3 KiB
Go
Raw Normal View History

2022-09-17 19:19:45 +00:00
// Package confwatcher contains a configuration watcher.
package confwatcher
import (
"os"
"path/filepath"
"time"
"github.com/fsnotify/fsnotify"
)
const (
minInterval = 1 * time.Second
additionalWait = 10 * time.Millisecond
)
2020-11-05 11:30:25 +00:00
// ConfWatcher is a configuration file watcher.
type ConfWatcher struct {
inner *fsnotify.Watcher
watchedPath string
// out
signal chan struct{}
done chan struct{}
}
2020-11-05 11:30:25 +00:00
// New allocates a ConfWatcher.
func New(confPath string) (*ConfWatcher, error) {
if _, err := os.Stat(confPath); err != nil {
return nil, err
}
inner, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
// use absolute paths to support Darwin
absolutePath, _ := filepath.Abs(confPath)
parentPath := filepath.Dir(absolutePath)
err = inner.Add(parentPath)
if err != nil {
inner.Close()
return nil, err
}
w := &ConfWatcher{
inner: inner,
watchedPath: absolutePath,
signal: make(chan struct{}),
done: make(chan struct{}),
}
go w.run()
2021-08-01 14:58:46 +00:00
return w, nil
}
2020-11-05 11:30:25 +00:00
// Close closes a ConfWatcher.
func (w *ConfWatcher) Close() {
go func() {
for range w.signal {
}
}()
w.inner.Close()
<-w.done
}
func (w *ConfWatcher) run() {
defer close(w.done)
var lastCalled time.Time
previousWatchedPath, _ := filepath.EvalSymlinks(w.watchedPath)
outer:
for {
select {
case event := <-w.inner.Events:
if time.Since(lastCalled) < minInterval {
continue
}
currentWatchedPath, _ := filepath.EvalSymlinks(w.watchedPath)
eventPath, _ := filepath.Abs(event.Name)
if currentWatchedPath == "" {
2021-01-13 20:47:23 +00:00
// watched file was removed; wait for write event to trigger reload
previousWatchedPath = ""
} else if currentWatchedPath != previousWatchedPath ||
(eventPath == currentWatchedPath &&
((event.Op&fsnotify.Write) == fsnotify.Write ||
(event.Op&fsnotify.Create) == fsnotify.Create)) {
// wait some additional time to allow the writer to complete its job
time.Sleep(additionalWait)
previousWatchedPath = currentWatchedPath
lastCalled = time.Now()
w.signal <- struct{}{}
}
case <-w.inner.Errors:
break outer
}
}
close(w.signal)
}
2021-05-07 21:07:31 +00:00
// Watch returns a channel that is called after the configuration file has changed.
func (w *ConfWatcher) Watch() chan struct{} {
return w.signal
}