Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gin): data race warning for gin mode #1580

Merged
merged 8 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (
"runtime"
"strconv"
"strings"
"sync/atomic"
)

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.
Expand Down
20 changes: 9 additions & 11 deletions mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"flag"
"io"
"os"
"sync/atomic"

"github.com/gin-gonic/gin/binding"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand All @@ -96,5 +94,5 @@ func EnableJsonDecoderDisallowUnknownFields() {

// Mode returns current gin mode.
func Mode() string {
return modeName
return modeName.Load().(string)
}
19 changes: 6 additions & 13 deletions mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
package gin

import (
"flag"
"os"
"sync/atomic"
"testing"

"github.com/gin-gonic/gin/binding"
Expand All @@ -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") })
Expand Down
Loading