Skip to content

Commit

Permalink
add mem, cli, env init options, do not load them by default (#61)
Browse files Browse the repository at this point in the history
add new method SetPriority to let user custom
  • Loading branch information
tianxiaoliang authored Mar 11, 2019
1 parent ea6fdfa commit cb6394a
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 54 deletions.
28 changes: 21 additions & 7 deletions archaius.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (

"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"
"github.com/go-chassis/go-archaius/sources/enviromentvariable-source"
"github.com/go-chassis/go-archaius/sources/file-source"
"github.com/go-chassis/go-archaius/sources/memory-source"
"github.com/go-chassis/go-cc-client"
Expand All @@ -23,7 +25,6 @@ var (

once = sync.Once{}
onceConfigCenter = sync.Once{}
onceExternal = sync.Once{}
)

func initFileSource(o *Options) (core.ConfigSource, error) {
Expand Down Expand Up @@ -89,6 +90,21 @@ func Init(opts ...Option) error {
errG = err
return
}

// 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)
}

eventHandler := EventListener{
Name: "EventHandler",
Factory: factory,
Expand All @@ -101,14 +117,12 @@ func Init(opts ...Option) error {
return errG
}

//Mock accept only one custom config source, add it into archaius runtime.
//it almost like Init(), but will not load any config sources you give
//it is used in UT or AT scenario
func Mock(sources ...core.ConfigSource) error {
//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
onceExternal.Do(func() {
once.Do(func() {
var err error

factory, err = NewConfigFactory()
if err != nil {
errG = err
Expand Down
18 changes: 4 additions & 14 deletions configurationfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ import (
"github.com/go-chassis/go-archaius/core/cast"
"github.com/go-chassis/go-archaius/core/config-manager"
"github.com/go-chassis/go-archaius/core/event-system"
"github.com/go-chassis/go-archaius/sources/commandline-source"
"github.com/go-chassis/go-archaius/sources/enviromentvariable-source"
"github.com/go-chassis/go-archaius/sources/memory-source"
"github.com/go-mesh/openlogging"
)

Expand Down Expand Up @@ -90,17 +87,6 @@ func NewConfigFactory() (ConfigurationFactory, error) {
arc.dispatcher = eventsystem.NewDispatcher()
arc.configMgr = configmanager.NewConfigurationManager(arc.dispatcher)

// Default config source init
// 1. Command line source
cmdSource := commandlinesource.NewCommandlineConfigSource()
arc.configMgr.AddSource(cmdSource, cmdSource.GetPriority())

// Environment variable source
envSource := envconfigsource.NewEnvConfigurationSource()
arc.configMgr.AddSource(envSource, envSource.GetPriority())
// External variable source
ms = memoryconfigsource.NewMemoryConfigurationSource()
arc.configMgr.AddSource(ms, ms.GetPriority())
openlogging.GetLogger().Debug("ConfigurationFactory Initiated")
return arc, nil
}
Expand Down Expand Up @@ -168,7 +154,11 @@ func (arc *ConfigFactory) GetConfigurationByKeyAndDimensionInfo(dimensionInfo, k

// AddSource return all values of different sources
func (arc *ConfigFactory) AddSource(source core.ConfigSource) error {
if source == nil {
return errors.New("source can not be nil")
}
if arc.initSuccess == false {
openlogging.Warn("Plz call factory.Init() first, failed to add source: " + source.GetSourceName())
return nil
}

Expand Down
42 changes: 29 additions & 13 deletions configurationfactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"fmt"
"github.com/go-chassis/go-archaius"
"github.com/go-chassis/go-archaius/core"
"github.com/go-chassis/go-archaius/sources/commandline-source"
"github.com/go-chassis/go-archaius/sources/enviromentvariable-source"
"github.com/go-chassis/go-archaius/sources/file-source"
"github.com/go-chassis/go-archaius/sources/memory-source"
"github.com/go-chassis/go-archaius/sources/test-source"
"github.com/go-mesh/openlogging"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -55,24 +58,35 @@ func TestConfigFactory(t *testing.T) {

_, err1 = io.WriteString(f1, f1content)
populateCmdConfig()
fmt.Println("init factory")
t.Log("init factory")
factory, err := archaius.NewConfigFactory()
assert.Equal(t, nil, err)
fmt.Println("verifying methods before config factory initialization")
assert.NoError(t, err)
factory.Init()
// build-in config sources
ms := memoryconfigsource.NewMemoryConfigurationSource()
err = factory.AddSource(ms)
assert.NoError(t, err)
cmdSource := commandlinesource.NewCommandlineConfigSource()
err = factory.AddSource(cmdSource)
assert.NoError(t, err)
envSource := envconfigsource.NewEnvConfigurationSource()
err = factory.AddSource(envSource)
assert.NoError(t, err)

t.Log("verifying methods before config factory initialization")
factory.DeInit()
fmt.Println(factory.GetValue("testkey"))
t.Log(factory.GetValue("testkey"))

assert.Equal(t, nil, factory.GetValue("testkey"))
assert.Equal(t, nil, factory.AddSource(nil))
assert.Error(t, factory.AddSource(nil))
assert.Equal(t, map[string]interface{}(map[string]interface{}(nil)), factory.GetConfigurations())
assert.Equal(t, false, factory.IsKeyExist("testkey"))
assert.Equal(t, nil, factory.Unmarshal("testkey"))
assert.Equal(t, nil, factory.GetConfigurationByKey("testkey"))
assert.Equal(t, nil, factory.AddSource(nil))
assert.Equal(t, nil, factory.GetConfigurationByKeyAndDimensionInfo("data@default#0.1", "hello"))
fmt.Println("DeInit")
t.Log("DeInit")
factory.DeInit()
fmt.Println("Init")
t.Log("Init")
factory.Init()
defer factory.DeInit()

Expand Down Expand Up @@ -101,11 +115,12 @@ func TestConfigFactory(t *testing.T) {
fmt.Println("Adding filesource to the configfactroy")
fsource := filesource.NewFileSource()
fsource.AddFile(filename1, 0, nil)
factory.AddSource(fsource)
err = factory.AddSource(fsource)
assert.NoError(t, err)

fmt.Println("Generating event through testsource(priority 1)")
fmt.Println("Generating event through testsource(priority 1)")
archaius.AddKeyValue("commonkey", "memsource1")
ms.AddKeyValue("commonkey", "memsource1")

fmt.Println("verifying the key of lower priority source")
time.Sleep(10 * time.Millisecond)
Expand All @@ -117,7 +132,8 @@ func TestConfigFactory(t *testing.T) {
fmt.Println("Adding testsource to the configfactory")
testConfig := map[string]interface{}{"aaa": "111", "bbb": "222", "commonkey": "testsource1"}
testSource := testsource.NewTestSource(testConfig)
factory.AddSource(testSource)
err = factory.AddSource(testSource)
assert.NoError(t, err)
defer testsource.CleanupTestSource()

fmt.Println("verifying common configuration keyvalue pair ")
Expand All @@ -144,14 +160,14 @@ func TestConfigFactory(t *testing.T) {
}

fmt.Println("verifying memsource configurations and accessing in different data type formats")
archaius.AddKeyValue("stringkey", "true")
ms.AddKeyValue("stringkey", "true")
time.Sleep(10 * time.Millisecond)
configvalue2, err := factory.GetValue("stringkey").ToBool()
if err != nil || configvalue2 != true {
t.Error("failed to get the value in bool")
}

archaius.AddKeyValue("boolkey", "hello")
ms.AddKeyValue("boolkey", "hello")
time.Sleep(10 * time.Millisecond)
configvalue3, err := factory.GetValue("boolkey").ToBool()
if err != nil || configvalue3 != false {
Expand Down
9 changes: 5 additions & 4 deletions core/config-manager/configurationmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"reflect"
"sync"

"fmt"
"github.com/go-chassis/go-archaius/core"
"github.com/go-chassis/go-archaius/sources/file-source"
"github.com/go-mesh/openlogging"
Expand Down Expand Up @@ -109,11 +110,11 @@ func (configMgr *ConfigurationManager) AddSource(source core.ConfigSource, prior

err := configMgr.pullSourceConfigs(sourceName)
if err != nil {
openlogging.GetLogger().Errorf("fail to load configuration of %s source: %s", sourceName, err)
errorMsg := "fail to load configuration of " + sourceName + " source"
return errors.New(errorMsg)
err = fmt.Errorf("fail to load configuration of %s source: %s", sourceName, err)
openlogging.Error(err.Error())
return err
}

openlogging.Info("invoke dynamic handler:" + source.GetSourceName())
go source.DynamicConfigHandler(configMgr)

return nil
Expand Down
1 change: 1 addition & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type ConfigSource interface {
AddDimensionInfo(dimensionInfo string) (map[string]string, error)
DynamicConfigHandler(DynamicConfigCallback) error
GetPriority() int
SetPriority(priority int)
Cleanup() error
}

Expand Down
3 changes: 0 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ func WithConfigCenterSource(cci ConfigCenterInfo, ccc ccclient.ConfigClient) Opt

//WithCommandLineSource enable cmd line source
//archaius will read command line params as key value
//TODO make option work
func WithCommandLineSource() Option {
return func(options *Options) {
options.UseCLISource = true
Expand All @@ -96,15 +95,13 @@ func WithCommandLineSource() Option {

//WithENVSource enable env source
//archaius will read ENV as key value
//TODO make option work
func WithENVSource() Option {
return func(options *Options) {
options.UseENVSource = true
}
}

//WithMemorySource accept the information for initiating a Memory source
//TODO make option work
func WithMemorySource() Option {
return func(options *Options) {
options.UseMemSource = true
Expand Down
11 changes: 9 additions & 2 deletions sources/commandline-source/commandlineconfigurationsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ var _ core.ConfigSource = &CommandLineConfigurationSource{}
type CommandLineConfigurationSource struct {
Configurations map[string]interface{}
sync.RWMutex
priority int
}

//NewCommandlineConfigSource defines a fucntion used for creating configuration source
func NewCommandlineConfigSource() core.ConfigSource {
cmdlineConfig := new(CommandLineConfigurationSource)
cmdlineConfig.priority = commandlinePriority
config, err := cmdlineConfig.pullCmdLineConfig()
if err != nil {
openlogging.GetLogger().Error("failed to initialize commandline configurations:" + err.Error())
Expand Down Expand Up @@ -99,8 +101,13 @@ func (confSrc *CommandLineConfigurationSource) GetConfigurationByKey(key string)
}

//GetPriority gets the priority of a configuration
func (*CommandLineConfigurationSource) GetPriority() int {
return commandlinePriority
func (confSrc *CommandLineConfigurationSource) GetPriority() int {
return confSrc.priority
}

//SetPriority custom priority
func (confSrc *CommandLineConfigurationSource) SetPriority(priority int) {
confSrc.priority = priority
}

//GetSourceName gets the source's name of a configuration
Expand Down
11 changes: 9 additions & 2 deletions sources/configcenter/configcentersource.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Handler struct {
sync.RWMutex
RefreshMode int
RefreshInterval time.Duration
priority int
}

//ConfigCenterConfig is pointer of config center source
Expand All @@ -74,6 +75,7 @@ func NewConfigCenterSource(cc ccclient.ConfigClient, dimInfo string,

if ConfigCenterConfig == nil {
ConfigCenterConfig = new(Handler)
ConfigCenterConfig.priority = configCenterSourcePriority
ConfigCenterConfig.cc = cc
ConfigCenterConfig.dimensionsInfo = dimInfo
ConfigCenterConfig.initSuccess = true
Expand Down Expand Up @@ -297,8 +299,13 @@ func (*Handler) GetSourceName() string {
}

//GetPriority returns priority of a configuration
func (*Handler) GetPriority() int {
return configCenterSourcePriority
func (cfgSrcHandler *Handler) GetPriority() int {
return cfgSrcHandler.priority
}

//SetPriority custom priority
func (cfgSrcHandler *Handler) SetPriority(priority int) {
cfgSrcHandler.priority = priority
}

//DynamicConfigHandler dynamically handles a configuration
Expand Down
11 changes: 9 additions & 2 deletions sources/enviromentvariable-source/envconfigurationsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ const (
type EnvConfigurationSource struct {
Configurations map[string]interface{}
sync.RWMutex
priority int
}

var _ core.ConfigSource = &EnvConfigurationSource{}

//NewEnvConfigurationSource configures a new environment configuration
func NewEnvConfigurationSource() core.ConfigSource {
envConfigSource := new(EnvConfigurationSource)
envConfigSource.priority = envVariableSourcePriority
config, err := envConfigSource.pullConfigurations()
if err != nil {
openlogging.GetLogger().Error("failed to initialize environment configurations: " + err.Error())
Expand Down Expand Up @@ -91,8 +93,13 @@ func (confSrc *EnvConfigurationSource) GetConfigurationByKey(key string) (interf
}

//GetPriority returns priority of environment configuration
func (*EnvConfigurationSource) GetPriority() int {
return envVariableSourcePriority
func (confSrc *EnvConfigurationSource) GetPriority() int {
return confSrc.priority
}

//SetPriority custom priority
func (confSrc *EnvConfigurationSource) SetPriority(priority int) {
confSrc.priority = priority
}

//GetSourceName returns the name of environment source
Expand Down
10 changes: 8 additions & 2 deletions sources/file-source/filesource.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type fileSource struct {
fileHandlers map[string]FileHandler
watchPool *watch
filelock sync.Mutex
priority int
sync.RWMutex
}

Expand Down Expand Up @@ -105,6 +106,7 @@ type FileSource interface {
func NewFileSource() FileSource {
if fileConfigSource == nil {
fileConfigSource = new(fileSource)
fileConfigSource.priority = fileSourcePriority
fileConfigSource.files = make([]file, 0)
fileConfigSource.fileHandlers = make(map[string]FileHandler)
}
Expand Down Expand Up @@ -311,10 +313,14 @@ func (*fileSource) GetSourceName() string {
return FileConfigSourceConst
}

func (*fileSource) GetPriority() int {
return fileSourcePriority
func (fSource *fileSource) GetPriority() int {
return fSource.priority
}

//SetPriority custom priority
func (fSource *fileSource) SetPriority(priority int) {
fSource.priority = priority
}
func (fSource *fileSource) DynamicConfigHandler(callback core.DynamicConfigCallback) error {
if callback == nil {
return errors.New("call back can not be nil")
Expand Down
Loading

0 comments on commit cb6394a

Please sign in to comment.