From 26281ed40438b2518d448ada220a732e96be4448 Mon Sep 17 00:00:00 2001 From: ktong Date: Sun, 11 Feb 2024 09:44:22 -0800 Subject: [PATCH] remove point in providers slice --- config.go | 4 ++-- provider/appconfig/appconfig.go | 17 +++++++++-------- provider/pflag/option.go | 2 +- watch.go | 6 +++--- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/config.go b/config.go index 6fb25240..7e1a4b57 100644 --- a/config.go +++ b/config.go @@ -25,7 +25,7 @@ type Config struct { delimiter string values map[string]any - providers []*provider + providers []provider onChanges map[string][]func(*Config) onChangesMutex sync.RWMutex @@ -84,7 +84,7 @@ func (c *Config) Load(loader Loader) error { } maps.Merge(c.values, values) - provider := &provider{ + provider := provider{ loader: loader, values: make(map[string]any), } diff --git a/provider/appconfig/appconfig.go b/provider/appconfig/appconfig.go index b65745d2..9ee98915 100644 --- a/provider/appconfig/appconfig.go +++ b/provider/appconfig/appconfig.go @@ -96,8 +96,9 @@ func (a *AppConfig) Load() (map[string]any, error) { } a.token.Store(output.InitialConfigurationToken) } + values, _, err := a.load(ctx) - return a.load(ctx) + return values, err } func (a *AppConfig) Watch(ctx context.Context, onChange func(map[string]any)) error { @@ -107,7 +108,7 @@ func (a *AppConfig) Watch(ctx context.Context, onChange func(map[string]any)) er for { select { case <-ticker.C: - values, err := a.load(ctx) + values, changed, err := a.load(ctx) if err != nil { a.logger.WarnContext( ctx, "Error when reloading from AWS AppConfig", @@ -120,7 +121,7 @@ func (a *AppConfig) Watch(ctx context.Context, onChange func(map[string]any)) er continue } - if values != nil { + if changed { onChange(values) } case <-ctx.Done(): @@ -129,7 +130,7 @@ func (a *AppConfig) Watch(ctx context.Context, onChange func(map[string]any)) er } } -func (a *AppConfig) load(ctx context.Context) (map[string]any, error) { +func (a *AppConfig) load(ctx context.Context) (map[string]any, bool, error) { ctx, cancel := context.WithTimeout(ctx, a.timeout) defer cancel() @@ -138,22 +139,22 @@ func (a *AppConfig) load(ctx context.Context) (map[string]any, error) { } output, err := a.client.GetLatestConfiguration(ctx, input) if err != nil { - return nil, err + return nil, false, err } a.token.Store(output.NextPollConfigurationToken) if len(output.Configuration) == 0 { // It may return empty configuration data // if the client already has the latest version. - return nil, nil //nolint:nilnil // Use nil to indicate no change + return nil, false, nil } var out map[string]any if e := a.unmarshal(output.Configuration, &out); e != nil { - return nil, fmt.Errorf("unmarshal: %w", e) + return nil, false, fmt.Errorf("unmarshal: %w", e) } - return out, nil + return out, true, nil } func (a *AppConfig) String() string { diff --git a/provider/pflag/option.go b/provider/pflag/option.go index ac0362f6..698c9708 100644 --- a/provider/pflag/option.go +++ b/provider/pflag/option.go @@ -47,7 +47,7 @@ func WithDelimiter(delimiter string) Option { // If it returns an nil/[]string{}/[]string{""}, the variable will be ignored. // // For example, with the default splitter, an flag name like "parent.child.key" -// // would be split into "parent", "child", and "key". +// would be split into "parent", "child", and "key". func WithNameSplitter(splitter func(string) []string) Option { return func(options *options) { options.splitter = splitter diff --git a/watch.go b/watch.go index 7348cac7..dd3efe31 100644 --- a/watch.go +++ b/watch.go @@ -26,7 +26,7 @@ func (c *Config) Watch(ctx context.Context) error { //nolint:cyclop,funlen,gocog panic("cannot watch change with nil context") } - if hasWatcher := slices.ContainsFunc(c.providers, func(provider *provider) bool { + if hasWatcher := slices.ContainsFunc(c.providers, func(provider provider) bool { _, ok := provider.loader.(Watcher) return ok @@ -97,8 +97,8 @@ func (c *Config) Watch(ctx context.Context) error { //nolint:cyclop,funlen,gocog }() errChan := make(chan error, len(c.providers)) - for _, provider := range c.providers { - provider := provider + for i := range c.providers { + provider := &c.providers[i] // Use pointer for later modification. if watcher, ok := provider.loader.(Watcher); ok { waitGroup.Add(1)