Skip to content

Commit 46c277c

Browse files
Merge pull request #144 from jenxie/fix-getconfig-crash
fix: 'fatal error: concurrent map read and map write' crash
2 parents 962ad01 + 9714e6e commit 46c277c

1 file changed

Lines changed: 60 additions & 52 deletions

File tree

config/config.go

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"flag"
55
"fmt"
66
"log"
7+
"sync"
78

89
"github.com/sebastianrakel/openvoxview/model"
910
"github.com/spf13/viper"
@@ -58,59 +59,66 @@ func PrintVersion(version string) bool {
5859
return false
5960
}
6061

61-
func GetConfig() (*Config, error) {
62-
viper.SetConfigName("config")
63-
viper.SetConfigType("yaml")
64-
viper.AddConfigPath(".")
65-
66-
if configPath != nil {
67-
log.Printf("Using config: %s", *configPath)
68-
viper.SetConfigFile(*configPath)
69-
}
62+
var (
63+
cachedConfig *Config
64+
cachedErr error
65+
configOnce sync.Once
66+
)
7067

71-
viper.SetDefault("port", 5000)
72-
viper.SetDefault("puppetdb.host", "localhost")
73-
viper.SetDefault("puppetdb.port", 8080)
74-
viper.SetDefault("puppetdb.tls_ignore", false)
75-
viper.SetDefault("unreported_hours", 3)
76-
viper.SetDefault("puppetca.port", 8140)
77-
viper.SetDefault("puppetca.tls", true)
78-
viper.SetDefault("puppetca.tls_ignore", false)
79-
viper.SetDefault("puppetca.readonly", true)
80-
viper.SetDefault("puppetca.deactivate_nodes", false)
81-
82-
viper.AutomaticEnv()
83-
84-
viper.BindEnv("port", "PORT")
85-
viper.BindEnv("listen", "LISTEN")
86-
viper.BindEnv("trusted_proxies", "TRUSTED_PROXIES")
87-
viper.BindEnv("puppetdb.port", "PUPPETDB_PORT")
88-
viper.BindEnv("puppetdb.host", "PUPPETDB_HOST")
89-
viper.BindEnv("puppetdb.tls", "PUPPETDB_TLS")
90-
viper.BindEnv("puppetdb.tls_ignore", "PUPPETDB_TLS_IGNORE")
91-
viper.BindEnv("puppetdb.tls_ca", "PUPPETDB_TLS_CA")
92-
viper.BindEnv("puppetdb.tls_key", "PUPPETDB_TLS_KEY")
93-
viper.BindEnv("puppetdb.tls_cert", "PUPPETDB_TLS_CERT")
94-
viper.BindEnv("unreported_hours", "UNREPORTED_HOURS")
95-
viper.BindEnv("puppetca.host", "PUPPETCA_HOST")
96-
viper.BindEnv("puppetca.port", "PUPPETCA_PORT")
97-
viper.BindEnv("puppetca.tls", "PUPPETCA_TLS")
98-
viper.BindEnv("puppetca.tls_ignore", "PUPPETCA_TLS_IGNORE")
99-
viper.BindEnv("puppetca.tls_ca", "PUPPETCA_TLS_CA")
100-
viper.BindEnv("puppetca.tls_key", "PUPPETCA_TLS_KEY")
101-
viper.BindEnv("puppetca.tls_cert", "PUPPETCA_TLS_CERT")
102-
viper.BindEnv("puppetca.readonly", "PUPPETCA_READONLY")
103-
viper.BindEnv("puppetca.deactivate_nodes", "PUPPETCA_DEACTIVATE_NODES")
104-
105-
viper.ReadInConfig()
106-
107-
var cfg Config
108-
109-
err := viper.Unmarshal(&cfg)
110-
111-
cfg.TrustedProxies = viper.GetStringSlice("trusted_proxies")
112-
113-
return &cfg, err
68+
func GetConfig() (*Config, error) {
69+
configOnce.Do(func() {
70+
viper.SetConfigName("config")
71+
viper.SetConfigType("yaml")
72+
viper.AddConfigPath(".")
73+
74+
if configPath != nil {
75+
log.Printf("Using config: %s", *configPath)
76+
viper.SetConfigFile(*configPath)
77+
}
78+
79+
viper.SetDefault("port", 5000)
80+
viper.SetDefault("puppetdb.host", "localhost")
81+
viper.SetDefault("puppetdb.port", 8080)
82+
viper.SetDefault("puppetdb.tls_ignore", false)
83+
viper.SetDefault("unreported_hours", 3)
84+
viper.SetDefault("puppetca.port", 8140)
85+
viper.SetDefault("puppetca.tls", true)
86+
viper.SetDefault("puppetca.tls_ignore", false)
87+
viper.SetDefault("puppetca.readonly", true)
88+
viper.SetDefault("puppetca.deactivate_nodes", false)
89+
90+
viper.AutomaticEnv()
91+
92+
viper.BindEnv("port", "PORT")
93+
viper.BindEnv("listen", "LISTEN")
94+
viper.BindEnv("trusted_proxies", "TRUSTED_PROXIES")
95+
viper.BindEnv("puppetdb.port", "PUPPETDB_PORT")
96+
viper.BindEnv("puppetdb.host", "PUPPETDB_HOST")
97+
viper.BindEnv("puppetdb.tls", "PUPPETDB_TLS")
98+
viper.BindEnv("puppetdb.tls_ignore", "PUPPETDB_TLS_IGNORE")
99+
viper.BindEnv("puppetdb.tls_ca", "PUPPETDB_TLS_CA")
100+
viper.BindEnv("puppetdb.tls_key", "PUPPETDB_TLS_KEY")
101+
viper.BindEnv("puppetdb.tls_cert", "PUPPETDB_TLS_CERT")
102+
viper.BindEnv("unreported_hours", "UNREPORTED_HOURS")
103+
viper.BindEnv("puppetca.host", "PUPPETCA_HOST")
104+
viper.BindEnv("puppetca.port", "PUPPETCA_PORT")
105+
viper.BindEnv("puppetca.tls", "PUPPETCA_TLS")
106+
viper.BindEnv("puppetca.tls_ignore", "PUPPETCA_TLS_IGNORE")
107+
viper.BindEnv("puppetca.tls_ca", "PUPPETCA_TLS_CA")
108+
viper.BindEnv("puppetca.tls_key", "PUPPETCA_TLS_KEY")
109+
viper.BindEnv("puppetca.tls_cert", "PUPPETCA_TLS_CERT")
110+
viper.BindEnv("puppetca.readonly", "PUPPETCA_READONLY")
111+
viper.BindEnv("puppetca.deactivate_nodes", "PUPPETCA_DEACTIVATE_NODES")
112+
113+
viper.ReadInConfig()
114+
115+
var cfg Config
116+
cachedErr = viper.Unmarshal(&cfg)
117+
cfg.TrustedProxies = viper.GetStringSlice("trusted_proxies")
118+
cachedConfig = &cfg
119+
})
120+
121+
return cachedConfig, cachedErr
114122
}
115123

116124
func (c *Config) GetPuppetDbAddress() string {

0 commit comments

Comments
 (0)