Skip to content

Commit a9496ed

Browse files
authored
Enhance config API and support initialization with config parser (#290)
1 parent 0f7c726 commit a9496ed

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

api/init.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/alibaba/sentinel-golang/core/log/metric"
88
"github.com/alibaba/sentinel-golang/core/system"
99
"github.com/alibaba/sentinel-golang/util"
10+
"github.com/pkg/errors"
1011
)
1112

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

23+
// InitWithParser initializes Sentinel using given config parser
24+
// parser deserializes the configBytes and return config.Entity
25+
func InitWithParser(configBytes []byte, parser func([]byte) (*config.Entity, error)) (err error) {
26+
if parser == nil {
27+
return errors.New("nil parser")
28+
}
29+
confEntity, err := parser(configBytes)
30+
if err != nil {
31+
return err
32+
}
33+
return InitWithConfig(confEntity)
34+
}
35+
2236
// InitWithConfig initializes Sentinel using given config.
2337
func InitWithConfig(confEntity *config.Entity) (err error) {
2438
defer func() {
@@ -35,7 +49,7 @@ func InitWithConfig(confEntity *config.Entity) (err error) {
3549
if err != nil {
3650
return err
3751
}
38-
config.SetDefaultConfig(confEntity)
52+
config.ResetGlobalConfig(confEntity)
3953
if err = config.OverrideConfigFromEnvAndInitLog(); err != nil {
4054
return err
4155
}
@@ -48,8 +62,7 @@ func InitWithConfigFile(configPath string) error {
4862
return initSentinel(configPath)
4963
}
5064

51-
// initCoreComponents init core components with default config
52-
// it's better SetDefaultConfig before initCoreComponents
65+
// initCoreComponents init core components with global config
5366
func initCoreComponents() error {
5467
if config.MetricLogFlushIntervalSec() > 0 {
5568
if err := metric.InitTask(); err != nil {
@@ -79,7 +92,7 @@ func initSentinel(configPath string) (err error) {
7992
}
8093
}()
8194
// Initialize general config and logging module.
82-
if err = config.InitConfig(configPath); err != nil {
95+
if err = config.InitConfigWithYaml(configPath); err != nil {
8396
return err
8497
}
8598
return initCoreComponents()

core/config/config.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ var (
1818
initLogOnce sync.Once
1919
)
2020

21-
func SetDefaultConfig(config *Entity) {
21+
func ResetGlobalConfig(config *Entity) {
2222
globalCfg = config
2323
}
2424

25-
// InitConfig loads general configuration from the given file.
26-
func InitConfig(configPath string) error {
25+
func InitConfigWithYaml(filePath string) (err error) {
26+
// Initialize general config and logging module.
27+
if err = applyYamlConfigFile(filePath); err != nil {
28+
return err
29+
}
30+
return OverrideConfigFromEnvAndInitLog()
31+
}
32+
33+
// applyYamlConfigFile loads general configuration from the given YAML file.
34+
func applyYamlConfigFile(configPath string) error {
2735
// Priority: system environment > YAML file > default config
2836
if util.IsBlank(configPath) {
2937
// 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 {
3442
}
3543
// First Sentinel will try to load config from the given file.
3644
// If the path is empty (not set), Sentinel will use the default config.
37-
err := loadFromYamlFile(configPath)
38-
if err != nil {
39-
return err
40-
}
41-
42-
return OverrideConfigFromEnvAndInitLog()
45+
return loadGlobalConfigFromYamlFile(configPath)
4346
}
4447

4548
func OverrideConfigFromEnvAndInitLog() error {
@@ -71,7 +74,7 @@ func OverrideConfigFromEnvAndInitLog() error {
7174
return nil
7275
}
7376

74-
func loadFromYamlFile(filePath string) error {
77+
func loadGlobalConfigFromYamlFile(filePath string) error {
7578
if filePath == DefaultConfigFilename {
7679
if _, err := os.Stat(DefaultConfigFilename); err != nil {
7780
//use default globalCfg.

core/config/config_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ func TestLoadFromYamlFile(t *testing.T) {
3333
}
3434
for _, tt := range tests {
3535
t.Run(tt.name, func(t *testing.T) {
36-
if err := loadFromYamlFile(tt.args.filePath); (err != nil) != tt.wantErr {
37-
t.Errorf("loadFromYamlFile() error = %v, wantErr %v", err, tt.wantErr)
36+
if err := loadGlobalConfigFromYamlFile(tt.args.filePath); (err != nil) != tt.wantErr {
37+
t.Errorf("loadGlobalConfigFromYamlFile() error = %v, wantErr %v", err, tt.wantErr)
3838
}
3939
})
4040
}
@@ -50,7 +50,7 @@ func TestOverrideFromSystemEnv(t *testing.T) {
5050
wantErr: false,
5151
},
5252
}
53-
err := loadFromYamlFile(testDataBaseDir + "sentinel.yml")
53+
err := loadGlobalConfigFromYamlFile(testDataBaseDir + "sentinel.yml")
5454
if err != nil {
5555
t.Errorf("Fail to initialize data.")
5656
}

0 commit comments

Comments
 (0)