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 14, 2020
1 parent d36315f commit ded1e3d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
25 changes: 21 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,8 @@ 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
// it's better ResetGlobalConfig before initCoreComponents
func initCoreComponents() error {
if config.MetricLogFlushIntervalSec() > 0 {
if err := metric.InitTask(); err != nil {
Expand Down Expand Up @@ -79,7 +93,10 @@ func initSentinel(configPath string) (err error) {
}
}()
// Initialize general config and logging module.
if err = config.InitConfig(configPath); err != nil {
if err = config.ApplyConfigFile(configPath); err != nil {
return err
}
if err = config.OverrideConfigFromEnvAndInitLog(); err != nil {
return err
}
return initCoreComponents()
Expand Down
13 changes: 4 additions & 9 deletions core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ 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 {
// ApplyConfigFile loads general configuration from the given file.
func ApplyConfigFile(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 +34,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 loadFromYamlFile(configPath)
}

func OverrideConfigFromEnvAndInitLog() error {
Expand Down

0 comments on commit ded1e3d

Please sign in to comment.