1
0
Fork 0
mirror of https://github.com/Luzifer/go_helpers.git synced 2024-12-25 05:21:20 +00:00

Fix logic bug in run loop, replace Stat with Lstat

in order to use a less expensive syscall when applied to symlinks like
in Kubernetes ConfigMap mounts

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-03-18 15:26:18 +01:00
parent 39823d6cdd
commit d1f1007b33
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5
2 changed files with 10 additions and 8 deletions

View file

@ -83,7 +83,7 @@ func newWatcher(filePath string, interval time.Duration, checks ...WatcherCheck)
stateCache: make(map[string]any), stateCache: make(map[string]any),
} }
// Initially run checks once // Initially run checks once
_, err := w.runStateChecks(true) _, err := w.runStateChecks()
return w, errors.Wrap(err, "executing initial checks") return w, errors.Wrap(err, "executing initial checks")
} }
@ -108,7 +108,7 @@ func (w *Watcher) SetState(key string, value any) {
func (w *Watcher) loop() { func (w *Watcher) loop() {
for { for {
evt, err := w.runStateChecks(false) evt, err := w.runStateChecks()
if err != nil { if err != nil {
w.Err = err w.Err = err
break break
@ -123,14 +123,16 @@ func (w *Watcher) loop() {
} }
} }
func (w *Watcher) runStateChecks(runAll bool) (WatcherEvent, error) { func (w *Watcher) runStateChecks() (WatcherEvent, error) {
for _, c := range w.checks { for _, c := range w.checks {
evt, err := c(w) evt, err := c(w)
if err != nil { if err != nil {
return WatcherEventInvalid, errors.Wrap(err, "checking file state") return WatcherEventInvalid, errors.Wrap(err, "checking file state")
} }
if evt == WatcherEventNoChange && !runAll { if evt == WatcherEventNoChange {
// Watcher noticed no change, ask the next one. If one notices
// a change we will return that one.
continue continue
} }

View file

@ -29,7 +29,7 @@ func WatcherCheckHash(hcf func() hash.Hash) WatcherCheck {
lastHash = v lastHash = v
} }
if _, err := os.Stat(w.FilePath); errors.Is(err, fs.ErrNotExist) { if _, err := os.Lstat(w.FilePath); errors.Is(err, fs.ErrNotExist) {
return WatcherEventInvalid, nil return WatcherEventInvalid, nil
} }
@ -65,7 +65,7 @@ func WatcherCheckMtime(w *Watcher) (WatcherEvent, error) {
lastChange = v lastChange = v
} }
s, err := os.Stat(w.FilePath) s, err := os.Lstat(w.FilePath)
switch { switch {
case err == nil: case err == nil:
// handle size change // handle size change
@ -94,7 +94,7 @@ func WatcherCheckPresence(w *Watcher) (WatcherEvent, error) {
wasPresent = v wasPresent = v
} }
_, err := os.Stat(w.FilePath) _, err := os.Lstat(w.FilePath)
if err != nil && !errors.Is(err, fs.ErrNotExist) { if err != nil && !errors.Is(err, fs.ErrNotExist) {
// Some weird error occurred // Some weird error occurred
return WatcherEventInvalid, errors.Wrap(err, "getting file stat") return WatcherEventInvalid, errors.Wrap(err, "getting file stat")
@ -124,7 +124,7 @@ func WatcherCheckSize(w *Watcher) (WatcherEvent, error) {
knownSize = v knownSize = v
} }
s, err := os.Stat(w.FilePath) s, err := os.Lstat(w.FilePath)
switch { switch {
case err == nil: case err == nil:
// handle size change // handle size change