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,147 +10,177 @@ import (
func (c *ConfigManager) GetBool(key string) bool { func (c *ConfigManager) GetBool(key string) bool {
c.mutex.RLock() c.mutex.RLock()
defer c.mutex.RUnlock() defer c.mutex.RUnlock()
if v, ok := c.combinedConfig[strings.ToLower(key)]; ok { v, ok := c.combinedConfig[strings.ToLower(key)]
val := v.Value if !ok {
switch val := val.(type) { v, ok = c.envConfig[strings.ToLower(key)]
case bool: if !ok {
return val
case string:
if strings.ToLower(val) == "true" {
return true
}
return false return false
case int:
if val == 0 {
return false
}
return true
case float32, float64:
if val == 0 {
return false
}
return true
case nil:
return false
case time.Duration:
if val == 0 || val < 0 {
return false
}
return true
default:
return val.(bool)
} }
} }
return false val := v.Value
switch val := val.(type) {
case bool:
return val
case string:
if strings.ToLower(val) == "true" {
return true
}
return false
case int:
if val == 0 {
return false
}
return true
case float32, float64:
if val == 0 {
return false
}
return true
case nil:
return false
case time.Duration:
if val == 0 || val < 0 {
return false
}
return true
default:
return val.(bool)
}
} }
func (c *ConfigManager) GetDuration(key string) time.Duration { func (c *ConfigManager) GetDuration(key string) time.Duration {
c.mutex.RLock() c.mutex.RLock()
defer c.mutex.RUnlock() defer c.mutex.RUnlock()
if v, ok := c.combinedConfig[strings.ToLower(key)]; ok { v, ok := c.combinedConfig[strings.ToLower(key)]
val := v.Value if !ok {
switch val := val.(type) { v, ok = c.envConfig[strings.ToLower(key)]
case time.Duration: if !ok {
return val
case string:
d, err := time.ParseDuration(val)
if err != nil {
return 0
}
return d
case int:
return time.Duration(val)
case float32:
return time.Duration(val)
case float64:
return time.Duration(val)
case nil:
return 0 return 0
default:
return val.(time.Duration)
} }
} }
return 0 val := v.Value
switch val := val.(type) {
case time.Duration:
return val
case string:
d, err := time.ParseDuration(val)
if err != nil {
return 0
}
return d
case int:
return time.Duration(val)
case float32:
return time.Duration(val)
case float64:
return time.Duration(val)
case nil:
return 0
default:
return val.(time.Duration)
}
} }
func (c *ConfigManager) GetString(key string) string { func (c *ConfigManager) GetString(key string) string {
c.mutex.RLock() c.mutex.RLock()
defer c.mutex.RUnlock() defer c.mutex.RUnlock()
if v, ok := c.combinedConfig[strings.ToLower(key)]; ok { v, ok := c.combinedConfig[strings.ToLower(key)]
switch val := v.Value.(type) { if !ok {
case string: v, ok = c.envConfig[strings.ToLower(key)]
return val if !ok {
default: return ""
return fmt.Sprintf("%v", v.Value)
} }
} }
return ""
switch val := v.Value.(type) {
case string:
return val
default:
return fmt.Sprintf("%v", v.Value)
}
} }
func (c *ConfigManager) GetStringMap(key string) map[string]any { func (c *ConfigManager) GetStringMap(key string) map[string]any {
c.mutex.RLock() c.mutex.RLock()
defer c.mutex.RUnlock() defer c.mutex.RUnlock()
if v, ok := c.combinedConfig[strings.ToLower(key)]; ok { v, ok := c.combinedConfig[strings.ToLower(key)]
switch val := v.Value.(type) { if !ok {
case map[string]any: v, ok = c.envConfig[strings.ToLower(key)]
return val if !ok {
default:
return nil return nil
} }
} }
return nil switch val := v.Value.(type) {
case map[string]any:
return val
default:
return nil
}
} }
func (c *ConfigManager) GetStringSlice(key string) []string { func (c *ConfigManager) GetStringSlice(key string) []string {
c.mutex.RLock() c.mutex.RLock()
defer c.mutex.RUnlock() defer c.mutex.RUnlock()
if v, ok := c.combinedConfig[strings.ToLower(key)]; ok { v, ok := c.combinedConfig[strings.ToLower(key)]
switch val := v.Value.(type) { if !ok {
case []string: v, ok = c.envConfig[strings.ToLower(key)]
return val if !ok {
default:
return nil return nil
} }
} }
return nil switch val := v.Value.(type) {
case []string:
return val
default:
return nil
}
} }
func (c *ConfigManager) GetInt(key string) int { func (c *ConfigManager) GetInt(key string) int {
c.mutex.RLock() c.mutex.RLock()
defer c.mutex.RUnlock() defer c.mutex.RUnlock()
if v, ok := c.combinedConfig[strings.ToLower(key)]; ok { v, ok := c.combinedConfig[strings.ToLower(key)]
switch val := v.Value.(type) { if !ok {
case int: v, ok = c.envConfig[strings.ToLower(key)]
return val if !ok {
case string:
i, err := strconv.Atoi(val)
if err != nil {
return 0
}
return i
case float32:
return int(val)
case float64:
return int(val)
case nil:
return 0 return 0
default:
return val.(int)
} }
} }
return 0 switch val := v.Value.(type) {
case int:
return val
case string:
i, err := strconv.Atoi(val)
if err != nil {
return 0
}
return i
case float32:
return int(val)
case float64:
return int(val)
case nil:
return 0
default:
return 0
}
} }
func (c *ConfigManager) GetIntSlice(key string) []int { func (c *ConfigManager) GetIntSlice(key string) []int {
c.mutex.RLock() c.mutex.RLock()
defer c.mutex.RUnlock() defer c.mutex.RUnlock()
if v, ok := c.combinedConfig[strings.ToLower(key)]; ok { v, ok := c.combinedConfig[strings.ToLower(key)]
switch val := v.Value.(type) { if !ok {
case []int: v, ok = c.envConfig[strings.ToLower(key)]
return val if !ok {
default:
return nil return nil
} }
} }
return nil switch val := v.Value.(type) {
case []int:
return val
default:
return nil
}
} }

View File

@@ -28,6 +28,7 @@ type (
ConfigManager struct { ConfigManager struct {
configName string configName string
configPath string
configFileUsed string configFileUsed string
configType configType configType configType
envPrefix string 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) { func (c *ConfigManager) SetConfigName(name string) {
c.mutex.Lock() c.mutex.Lock()
defer c.mutex.Unlock() defer c.mutex.Unlock()

View File

@@ -9,6 +9,7 @@ func (c *ConfigManager) SetBool(key string, value bool) {
defer c.mutex.Unlock() defer c.mutex.Unlock()
lower := strings.ToLower(key) lower := strings.ToLower(key)
c.mapConfig[lower] = ConfigMap{Key: key, Value: value} c.mapConfig[lower] = ConfigMap{Key: key, Value: value}
c.combinedConfig[lower] = ConfigMap{Key: key, Value: value}
} }
func (c *ConfigManager) SetString(key string, value string) { func (c *ConfigManager) SetString(key string, value string) {
@@ -16,6 +17,7 @@ func (c *ConfigManager) SetString(key string, value string) {
defer c.mutex.Unlock() defer c.mutex.Unlock()
lower := strings.ToLower(key) lower := strings.ToLower(key)
c.mapConfig[lower] = ConfigMap{Key: key, Value: value} c.mapConfig[lower] = ConfigMap{Key: key, Value: value}
c.combinedConfig[lower] = ConfigMap{Key: key, Value: value}
} }
func (c *ConfigManager) Set(key string, value any) { func (c *ConfigManager) Set(key string, value any) {
@@ -23,6 +25,7 @@ func (c *ConfigManager) Set(key string, value any) {
defer c.mutex.Unlock() defer c.mutex.Unlock()
lower := strings.ToLower(key) lower := strings.ToLower(key)
c.mapConfig[lower] = ConfigMap{Key: key, Value: value} c.mapConfig[lower] = ConfigMap{Key: key, Value: value}
c.combinedConfig[lower] = ConfigMap{Key: key, Value: value}
} }
func (c *ConfigManager) SetDefault(key string, value any) { func (c *ConfigManager) SetDefault(key string, value any) {
@@ -30,4 +33,10 @@ func (c *ConfigManager) SetDefault(key string, value any) {
defer c.mutex.Unlock() defer c.mutex.Unlock()
lower := strings.ToLower(key) lower := strings.ToLower(key)
c.defaultConfig[lower] = ConfigMap{Key: key, Value: value} 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
}
}
} }