Fix race condition in AppConfig

This commit is contained in:
Adphi 2018-10-16 18:16:18 +02:00
parent 712c7a008a
commit 7772f3b68d
2 changed files with 15 additions and 11 deletions

View File

@ -21,12 +21,12 @@ unit_tests:
script: script:
- make test - make test
#race_detector: race_detector:
# stage: test stage: test
# tags: tags:
# - docker - docker
# script: script:
# - make race - make race
code_coverage: code_coverage:
stage: test stage: test

View File

@ -59,8 +59,8 @@ func (c *Client) AppsConfigDeleteValue(id, key, value string) error {
//AppsConfig returns all apps AppConfigDetails //AppsConfig returns all apps AppConfigDetails
func (c *Client) AppsConfig() (map[string]map[string]string, error) { func (c *Client) AppsConfig() (map[string]map[string]string, error) {
config := make(map[string]map[string]string) config := map[string]map[string]string{}
var err error m := sync.Mutex{}
appsIDs, err := c.AppsConfigList() appsIDs, err := c.AppsConfigList()
if err != nil { if err != nil {
return nil, err return nil, err
@ -72,7 +72,9 @@ func (c *Client) AppsConfig() (map[string]map[string]string, error) {
defer wg.Done() defer wg.Done()
d, err := c.AppsConfigDetails(id) d, err := c.AppsConfigDetails(id)
if err == nil { if err == nil {
m.Lock()
config[id] = d config[id] = d
m.Unlock()
} }
}(appsIDs[i]) }(appsIDs[i])
} }
@ -82,7 +84,8 @@ func (c *Client) AppsConfig() (map[string]map[string]string, error) {
//AppsConfigDetails returns all the config's key, values pair of the app //AppsConfigDetails returns all the config's key, values pair of the app
func (c *Client) AppsConfigDetails(appID string) (map[string]string, error) { func (c *Client) AppsConfigDetails(appID string) (map[string]string, error) {
config := make(map[string]string) config := map[string]string{}
m := sync.Mutex{}
var err error var err error
var ks []string var ks []string
ks, err = c.AppsConfigKeys(appID) ks, err = c.AppsConfigKeys(appID)
@ -94,10 +97,11 @@ func (c *Client) AppsConfigDetails(appID string) (map[string]string, error) {
for i := range ks { for i := range ks {
go func(key string) { go func(key string) {
defer wg.Done() defer wg.Done()
var v string v, err := c.AppsConfigValue(appID, key)
v, err = c.AppsConfigValue(appID, key)
if err == nil { if err == nil {
m.Lock()
config[key] = v config[key] = v
m.Unlock()
} }
}(ks[i]) }(ks[i])
} }