The credential wait's test stays under a second, and a failed watch says the re-read, not a restart (issue 1496)
Review of 3baeaec: TestAwaitCredential took 1.9 s, over the unit limit; it now
sets the watch's settle to 10 ms, writes at 20 ms and gives up at 100 ms (0.2 s).
The watch's journal line for a failure named a bundle and a restart; the
credential wait now says it is read again every 30 s instead.
This commit is contained in:
@@ -35,24 +35,31 @@ type bundleWatch struct {
|
||||
changed func()
|
||||
logf func(string, ...any)
|
||||
done chan struct{}
|
||||
// what names what is watched, and lost what follows when the watch fails, in the words a journal line carries.
|
||||
what, lost string
|
||||
}
|
||||
|
||||
// WatchFile hears, as the bundles are heard, the directory of the file at path, or the nearest directory above it that
|
||||
// exists, and calls changed once at its start and once after each burst of changes there has settled: a runtime waiting
|
||||
// for its broker credential reads it again when the node-engine writes it, never on a clock (issue 1496). It answers its
|
||||
// stop.
|
||||
func WatchFile(path string, changed func(), logf func(string, ...any)) (func(), error) {
|
||||
return watchBundles([]string{path}, changed, logf)
|
||||
func WatchFile(path string, changed func(), logf func(string, ...any), lost string) (func(), error) {
|
||||
return watch([]string{path}, changed, logf, path, lost)
|
||||
}
|
||||
|
||||
// watchBundles starts the watch of the entrypoints at paths. It answers its stop.
|
||||
func watchBundles(paths []string, changed func(), logf func(string, ...any)) (func(), error) {
|
||||
return watch(paths, changed, logf, "the bundles", "a bundle placed or changed there is served only after a restart")
|
||||
}
|
||||
|
||||
// watch starts the watch of the files at paths, named what in its words; lost says what follows when it fails.
|
||||
func watch(paths []string, changed func(), logf func(string, ...any), what, lost string) (func(), error) {
|
||||
fd, err := unix.InotifyInit1(unix.IN_CLOEXEC | unix.IN_NONBLOCK)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("inotify: %w", err)
|
||||
}
|
||||
w := &bundleWatch{fd: fd, paths: paths, byWD: map[int]string{}, settle: Settle, changed: changed, logf: logf,
|
||||
done: make(chan struct{})}
|
||||
done: make(chan struct{}), what: what, lost: lost}
|
||||
if err := unix.Pipe2(w.wake[:], unix.O_CLOEXEC|unix.O_NONBLOCK); err != nil {
|
||||
unix.Close(fd)
|
||||
return nil, fmt.Errorf("pipe: %w", err)
|
||||
@@ -83,7 +90,7 @@ func (w *bundleWatch) arm() {
|
||||
}
|
||||
wd, err := unix.InotifyAddWatch(w.fd, dir, watchMask)
|
||||
if err != nil {
|
||||
w.logf("[mesh-tools] cannot watch %s for %s's bundle: %v; a bundle placed or changed there is served only after a restart", dir, p, err)
|
||||
w.logf("[mesh-tools] cannot watch %s for %s: %v; %s", dir, p, err, w.lost)
|
||||
break
|
||||
}
|
||||
w.byWD[wd] = dir
|
||||
@@ -126,7 +133,7 @@ func (w *bundleWatch) loop() {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
w.logf("[mesh-tools] the watch of the bundles stopped: %v; a bundle placed or changed later is served only after a restart", err)
|
||||
w.logf("[mesh-tools] the watch of %s stopped: %v; %s", w.what, err, w.lost)
|
||||
return
|
||||
}
|
||||
if fds[1].Revents != 0 {
|
||||
|
||||
Reference in New Issue
Block a user