fix(config): load env and file source order bug (#1220)

* fix config load env & file order bug

* fix watch issue

* return empty as each update can be resolved
pull/1227/head
Kuiba 3 years ago committed by GitHub
parent ab45baf5ec
commit 0e70bddaa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      config/config.go
  2. 2
      config/env/env_test.go
  3. 8
      config/reader.go

@ -67,11 +67,15 @@ func (c *config) watch(w Watcher) {
kvs, err := w.Next()
if err != nil {
time.Sleep(time.Second)
c.log.Errorf("Failed to watch next config: %v", err)
c.log.Errorf("failed to watch next config: %v", err)
continue
}
if err := c.reader.Merge(kvs...); err != nil {
c.log.Errorf("Failed to merge next config: %v", err)
c.log.Errorf("failed to merge next config: %v", err)
continue
}
if err := c.reader.Resolve(); err != nil {
c.log.Errorf("failed to resolve next config: %v", err)
continue
}
c.cached.Range(func(key, value interface{}) bool {
@ -95,16 +99,20 @@ func (c *config) Load() error {
return err
}
if err := c.reader.Merge(kvs...); err != nil {
c.log.Errorf("Failed to merge config source: %v", err)
c.log.Errorf("failed to merge config source: %v", err)
return err
}
w, err := src.Watch()
if err != nil {
c.log.Errorf("Failed to watch config source: %v", err)
c.log.Errorf("failed to watch config source: %v", err)
return err
}
go c.watch(w)
}
if err := c.reader.Resolve(); err != nil {
c.log.Errorf("failed to resolve config source: %v", err)
return err
}
return nil
}

@ -58,8 +58,8 @@ func TestEnvWithPrefix(t *testing.T) {
}
c := config.New(config.WithSource(
NewSource(prefix1, prefix2),
file.NewSource(path),
NewSource(prefix1, prefix2),
))
if err := c.Load(); err != nil {

@ -17,6 +17,7 @@ type Reader interface {
Merge(...*KeyValue) error
Value(string) (Value, bool)
Source() ([]byte, error)
Resolve() error
}
type reader struct {
@ -45,9 +46,6 @@ func (r *reader) Merge(kvs ...*KeyValue) error {
return err
}
}
if err := r.opts.resolver(merged); err != nil {
return err
}
r.values = merged
return nil
}
@ -60,6 +58,10 @@ func (r *reader) Source() ([]byte, error) {
return marshalJSON(convertMap(r.values))
}
func (r *reader) Resolve() error {
return r.opts.resolver(r.values)
}
func cloneMap(src map[string]interface{}) (map[string]interface{}, error) {
// https://gist.github.com/soroushjp/0ec92102641ddfc3ad5515ca76405f4d
var buf bytes.Buffer

Loading…
Cancel
Save