fix(gin): data race warning for gin mode (#1580)
* fix: data race warning (#1180) * Fix the tests * refactor: remove unnecessary imports and optimize codebase - Remove unnecessary import of `flag` Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com> * test: refactor test assertions for mode settings - Update test assertions for mode setting in `mode_test.go` Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com> --------- Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com> Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
committed by
GitHub
parent
f5f5da8fa0
commit
7d147928ee
@@ -8,6 +8,7 @@ import (
|
||||
"flag"
|
||||
"io"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
@@ -43,10 +44,8 @@ var DefaultWriter io.Writer = os.Stdout
|
||||
// DefaultErrorWriter is the default io.Writer used by Gin to debug errors
|
||||
var DefaultErrorWriter io.Writer = os.Stderr
|
||||
|
||||
var (
|
||||
ginMode = debugCode
|
||||
modeName = DebugMode
|
||||
)
|
||||
var ginMode int32 = debugCode
|
||||
var modeName atomic.Value
|
||||
|
||||
func init() {
|
||||
mode := os.Getenv(EnvGinMode)
|
||||
@@ -64,17 +63,16 @@ func SetMode(value string) {
|
||||
}
|
||||
|
||||
switch value {
|
||||
case DebugMode:
|
||||
ginMode = debugCode
|
||||
case DebugMode, "":
|
||||
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 + " (available mode: debug release test)")
|
||||
}
|
||||
|
||||
modeName = value
|
||||
modeName.Store(value)
|
||||
}
|
||||
|
||||
// DisableBindValidation closes the default validator.
|
||||
@@ -96,5 +94,5 @@ func EnableJsonDecoderDisallowUnknownFields() {
|
||||
|
||||
// Mode returns current gin mode.
|
||||
func Mode() string {
|
||||
return modeName
|
||||
return modeName.Load().(string)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user