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

Enhance config API, support initialization by config parser #290

Merged
merged 2 commits into from
Oct 19, 2020
Merged
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
21 changes: 17 additions & 4 deletions api/init.go
Original file line number Diff line number Diff line change
@@ -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:
@@ -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() {
@@ -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
}
@@ -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 {
@@ -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()
23 changes: 13 additions & 10 deletions core/config/config.go
Original file line number Diff line number Diff line change
@@ -18,12 +18,20 @@ 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
}
return OverrideConfigFromEnvAndInitLog()
}

// 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.
@@ -34,12 +42,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 {
@@ -71,7 +74,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.
6 changes: 3 additions & 3 deletions core/config/config_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
@@ -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.")
}