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:
Kostadin Plachkov 2024-05-08 04:13:36 +03:00 committed by GitHub
parent f5f5da8fa0
commit 7d147928ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 25 deletions

View File

@ -10,6 +10,7 @@ import (
"runtime"
"strconv"
"strings"
"sync/atomic"
)
const ginSupportMinGoVer = 18
@ -17,7 +18,7 @@ const ginSupportMinGoVer = 18
// 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.

20
mode.go
View File

@ -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)
}

View File

@ -5,8 +5,8 @@
package gin
import (
"flag"
"os"
"sync/atomic"
"testing"
"github.com/gin-gonic/gin/binding"
@ -18,31 +18,24 @@ func init() {
}
func TestSetMode(t *testing.T) {
assert.Equal(t, testCode, ginMode)
assert.Equal(t, int32(testCode), atomic.LoadInt32(&ginMode))
assert.Equal(t, TestMode, Mode())
os.Unsetenv(EnvGinMode)
SetMode("")
assert.Equal(t, testCode, ginMode)
assert.Equal(t, int32(testCode), atomic.LoadInt32(&ginMode))
assert.Equal(t, TestMode, Mode())
tmp := flag.CommandLine
flag.CommandLine = flag.NewFlagSet("", flag.ContinueOnError)
SetMode("")
assert.Equal(t, debugCode, ginMode)
assert.Equal(t, DebugMode, Mode())
flag.CommandLine = tmp
SetMode(DebugMode)
assert.Equal(t, debugCode, ginMode)
assert.Equal(t, int32(debugCode), atomic.LoadInt32(&ginMode))
assert.Equal(t, DebugMode, Mode())
SetMode(ReleaseMode)
assert.Equal(t, releaseCode, ginMode)
assert.Equal(t, int32(releaseCode), atomic.LoadInt32(&ginMode))
assert.Equal(t, ReleaseMode, Mode())
SetMode(TestMode)
assert.Equal(t, testCode, ginMode)
assert.Equal(t, int32(testCode), atomic.LoadInt32(&ginMode))
assert.Equal(t, TestMode, Mode())
assert.Panics(t, func() { SetMode("unknown") })