Skip to content

Commit 7d14792

Browse files
kplachkovappleboy
andauthored
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 <[email protected]> * test: refactor test assertions for mode settings - Update test assertions for mode setting in `mode_test.go` Signed-off-by: Bo-Yi Wu <[email protected]> --------- Signed-off-by: Bo-Yi Wu <[email protected]> Co-authored-by: Bo-Yi Wu <[email protected]>
1 parent f5f5da8 commit 7d14792

File tree

3 files changed

+17
-25
lines changed

3 files changed

+17
-25
lines changed

debug.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import (
1010
"runtime"
1111
"strconv"
1212
"strings"
13+
"sync/atomic"
1314
)
1415

1516
const ginSupportMinGoVer = 18
1617

1718
// IsDebugging returns true if the framework is running in debug mode.
1819
// Use SetMode(gin.ReleaseMode) to disable debug mode.
1920
func IsDebugging() bool {
20-
return ginMode == debugCode
21+
return atomic.LoadInt32(&ginMode) == debugCode
2122
}
2223

2324
// DebugPrintRouteFunc indicates debug log output format.

mode.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"flag"
99
"io"
1010
"os"
11+
"sync/atomic"
1112

1213
"github.com/gin-gonic/gin/binding"
1314
)
@@ -43,10 +44,8 @@ var DefaultWriter io.Writer = os.Stdout
4344
// DefaultErrorWriter is the default io.Writer used by Gin to debug errors
4445
var DefaultErrorWriter io.Writer = os.Stderr
4546

46-
var (
47-
ginMode = debugCode
48-
modeName = DebugMode
49-
)
47+
var ginMode int32 = debugCode
48+
var modeName atomic.Value
5049

5150
func init() {
5251
mode := os.Getenv(EnvGinMode)
@@ -64,17 +63,16 @@ func SetMode(value string) {
6463
}
6564

6665
switch value {
67-
case DebugMode:
68-
ginMode = debugCode
66+
case DebugMode, "":
67+
atomic.StoreInt32(&ginMode, debugCode)
6968
case ReleaseMode:
70-
ginMode = releaseCode
69+
atomic.StoreInt32(&ginMode, releaseCode)
7170
case TestMode:
72-
ginMode = testCode
71+
atomic.StoreInt32(&ginMode, testCode)
7372
default:
7473
panic("gin mode unknown: " + value + " (available mode: debug release test)")
7574
}
76-
77-
modeName = value
75+
modeName.Store(value)
7876
}
7977

8078
// DisableBindValidation closes the default validator.
@@ -96,5 +94,5 @@ func EnableJsonDecoderDisallowUnknownFields() {
9694

9795
// Mode returns current gin mode.
9896
func Mode() string {
99-
return modeName
97+
return modeName.Load().(string)
10098
}

mode_test.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
package gin
66

77
import (
8-
"flag"
98
"os"
9+
"sync/atomic"
1010
"testing"
1111

1212
"github.com/gin-gonic/gin/binding"
@@ -18,31 +18,24 @@ func init() {
1818
}
1919

2020
func TestSetMode(t *testing.T) {
21-
assert.Equal(t, testCode, ginMode)
21+
assert.Equal(t, int32(testCode), atomic.LoadInt32(&ginMode))
2222
assert.Equal(t, TestMode, Mode())
2323
os.Unsetenv(EnvGinMode)
2424

2525
SetMode("")
26-
assert.Equal(t, testCode, ginMode)
26+
assert.Equal(t, int32(testCode), atomic.LoadInt32(&ginMode))
2727
assert.Equal(t, TestMode, Mode())
2828

29-
tmp := flag.CommandLine
30-
flag.CommandLine = flag.NewFlagSet("", flag.ContinueOnError)
31-
SetMode("")
32-
assert.Equal(t, debugCode, ginMode)
33-
assert.Equal(t, DebugMode, Mode())
34-
flag.CommandLine = tmp
35-
3629
SetMode(DebugMode)
37-
assert.Equal(t, debugCode, ginMode)
30+
assert.Equal(t, int32(debugCode), atomic.LoadInt32(&ginMode))
3831
assert.Equal(t, DebugMode, Mode())
3932

4033
SetMode(ReleaseMode)
41-
assert.Equal(t, releaseCode, ginMode)
34+
assert.Equal(t, int32(releaseCode), atomic.LoadInt32(&ginMode))
4235
assert.Equal(t, ReleaseMode, Mode())
4336

4437
SetMode(TestMode)
45-
assert.Equal(t, testCode, ginMode)
38+
assert.Equal(t, int32(testCode), atomic.LoadInt32(&ginMode))
4639
assert.Equal(t, TestMode, Mode())
4740

4841
assert.Panics(t, func() { SetMode("unknown") })

0 commit comments

Comments
 (0)