From bd93629e32b2c0b5e7f10be5d13e1232b4a083df Mon Sep 17 00:00:00 2001 From: Edward Middleton Date: Thu, 12 Dec 2013 21:57:10 +0900 Subject: [PATCH] * fix race condition --- utils.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/utils.go b/utils.go index 6ad79420..9b705734 100644 --- a/utils.go +++ b/utils.go @@ -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 }