mirror of
https://github.com/gin-gonic/gin.git
synced 2026-07-05 18:00:54 +08:00
Compare commits
1 Commits
8ed70764ce
...
738d09978c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
738d09978c |
19
gin.go
19
gin.go
@ -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
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user