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

20
mode.go
View File

@ -8,6 +8,7 @@ import (
"flag" "flag"
"io" "io"
"os" "os"
"sync/atomic"
"github.com/gin-gonic/gin/binding" "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 // DefaultErrorWriter is the default io.Writer used by Gin to debug errors
var DefaultErrorWriter io.Writer = os.Stderr var DefaultErrorWriter io.Writer = os.Stderr
var ( var ginMode int32 = debugCode
ginMode = debugCode var modeName atomic.Value
modeName = DebugMode
)
func init() { func init() {
mode := os.Getenv(EnvGinMode) mode := os.Getenv(EnvGinMode)
@ -64,17 +63,16 @@ 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 + " (available mode: debug release test)") panic("gin mode unknown: " + value + " (available mode: debug release test)")
} }
modeName.Store(value)
modeName = value
} }
// DisableBindValidation closes the default validator. // DisableBindValidation closes the default validator.
@ -96,5 +94,5 @@ func EnableJsonDecoderDisallowUnknownFields() {
// Mode returns current gin mode. // Mode returns current gin mode.
func Mode() string { func Mode() string {
return modeName return modeName.Load().(string)
} }

View File

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