Skip to content

Commit

Permalink
add comments for the choice of concurrent prime (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong authored Nov 21, 2024
1 parent 31f0b86 commit 37bc6a3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
18 changes: 11 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ type Config struct {
onStatus func(loader Loader, changed bool, err error)
converter *convert.Converter

providers providers
onChanges onChanges
watchProvider atomic.Pointer[func(*provider)]
providers providers
onChanges onChanges
watched atomic.Pointer[func(*provider)]
}

// New creates a new Config with the given Option(s).
Expand Down Expand Up @@ -84,7 +84,7 @@ func (c *Config) Load(loader Loader) error {
return nil
}

// Special handling if loader is also watcher.
// Register status callback if the loader is a Statuser.
if statuser, ok := loader.(Statuser); ok {
statuser.Status(func(changed bool, err error) {
if err != nil {
Expand All @@ -101,7 +101,9 @@ func (c *Config) Load(loader Loader) error {
})
}

if watch := c.watchProvider.Load(); watch != nil {
// Register watch callback if the loader is a Watcher and the watch is started.
// While Config.Watch is called, c.watched is set for registering the watch callback.
if watch := c.watched.Load(); watch != nil {
(*watch)(provider)
}

Expand Down Expand Up @@ -286,8 +288,10 @@ func (p *providers) traverse(action func(*provider)) {
}

func (p *providers) sub(path []string) any {
p.mutex.RLock()
defer p.mutex.RUnlock()
// Here does not need lock since p.values is atomic pointer.
// The map of configuration is just swapping in and out,
// but the map itself is immutable.
// So unmarshal isn't blocked by Config.Load or updating changes by Watch.

val := p.values.Load()
if val == nil { // To support zero Config
Expand Down
4 changes: 3 additions & 1 deletion watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ func (c *Config) Watch(ctx context.Context) error { //nolint:cyclop,funlen,gocog
}
}

if !c.watchProvider.CompareAndSwap(nil, &watchProvider) {
// Set c.watched so that the loader loaded after Watch can register the watch callback.
// It's also used for marker that Watch has been called.
if !c.watched.CompareAndSwap(nil, &watchProvider) {
c.log(ctx, slog.LevelWarn, "Config has been watched, call Watch more than once has no effects.")

return nil
Expand Down

0 comments on commit 37bc6a3

Please sign in to comment.