Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug, can not re-init #67

Merged
merged 1 commit into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 110 additions & 113 deletions archaius.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions archaius_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}