From 36c4909c10bbd8254f7eced59bcc2da1992849f5 Mon Sep 17 00:00:00 2001 From: tian Date: Fri, 26 Apr 2019 11:19:09 +0800 Subject: [PATCH] fix bug, can not re-init --- archaius.go | 223 +++++++++++++++++++++++------------------------ archaius_test.go | 5 ++ 2 files changed, 115 insertions(+), 113 deletions(-) diff --git a/archaius.go b/archaius.go index a1e2883c..c4ebe31f 100755 --- a/archaius.go +++ b/archaius.go @@ -3,11 +3,10 @@ package archaius import ( + "errors" "os" "strings" - "sync" - "errors" "github.com/go-chassis/go-archaius/core" "github.com/go-chassis/go-archaius/sources/commandline-source" "github.com/go-chassis/go-archaius/sources/configcenter" @@ -23,8 +22,8 @@ var ( fs filesource.FileSource ms = memoryconfigsource.NewMemoryConfigurationSource() - once = sync.Once{} - onceConfigCenter = sync.Once{} + running = false + configServerRunning = false ) func initFileSource(o *Options) (core.ConfigSource, error) { @@ -57,145 +56,137 @@ func initFileSource(o *Options) (core.ConfigSource, error) { // Init create a Archaius config singleton func Init(opts ...Option) error { - var errG error - once.Do(func() { - var err error - o := &Options{} - for _, opt := range opts { - opt(o) - } + if running { + openlogging.Debug("can not init archaius again, call Clean first") + return nil + } + var err error + o := &Options{} + for _, opt := range opts { + opt(o) + } - // created config factory object - factory, err = NewConfigFactory() - if err != nil { - errG = err - return - } - factory.DeInit() - factory.Init() + // created config factory object + factory, err = NewConfigFactory() + if err != nil { - fs, err := initFileSource(o) - if err != nil { - errG = err - return - } - if o.ConfigCenterInfo != (ConfigCenterInfo{}) { - if err := EnableConfigCenterSource(o.ConfigCenterInfo, o.ConfigClient); err != nil { - errG = err - return - } - } - err = factory.AddSource(fs) - if err != nil { - errG = err - return - } + return err + } + factory.DeInit() + factory.Init() - // build-in config sources - if o.UseMemSource { - ms = memoryconfigsource.NewMemoryConfigurationSource() - factory.AddSource(ms) - } - if o.UseCLISource { - cmdSource := commandlinesource.NewCommandlineConfigSource() - factory.AddSource(cmdSource) - } - if o.UseENVSource { - envSource := envconfigsource.NewEnvConfigurationSource() - factory.AddSource(envSource) + fs, err := initFileSource(o) + if err != nil { + return err + } + if o.ConfigCenterInfo != (ConfigCenterInfo{}) { + if err := EnableConfigCenterSource(o.ConfigCenterInfo, o.ConfigClient); err != nil { + return err } + } + err = factory.AddSource(fs) + if err != nil { + return err + } - eventHandler := EventListener{ - Name: "EventHandler", - Factory: factory, - } + // build-in config sources + if o.UseMemSource { + ms = memoryconfigsource.NewMemoryConfigurationSource() + factory.AddSource(ms) + } + if o.UseCLISource { + cmdSource := commandlinesource.NewCommandlineConfigSource() + factory.AddSource(cmdSource) + } + if o.UseENVSource { + envSource := envconfigsource.NewEnvConfigurationSource() + factory.AddSource(envSource) + } - factory.RegisterListener(eventHandler, "a*") - openlogging.GetLogger().Info("archaius init success") - }) + eventHandler := EventListener{ + Name: "EventHandler", + Factory: factory, + } - return errG + factory.RegisterListener(eventHandler, "a*") + openlogging.GetLogger().Info("archaius init success") + running = true + return nil } //CustomInit accept is able to accept a list of config source, add it into archaius runtime. //it almost like Init(), but you can fully control config sources you inject to archaius func CustomInit(sources ...core.ConfigSource) error { - var errG error - once.Do(func() { - var err error - factory, err = NewConfigFactory() - if err != nil { - errG = err - return - } - - factory.DeInit() - factory.Init() - for _, s := range sources { - err = factory.AddSource(s) - if err != nil { - errG = err - return - } - } + var err error + factory, err = NewConfigFactory() + if err != nil { + return err + } - eventHandler := EventListener{ - Name: "EventHandler", - Factory: factory, + factory.DeInit() + factory.Init() + for _, s := range sources { + err = factory.AddSource(s) + if err != nil { + return err } + } - factory.RegisterListener(eventHandler, "a*") + eventHandler := EventListener{ + Name: "EventHandler", + Factory: factory, + } - }) + factory.RegisterListener(eventHandler, "a*") - return errG + return err } //EnableConfigCenterSource create a config center source singleton //A config center source pull remote config server key values into local memory //so that you can use GetXXX to get value easily func EnableConfigCenterSource(ci ConfigCenterInfo, cc config.Client) error { - var errG error if ci == (ConfigCenterInfo{}) { return errors.New("ConfigCenterInfo can not be empty") } - onceConfigCenter.Do(func() { - var err error - if cc == nil { - opts := config.Options{ - DimensionInfo: ci.DefaultDimensionInfo, - ServerURI: ci.URL, - TenantName: ci.TenantName, - EnableSSL: ci.EnableSSL, - TLSConfig: ci.TLSConfig, - RefreshPort: ci.RefreshPort, - AutoDiscovery: ci.AutoDiscovery, - Env: ci.Environment, - } - cc, err = config.NewClient(ci.ClientType, opts) - if err != nil { - errG = err - return - } + if configServerRunning { + openlogging.Debug("can not init config server again, call Clean first") + return nil + } + + var err error + if cc == nil { + opts := config.Options{ + DimensionInfo: ci.DefaultDimensionInfo, + ServerURI: ci.URL, + TenantName: ci.TenantName, + EnableSSL: ci.EnableSSL, + TLSConfig: ci.TLSConfig, + RefreshPort: ci.RefreshPort, + AutoDiscovery: ci.AutoDiscovery, + Env: ci.Environment, } - configCenterSource := configcenter.NewConfigCenterSource(cc, - ci.DefaultDimensionInfo, ci.RefreshMode, - ci.RefreshInterval) - err = factory.AddSource(configCenterSource) + cc, err = config.NewClient(ci.ClientType, opts) if err != nil { - errG = err - return - } - - eventHandler := EventListener{ - Name: "EventHandler", - Factory: factory, + return err } + } + configCenterSource := configcenter.NewConfigCenterSource(cc, + ci.DefaultDimensionInfo, ci.RefreshMode, + ci.RefreshInterval) + err = factory.AddSource(configCenterSource) + if err != nil { + return err + } - factory.RegisterListener(eventHandler, "a*") - }) + eventHandler := EventListener{ + Name: "EventHandler", + Factory: factory, + } - return errG + factory.RegisterListener(eventHandler, "a*") + configServerRunning = true + return nil } // EventListener is a struct having information about registering key and object @@ -333,5 +324,11 @@ func GetConfigFactory() ConfigurationFactory { //it deletes all sources which means all of key value is deleted. //after you call Clean, you can init archaius again func Clean() error { - return factory.DeInit() + err := factory.DeInit() + if err != nil { + return err + } + running = false + configServerRunning = false + return nil } diff --git a/archaius_test.go b/archaius_test.go index 74c1c681..77c406e7 100755 --- a/archaius_test.go +++ b/archaius_test.go @@ -89,4 +89,9 @@ func TestClean(t *testing.T) { assert.NoError(t, err) s := archaius.Get("age") assert.Equal(t, nil, s) + + err = archaius.Init( + archaius.WithOptionalFiles([]string{filename2})) + s = archaius.Get("addr") + assert.Equal(t, 14, s) }