Compare commits

..

1 Commits

2 changed files with 15 additions and 14 deletions

19
gin.go
View File

@ -23,12 +23,10 @@ import (
"golang.org/x/net/http2/h2c"
)
const (
defaultMultipartMemory = 32 << 20 // 32 MB
escapedColon = "\\:"
colon = ":"
backslash = "\\"
)
const defaultMultipartMemory = 32 << 20 // 32 MB
const escapedColon = "\\:"
const colon = ":"
const backslash = "\\"
var (
default404Body = []byte("404 page not found")
@ -48,10 +46,8 @@ var defaultTrustedCIDRs = []*net.IPNet{
},
}
var (
regSafePrefix = regexp.MustCompile("[^a-zA-Z0-9/-]+")
regRemoveRepeatedChar = regexp.MustCompile("/{2,}")
)
var regSafePrefix = regexp.MustCompile("[^a-zA-Z0-9/-]+")
var regRemoveRepeatedChar = regexp.MustCompile("/{2,}")
// HandlerFunc defines the handler used by gin middleware as return value.
type HandlerFunc func(*Context)
@ -99,7 +95,8 @@ type Engine struct {
RouterGroup
// routeTreesUpdated ensures that the initialization or update of the route trees
// (used for routing HTTP requests) happens only once, even if called multiple times concurrently.
// (used for routing HTTP requests) happens only once, even if called multiple times
// concurrently. It helps prevent race conditions and redundant setup operations.
routeTreesUpdated sync.Once
// RedirectTrailingSlash enables automatic redirection if the current route can't be matched but a

View File

@ -93,15 +93,19 @@ func TestUpdateRouteTreesCalledOnce(t *testing.T) {
SetMode(TestMode)
router := New()
callCount := 0
originalUpdate := router.updateRouteTrees
router.GET(`/test\:action`, func(c *Context) {
c.String(http.StatusOK, "ok")
c.JSON(http.StatusOK, H{"call": callCount})
})
for range 5 {
for i := 0; i < 5; i++ {
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/test:action", nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "ok", w.Body.String())
}
_ = originalUpdate
}