mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 04:57:07 +08:00
fix: data race warning (#1180)
This commit is contained in:
parent
e9f187f60a
commit
1bd3044d48
3
debug.go
3
debug.go
@ -12,6 +12,7 @@ import (
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
const ginSupportMinGoVer = 6
|
||||
@ -19,7 +20,7 @@ const ginSupportMinGoVer = 6
|
||||
// IsDebugging returns true if the framework is running in debug mode.
|
||||
// Use SetMode(gin.ReleaseMode) to disable debug mode.
|
||||
func IsDebugging() bool {
|
||||
return ginMode == debugCode
|
||||
return atomic.LoadInt32(&ginMode) == debugCode
|
||||
}
|
||||
|
||||
// DebugPrintRouteFunc indicates debug log output format.
|
||||
|
16
mode.go
16
mode.go
@ -7,6 +7,7 @@ package gin
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
@ -38,10 +39,11 @@ const (
|
||||
var DefaultWriter io.Writer = os.Stdout
|
||||
var DefaultErrorWriter io.Writer = os.Stderr
|
||||
|
||||
var ginMode = debugCode
|
||||
var modeName = DebugMode
|
||||
var ginMode int32 = debugCode
|
||||
var modeName atomic.Value
|
||||
|
||||
func init() {
|
||||
modeName.Store(DebugMode)
|
||||
mode := os.Getenv(ENV_GIN_MODE)
|
||||
SetMode(mode)
|
||||
}
|
||||
@ -50,18 +52,18 @@ func init() {
|
||||
func SetMode(value string) {
|
||||
switch value {
|
||||
case DebugMode, "":
|
||||
ginMode = debugCode
|
||||
atomic.StoreInt32(&ginMode, debugCode)
|
||||
case ReleaseMode:
|
||||
ginMode = releaseCode
|
||||
atomic.StoreInt32(&ginMode, releaseCode)
|
||||
case TestMode:
|
||||
ginMode = testCode
|
||||
atomic.StoreInt32(&ginMode, testCode)
|
||||
default:
|
||||
panic("gin mode unknown: " + value)
|
||||
}
|
||||
if value == "" {
|
||||
value = DebugMode
|
||||
}
|
||||
modeName = value
|
||||
modeName.Store(value)
|
||||
}
|
||||
|
||||
// DisableBindValidation closes the default validator.
|
||||
@ -77,5 +79,5 @@ func EnableJsonDecoderUseNumber() {
|
||||
|
||||
// Mode returns currently gin mode.
|
||||
func Mode() string {
|
||||
return modeName
|
||||
return modeName.Load().(string)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user