Skip to content

Commit

Permalink
Enhance config API, support initialization by config parser
Browse files Browse the repository at this point in the history
  • Loading branch information
louyuting committed Oct 19, 2020
1 parent 0f7c726 commit 873beb4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
21 changes: 17 additions & 4 deletions api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/alibaba/sentinel-golang/core/log/metric"
"github.com/alibaba/sentinel-golang/core/system"
"github.com/alibaba/sentinel-golang/util"
"github.com/pkg/errors"
)

// Initialization func initialize the Sentinel's runtime environment, including:
Expand All @@ -19,6 +20,19 @@ func InitDefault() error {
return initSentinel("")
}

// InitWithParser initializes Sentinel using given config parser
// parser deserializes the configBytes and return config.Entity
func InitWithParser(configBytes []byte, parser func([]byte) (*config.Entity, error)) (err error) {
if parser == nil {
return errors.New("nil parser")
}
confEntity, err := parser(configBytes)
if err != nil {
return err
}
return InitWithConfig(confEntity)
}

// InitWithConfig initializes Sentinel using given config.
func InitWithConfig(confEntity *config.Entity) (err error) {
defer func() {
Expand All @@ -35,7 +49,7 @@ func InitWithConfig(confEntity *config.Entity) (err error) {
if err != nil {
return err
}
config.SetDefaultConfig(confEntity)
config.ResetGlobalConfig(confEntity)
if err = config.OverrideConfigFromEnvAndInitLog(); err != nil {
return err
}
Expand All @@ -48,8 +62,7 @@ func InitWithConfigFile(configPath string) error {
return initSentinel(configPath)
}

// initCoreComponents init core components with default config
// it's better SetDefaultConfig before initCoreComponents
// initCoreComponents init core components with global config
func initCoreComponents() error {
if config.MetricLogFlushIntervalSec() > 0 {
if err := metric.InitTask(); err != nil {
Expand Down Expand Up @@ -79,7 +92,7 @@ func initSentinel(configPath string) (err error) {
}
}()
// Initialize general config and logging module.
if err = config.InitConfig(configPath); err != nil {
if err = config.InitConfigWithYaml(configPath); err != nil {
return err
}
return initCoreComponents()
Expand Down
26 changes: 16 additions & 10 deletions core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@ var (
initLogOnce sync.Once
)

func SetDefaultConfig(config *Entity) {
func ResetGlobalConfig(config *Entity) {
globalCfg = config
}

// InitConfig loads general configuration from the given file.
func InitConfig(configPath string) error {
func InitConfigWithYaml(filePath string) (err error) {
// Initialize general config and logging module.
if err = applyYamlConfigFile(filePath); err != nil {
return err
}
if err = OverrideConfigFromEnvAndInitLog(); err != nil {
return err
}
return nil
}

// applyYamlConfigFile loads general configuration from the given YAML file.
func applyYamlConfigFile(configPath string) error {
// Priority: system environment > YAML file > default config
if util.IsBlank(configPath) {
// If the config file path is absent, Sentinel will try to resolve it from the system env.
Expand All @@ -34,12 +45,7 @@ func InitConfig(configPath string) error {
}
// First Sentinel will try to load config from the given file.
// If the path is empty (not set), Sentinel will use the default config.
err := loadFromYamlFile(configPath)
if err != nil {
return err
}

return OverrideConfigFromEnvAndInitLog()
return loadGlobalConfigFromYamlFile(configPath)
}

func OverrideConfigFromEnvAndInitLog() error {
Expand Down Expand Up @@ -71,7 +77,7 @@ func OverrideConfigFromEnvAndInitLog() error {
return nil
}

func loadFromYamlFile(filePath string) error {
func loadGlobalConfigFromYamlFile(filePath string) error {
if filePath == DefaultConfigFilename {
if _, err := os.Stat(DefaultConfigFilename); err != nil {
//use default globalCfg.
Expand Down
6 changes: 3 additions & 3 deletions core/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestLoadFromYamlFile(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := loadFromYamlFile(tt.args.filePath); (err != nil) != tt.wantErr {
t.Errorf("loadFromYamlFile() error = %v, wantErr %v", err, tt.wantErr)
if err := loadGlobalConfigFromYamlFile(tt.args.filePath); (err != nil) != tt.wantErr {
t.Errorf("loadGlobalConfigFromYamlFile() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
Expand All @@ -50,7 +50,7 @@ func TestOverrideFromSystemEnv(t *testing.T) {
wantErr: false,
},
}
err := loadFromYamlFile(testDataBaseDir + "sentinel.yml")
err := loadGlobalConfigFromYamlFile(testDataBaseDir + "sentinel.yml")
if err != nil {
t.Errorf("Fail to initialize data.")
}
Expand Down

0 comments on commit 873beb4

Please sign in to comment.