Skip to content

Commit

Permalink
deal with thread safety
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly committed Oct 10, 2023
1 parent 087897a commit 6719df5
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions configmap/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"path"
"strings"
"sync/atomic"

"fortio.org/dflag"
"fortio.org/dflag/dynloglevel"
Expand All @@ -38,7 +39,7 @@ type Updater struct {
watcher *fsnotify.Watcher
flagSet *flag.FlagSet
done chan bool
warnings int // Count of unknown flags that have been logged.
warnings atomic.Int32 // Count of unknown flags that have been logged (increases at each iteration).
}

// Setup is a combination/shortcut for New+Initialize+Start.
Expand Down Expand Up @@ -73,7 +74,6 @@ func New(flagSet *flag.FlagSet, dirPath string) (*Updater, error) {
watcher: watcher,
started: false,
done: nil,
warnings: 0,
}, nil
}

Expand Down Expand Up @@ -132,7 +132,7 @@ func (u *Updater) readAll(dynamicOnly bool) error {
// ignore
} else if errors.Is(err, errFlagNotFound) {
log.S(log.Warning, "config map for unknown flag", log.Str("flag", f.Name()), log.Str("path", fullPath))
u.warnings++
u.warnings.Add(1)
} else {
errorStrings = append(errorStrings, fmt.Sprintf("flag %v: %v", f.Name(), err.Error()))
}
Expand All @@ -145,9 +145,9 @@ func (u *Updater) readAll(dynamicOnly bool) error {
return nil
}

// Return the warnings count. Thread safety?
// Return the warnings count.
func (u *Updater) Warnings() int {
return u.warnings
return int(u.warnings.Load())
}

func (u *Updater) readFlagFile(fullPath string, dynamicOnly bool) error {
Expand Down

0 comments on commit 6719df5

Please sign in to comment.