Skip to content

Commit

Permalink
fix: fix config init race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
lzap authored and ezr-ondrej committed May 29, 2024
1 parent a10e9ae commit 7ab41f2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
33 changes: 21 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ type loggingConfig struct {
Region string `json:"region,omitempty"`
}

var Config *EdgeConfig
var config *EdgeConfig
var configMu = sync.Mutex{}

// DevConfigFile is a wrapper for local dev kafka edgeConfig
type DevConfigFile struct {
Expand Down Expand Up @@ -378,22 +379,30 @@ func CreateEdgeAPIConfig() (*EdgeConfig, error) {

// Init configuration for service
func Init() {
newConfig, err := CreateEdgeAPIConfig()
if err != nil {
return
}
Config = newConfig
_ = Get()
}

// Get returns an initialized EdgeConfig
func Get() *EdgeConfig {
if Config == nil {
var lock = &sync.Mutex{}
lock.Lock()
defer lock.Unlock()
Init()
configMu.Lock()
defer configMu.Unlock()

if config == nil {
var err error
config, err = CreateEdgeAPIConfig()
if err != nil {
panic(err)
}
}
return Config

return config
}

func cleanup() {
configMu.Lock()
defer configMu.Unlock()

config = nil
}

// GetConfigValues return all configuration values that may be used for logging
Expand Down
28 changes: 16 additions & 12 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
)

func TestInitializeApplicationConfig(t *testing.T) {
currentConfig := Config
currentConfig := config

Init()
assert.NotNil(t, Config)
assert.NotEqual(t, Config, currentConfig)
assert.NotNil(t, config)
assert.NotEqual(t, config, currentConfig)
}

func TestCreateNewConfig(t *testing.T) {
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestRedactPasswordFromURL(t *testing.T) {
}

func TestKafkaBroker(t *testing.T) {
originalConfig := Config
originalConfig := config
originalClowderEnvConfig := os.Getenv("ACG_CONFIG")
originalClowderLoadedConfig := clowder.LoadedConfig
originalClowderObjectBuckets := clowder.ObjectBuckets
Expand All @@ -76,7 +76,7 @@ func TestKafkaBroker(t *testing.T) {
// restore configs
defer func(conf *EdgeConfig, clowderEnvConfig string, clowderLoadedConfig *clowder.AppConfig,
clowderObjectBuckets map[string]clowder.ObjectStoreBucket, originalBucketName string) {
Config = conf
config = conf
clowder.LoadedConfig = clowderLoadedConfig
clowder.ObjectBuckets = clowderObjectBuckets
if clowderEnvConfig == "" {
Expand All @@ -90,6 +90,8 @@ func TestKafkaBroker(t *testing.T) {

}(originalConfig, originalClowderEnvConfig, originalClowderLoadedConfig, originalClowderObjectBuckets, originalEDGETarBallsBucket)

defer cleanup()

err := os.Setenv("ACG_CONFIG", "need some value only, as the config path is not needed here")
assert.NoError(t, err)

Expand Down Expand Up @@ -168,21 +170,23 @@ func TestKafkaBroker(t *testing.T) {
clowder.LoadedConfig = testCase.clowderConfig
clowder.KafkaServers = testCase.KafkaServers
// init the configuration
cleanup()
Init()
assert.Equal(t, Config.KafkaBroker, testCase.ExpectedKafkaBroker)
assert.Equal(t, Config.KafkaServers, testCase.ExpectedKafkaServers)
defer cleanup()
assert.Equal(t, config.KafkaBroker, testCase.ExpectedKafkaBroker)
assert.Equal(t, config.KafkaServers, testCase.ExpectedKafkaServers)
if testCase.ExpectedKafkaBroker != nil {
if testCase.ExpectedKafkaBroker.Cacert != nil && *testCase.ExpectedKafkaBroker.Cacert != "" {
assert.NotEmpty(t, Config.KafkaBrokerCaCertPath)
caCertByteContent, err := os.ReadFile(Config.KafkaBrokerCaCertPath)
assert.NotEmpty(t, config.KafkaBrokerCaCertPath)
caCertByteContent, err := os.ReadFile(config.KafkaBrokerCaCertPath)
assert.NoError(t, err)
assert.Equal(t, string(caCertByteContent), *kafkaBroker.Cacert)
} else {
assert.Empty(t, Config.KafkaBrokerCaCertPath)
assert.Empty(t, config.KafkaBrokerCaCertPath)
}
} else {
assert.Nil(t, Config.KafkaBroker)
assert.Empty(t, Config.KafkaBrokerCaCertPath)
assert.Nil(t, config.KafkaBroker)
assert.Empty(t, config.KafkaBrokerCaCertPath)
}
})
}
Expand Down

0 comments on commit 7ab41f2

Please sign in to comment.