fix: data race warning (#1180)

This commit is contained in:
Kostadin Plachkov 2018-10-01 21:13:28 +03:00
parent e9f187f60a
commit 1bd3044d48
2 changed files with 11 additions and 8 deletions

View File

@ -12,6 +12,7 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"sync/atomic"
) )
const ginSupportMinGoVer = 6 const ginSupportMinGoVer = 6
@ -19,7 +20,7 @@ const ginSupportMinGoVer = 6
// IsDebugging returns true if the framework is running in debug mode. // IsDebugging returns true if the framework is running in debug mode.
// Use SetMode(gin.ReleaseMode) to disable debug mode. // Use SetMode(gin.ReleaseMode) to disable debug mode.
func IsDebugging() bool { func IsDebugging() bool {
return ginMode == debugCode return atomic.LoadInt32(&ginMode) == debugCode
} }
// DebugPrintRouteFunc indicates debug log output format. // DebugPrintRouteFunc indicates debug log output format.

16
mode.go
View File

@ -7,6 +7,7 @@ package gin
import ( import (
"io" "io"
"os" "os"
"sync/atomic"
"github.com/gin-gonic/gin/binding" "github.com/gin-gonic/gin/binding"
) )
@ -38,10 +39,11 @@ const (
var DefaultWriter io.Writer = os.Stdout var DefaultWriter io.Writer = os.Stdout
var DefaultErrorWriter io.Writer = os.Stderr var DefaultErrorWriter io.Writer = os.Stderr
var ginMode = debugCode var ginMode int32 = debugCode
var modeName = DebugMode var modeName atomic.Value
func init() { func init() {
modeName.Store(DebugMode)
mode := os.Getenv(ENV_GIN_MODE) mode := os.Getenv(ENV_GIN_MODE)
SetMode(mode) SetMode(mode)
} }
@ -50,18 +52,18 @@ func init() {
func SetMode(value string) { func SetMode(value string) {
switch value { switch value {
case DebugMode, "": case DebugMode, "":
ginMode = debugCode atomic.StoreInt32(&ginMode, debugCode)
case ReleaseMode: case ReleaseMode:
ginMode = releaseCode atomic.StoreInt32(&ginMode, releaseCode)
case TestMode: case TestMode:
ginMode = testCode atomic.StoreInt32(&ginMode, testCode)
default: default:
panic("gin mode unknown: " + value) panic("gin mode unknown: " + value)
} }
if value == "" { if value == "" {
value = DebugMode value = DebugMode
} }
modeName = value modeName.Store(value)
} }
// DisableBindValidation closes the default validator. // DisableBindValidation closes the default validator.
@ -77,5 +79,5 @@ func EnableJsonDecoderUseNumber() {
// Mode returns currently gin mode. // Mode returns currently gin mode.
func Mode() string { func Mode() string {
return modeName return modeName.Load().(string)
} }