1
0
Fork 0
forked from External/grumble

Use a RWMutex for server config locking instead of routing config gets and sets through Server#handler.

This commit is contained in:
Mikkel Krautz 2011-05-13 15:32:42 +02:00
parent 41f6af2334
commit c785c45166
5 changed files with 62 additions and 61 deletions

View file

@ -6,6 +6,7 @@ package serverconf
import (
"strconv"
"sync"
)
var defaultCfg = map[string]string{
@ -21,14 +22,52 @@ var defaultCfg = map[string]string{
"SendVersion": "true",
}
type Config map[string]string
func (cfg Config) Set(key string, value string) {
cfg[key] = value
type Config struct {
cfgMap map[string]string
mutex sync.RWMutex
}
func (cfg Config) StringValue(key string) (value string) {
value, exists := cfg[key]
// Create a new Config using cfgMap as the intial internal config map.
// If cfgMap is nil, ConfigWithMap will create a new config map.
func New(cfgMap map[string]string) *Config {
if cfgMap == nil {
cfgMap = make(map[string]string)
}
return &Config{cfgMap: cfgMap}
}
// Get a copy of the Config's internal config map
func (cfg *Config) GetAll() (all map[string]string) {
cfg.mutex.RLock()
defer cfg.mutex.RUnlock()
all = make(map[string]string)
for k,v := range cfg.cfgMap {
all[k] = v
}
return
}
// Set a new value for a config key
func (cfg *Config) Set(key string, value string) {
cfg.mutex.Lock()
defer cfg.mutex.Unlock()
cfg.cfgMap[key] = value
}
// Reset the value of a config key
func (cfg *Config) Reset(key string) {
cfg.mutex.Lock()
defer cfg.mutex.Unlock()
cfg.cfgMap[key] = "", false
}
// Get the value of a specific config key encoded as a string
func (cfg *Config) StringValue(key string) (value string) {
cfg.mutex.RLock()
defer cfg.mutex.RUnlock()
value, exists := cfg.cfgMap[key]
if exists {
return value
}
@ -41,24 +80,23 @@ func (cfg Config) StringValue(key string) (value string) {
return ""
}
func (cfg Config) IntValue(key string) (intval int) {
// Get the value of a speific config key as an int
func (cfg *Config) IntValue(key string) (intval int) {
str := cfg.StringValue(key)
intval, _ = strconv.Atoi(str)
return
}
func (cfg Config) Uint32Value(key string) (uint32val uint32) {
// Get the value of a specific config key as a uint32
func (cfg *Config) Uint32Value(key string) (uint32val uint32) {
str := cfg.StringValue(key)
uintval, _ := strconv.Atoui(str)
return uint32(uintval)
}
func (cfg Config) BoolValue(key string) (boolval bool) {
// Get the value fo a sepcific config key as a bool
func (cfg *Config) BoolValue(key string) (boolval bool) {
str := cfg.StringValue(key)
boolval, _ = strconv.Atob(str)
return
}
func (cfg Config) Reset(key string) {
cfg[key] = "", false
}

View file

@ -9,7 +9,7 @@ import (
)
func TestIntValue(t *testing.T) {
cfg := make(Config)
cfg := New(nil)
cfg.Set("Test", "13")
if cfg.IntValue("Test") != 13 {
t.Errorf("Expected 13")
@ -17,7 +17,7 @@ func TestIntValue(t *testing.T) {
}
func TestFloatAsInt(t *testing.T) {
cfg := make(Config)
cfg := New(nil)
cfg.Set("Test", "13.4")
if cfg.IntValue("Test") != 0 {
t.Errorf("Expected 0")
@ -25,14 +25,14 @@ func TestFloatAsInt(t *testing.T) {
}
func TestDefaultValue(t *testing.T) {
cfg := make(Config)
cfg := New(nil)
if cfg.IntValue("MaxBandwidth") != 72000 {
t.Errorf("Expected 72000")
}
}
func TestBoolValue(t *testing.T) {
cfg := make(Config)
cfg := New(nil)
cfg.Set("DoStuffOnStartup", "true")
if cfg.BoolValue("DoStuffOnStartup") != true {
t.Errorf("Expected true")