optimistic writeback and pessimistic read

This commit is contained in:
2023-11-03 00:56:56 -07:00
parent 0fc1bf396d
commit 610b979966
3 changed files with 139 additions and 93 deletions

View File

@@ -10,7 +10,13 @@ import (
func (c *ConfigManager) GetBool(key string) bool {
c.mutex.RLock()
defer c.mutex.RUnlock()
if v, ok := c.combinedConfig[strings.ToLower(key)]; ok {
v, ok := c.combinedConfig[strings.ToLower(key)]
if !ok {
v, ok = c.envConfig[strings.ToLower(key)]
if !ok {
return false
}
}
val := v.Value
switch val := val.(type) {
case bool:
@@ -40,14 +46,18 @@ func (c *ConfigManager) GetBool(key string) bool {
default:
return val.(bool)
}
}
return false
}
func (c *ConfigManager) GetDuration(key string) time.Duration {
c.mutex.RLock()
defer c.mutex.RUnlock()
if v, ok := c.combinedConfig[strings.ToLower(key)]; ok {
v, ok := c.combinedConfig[strings.ToLower(key)]
if !ok {
v, ok = c.envConfig[strings.ToLower(key)]
if !ok {
return 0
}
}
val := v.Value
switch val := val.(type) {
case time.Duration:
@@ -68,57 +78,75 @@ func (c *ConfigManager) GetDuration(key string) time.Duration {
return 0
default:
return val.(time.Duration)
}
}
return 0
}
func (c *ConfigManager) GetString(key string) string {
c.mutex.RLock()
defer c.mutex.RUnlock()
if v, ok := c.combinedConfig[strings.ToLower(key)]; ok {
v, ok := c.combinedConfig[strings.ToLower(key)]
if !ok {
v, ok = c.envConfig[strings.ToLower(key)]
if !ok {
return ""
}
}
switch val := v.Value.(type) {
case string:
return val
default:
return fmt.Sprintf("%v", v.Value)
}
}
return ""
}
func (c *ConfigManager) GetStringMap(key string) map[string]any {
c.mutex.RLock()
defer c.mutex.RUnlock()
if v, ok := c.combinedConfig[strings.ToLower(key)]; ok {
v, ok := c.combinedConfig[strings.ToLower(key)]
if !ok {
v, ok = c.envConfig[strings.ToLower(key)]
if !ok {
return nil
}
}
switch val := v.Value.(type) {
case map[string]any:
return val
default:
return nil
}
}
return nil
}
func (c *ConfigManager) GetStringSlice(key string) []string {
c.mutex.RLock()
defer c.mutex.RUnlock()
if v, ok := c.combinedConfig[strings.ToLower(key)]; ok {
v, ok := c.combinedConfig[strings.ToLower(key)]
if !ok {
v, ok = c.envConfig[strings.ToLower(key)]
if !ok {
return nil
}
}
switch val := v.Value.(type) {
case []string:
return val
default:
return nil
}
}
return nil
}
func (c *ConfigManager) GetInt(key string) int {
c.mutex.RLock()
defer c.mutex.RUnlock()
if v, ok := c.combinedConfig[strings.ToLower(key)]; ok {
v, ok := c.combinedConfig[strings.ToLower(key)]
if !ok {
v, ok = c.envConfig[strings.ToLower(key)]
if !ok {
return 0
}
}
switch val := v.Value.(type) {
case int:
return val
@@ -135,22 +163,24 @@ func (c *ConfigManager) GetInt(key string) int {
case nil:
return 0
default:
return val.(int)
}
}
return 0
}
}
func (c *ConfigManager) GetIntSlice(key string) []int {
c.mutex.RLock()
defer c.mutex.RUnlock()
if v, ok := c.combinedConfig[strings.ToLower(key)]; ok {
v, ok := c.combinedConfig[strings.ToLower(key)]
if !ok {
v, ok = c.envConfig[strings.ToLower(key)]
if !ok {
return nil
}
}
switch val := v.Value.(type) {
case []int:
return val
default:
return nil
}
}
return nil
}

View File

@@ -28,6 +28,7 @@ type (
ConfigManager struct {
configName string
configPath string
configFileUsed string
configType configType
envPrefix string
@@ -201,6 +202,12 @@ func readFile(filename string, fileType configType) (map[string]any, error) {
}
}
func (c *ConfigManager) SetConfigDir(path string) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.configPath = name
}
func (c *ConfigManager) SetConfigName(name string) {
c.mutex.Lock()
defer c.mutex.Unlock()

View File

@@ -9,6 +9,7 @@ func (c *ConfigManager) SetBool(key string, value bool) {
defer c.mutex.Unlock()
lower := strings.ToLower(key)
c.mapConfig[lower] = ConfigMap{Key: key, Value: value}
c.combinedConfig[lower] = ConfigMap{Key: key, Value: value}
}
func (c *ConfigManager) SetString(key string, value string) {
@@ -16,6 +17,7 @@ func (c *ConfigManager) SetString(key string, value string) {
defer c.mutex.Unlock()
lower := strings.ToLower(key)
c.mapConfig[lower] = ConfigMap{Key: key, Value: value}
c.combinedConfig[lower] = ConfigMap{Key: key, Value: value}
}
func (c *ConfigManager) Set(key string, value any) {
@@ -23,6 +25,7 @@ func (c *ConfigManager) Set(key string, value any) {
defer c.mutex.Unlock()
lower := strings.ToLower(key)
c.mapConfig[lower] = ConfigMap{Key: key, Value: value}
c.combinedConfig[lower] = ConfigMap{Key: key, Value: value}
}
func (c *ConfigManager) SetDefault(key string, value any) {
@@ -30,4 +33,10 @@ func (c *ConfigManager) SetDefault(key string, value any) {
defer c.mutex.Unlock()
lower := strings.ToLower(key)
c.defaultConfig[lower] = ConfigMap{Key: key, Value: value}
if _, ok := c.mapConfig[lower]; !ok {
if envVal, ok := c.envConfig[lower]; !ok {
c.mapConfig[lower] = envVal
c.combinedConfig[lower] = envVal
}
}
}