* fix race condition

This commit is contained in:
Edward Middleton 2013-12-12 21:57:10 +09:00
parent 1e6be1646e
commit bd93629e32
1 changed files with 14 additions and 3 deletions

View File

@ -6,14 +6,23 @@ import (
"reflect"
"strconv"
"strings"
"sync"
)
var toSnakeMap = map[string]string{}
type SnakeMap struct {
Map map[string]string
mu sync.RWMutex
}
var snake = SnakeMap{Map:map[string]string{}}
var toUpperMap = map[string]string{}
func toSnake(u string) string {
if v := toSnakeMap[u]; v != "" {
snake.mu.RLock()
if v := snake.Map[u]; v != "" {
snake.mu.RUnlock()
return v
} else {
snake.mu.RUnlock()
}
buf := bytes.NewBufferString("")
@ -25,7 +34,9 @@ func toSnake(u string) string {
}
s := strings.ToLower(buf.String())
toSnakeMap[u] = s
snake.mu.Lock()
defer snake.mu.Unlock()
snake.Map[u] = s
return s
}