-
Notifications
You must be signed in to change notification settings - Fork 8k
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 the misplacement of adding slashes #2847
Conversation
Thanks for your effort. Can you also provide the benchmark result like this comment: #2767 (comment) Hi @qm012 @rw-access, please also take a look if you have free time. |
How to do benchmark test, please guide me |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How to do benchmark test, please guide me
Please refer to the #2796 (comment)
- fork https://github.com/gin-gonic/go-http-routing-benchmark
- Go.mod changes the 'gin' branch version
- Run the code for 'this PR' and 'master' using 'https://app.travis-ci.com/'
- Use 'benchstat old.txt new.txt' to generate test reports
routes_test.go
Outdated
@@ -621,3 +621,24 @@ func TestRouteContextHoldsFullPath(t *testing.T) { | |||
w := performRequest(router, http.MethodGet, "/not-found") | |||
assert.Equal(t, http.StatusNotFound, w.Code) | |||
} | |||
|
|||
// Reproduction test for the bug of issue #2843 | |||
func TestRouteTrailingSlashRoute(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we don't need a new function, could we integrate it into the function 'TestRouterNotFound'?
Lines 434 to 470 in 4e75841
func TestRouterNotFound(t *testing.T) { | |
router := New() | |
router.RedirectFixedPath = true | |
router.GET("/path", func(c *Context) {}) | |
router.GET("/dir/", func(c *Context) {}) | |
router.GET("/", func(c *Context) {}) | |
testRoutes := []struct { | |
route string | |
code int | |
location string | |
}{ | |
{"/path/", http.StatusMovedPermanently, "/path"}, // TSR -/ | |
{"/dir", http.StatusMovedPermanently, "/dir/"}, // TSR +/ | |
{"/PATH", http.StatusMovedPermanently, "/path"}, // Fixed Case | |
{"/DIR/", http.StatusMovedPermanently, "/dir/"}, // Fixed Case | |
{"/PATH/", http.StatusMovedPermanently, "/path"}, // Fixed Case -/ | |
{"/DIR", http.StatusMovedPermanently, "/dir/"}, // Fixed Case +/ | |
{"/../path", http.StatusMovedPermanently, "/path"}, // Without CleanPath | |
{"/nope", http.StatusNotFound, ""}, // NotFound | |
} | |
for _, tr := range testRoutes { | |
w := performRequest(router, http.MethodGet, tr.route) | |
assert.Equal(t, tr.code, w.Code) | |
if w.Code != http.StatusNotFound { | |
assert.Equal(t, tr.location, fmt.Sprint(w.Header().Get("Location"))) | |
} | |
} | |
// Test custom not found handler | |
var notFound bool | |
router.NoRoute(func(c *Context) { | |
c.AbortWithStatus(http.StatusNotFound) | |
notFound = true | |
}) | |
w := performRequest(router, http.MethodGet, "/nope") | |
assert.Equal(t, http.StatusNotFound, w.Code) |
tree.go
Outdated
@@ -599,7 +599,7 @@ walk: // Outer loop for walking the tree | |||
// Nothing found. We can recommend to redirect to the same URL with an | |||
// extra trailing slash if a leaf exists for that path | |||
value.tsr = path == "/" || | |||
(len(prefix) == len(path)+1 && n.handlers != nil) | |||
(len(prefix) == len(path)+1 && path == prefix[:len(prefix) - 1] && n.handlers != nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I run go test
without any problems
The latest benchmarks:
The latest benchstat report: name old time/op new time/op delta
Gin_Param 60.9ns ± 0% 61.4ns ± 1% +0.87% (p=0.000 n=9+9)
Gin_Param5 112ns ± 0% 116ns ± 2% +3.66% (p=0.000 n=8+9)
Gin_Param20 301ns ± 0% 305ns ± 1% +1.60% (p=0.000 n=8+9)
Gin_ParamWrite 115ns ± 2% 116ns ± 0% ~ (p=0.115 n=10+8)
Gin_GithubStatic 75.9ns ± 2% 74.7ns ± 2% ~ (p=0.435 n=10+9)
Gin_GithubParam 131ns ± 2% 132ns ± 2% ~ (p=0.726 n=10+10)
Gin_GithubAll 27.1µs ± 1% 25.7µs ± 1% -5.32% (p=0.000 n=10+10)
Gin_GPlusStatic 57.4ns ± 0% 60.2ns ± 3% +4.87% (p=0.000 n=10+10)
Gin_GPlusParam 79.4ns ± 0% 92.9ns ± 4% +16.95% (p=0.000 n=10+10)
Gin_GPlus2Params 101ns ± 0% 117ns ± 1% +15.76% (p=0.000 n=10+10)
Gin_GPlusAll 1.13µs ± 0% 1.15µs ± 1% +1.81% (p=0.000 n=10+9)
Gin_ParseStatic 59.5ns ± 3% 58.0ns ± 2% ~ (p=0.195 n=10+8)
Gin_ParseParam 68.0ns ± 3% 65.8ns ± 1% ~ (p=0.076 n=10+9)
Gin_Parse2Params 83.4ns ± 2% 82.1ns ± 1% ~ (p=0.194 n=10+8)
Gin_ParseAll 1.98µs ± 1% 1.96µs ± 2% ~ (p=0.137 n=10+10)
Gin_StaticAll 19.2µs ± 1% 18.5µs ± 1% -3.50% (p=0.000 n=10+10) |
Hi, @qm012 I modified the code based on the codereview comments and re-tested the benchmark ~ The latest benchmarks:
The latest benchstat report: name old time/op new time/op delta
Gin_Param 60.9ns ± 0% 60.4ns ± 3% ~ (p=0.151 n=9+10)
Gin_Param5 112ns ± 0% 115ns ± 1% +2.39% (p=0.000 n=8+10)
Gin_Param20 301ns ± 0% 303ns ± 1% +0.86% (p=0.000 n=8+9)
Gin_ParamWrite 115ns ± 2% 116ns ± 2% ~ (p=0.085 n=10+10)
Gin_GithubStatic 75.9ns ± 2% 74.2ns ± 1% ~ (p=0.089 n=10+10)
Gin_GithubParam 131ns ± 2% 128ns ± 0% -2.24% (p=0.000 n=10+8)
Gin_GithubAll 27.1µs ± 1% 25.8µs ± 1% -4.66% (p=0.000 n=10+10)
Gin_GPlusStatic 57.4ns ± 0% 56.8ns ± 1% -1.06% (p=0.000 n=10+8)
Gin_GPlusParam 79.4ns ± 0% 87.7ns ± 0% +10.39% (p=0.000 n=10+9)
Gin_GPlus2Params 101ns ± 0% 114ns ± 2% +12.79% (p=0.000 n=10+10)
Gin_GPlusAll 1.13µs ± 0% 1.13µs ± 1% +0.64% (p=0.023 n=10+10)
Gin_ParseStatic 59.5ns ± 3% 58.0ns ± 2% ~ (p=0.239 n=10+10)
Gin_ParseParam 68.0ns ± 3% 65.5ns ± 1% -3.66% (p=0.000 n=10+9)
Gin_Parse2Params 83.4ns ± 2% 81.3ns ± 0% -2.51% (p=0.000 n=10+10)
Gin_ParseAll 1.98µs ± 1% 1.95µs ± 1% -1.61% (p=0.000 n=10+10)
Gin_StaticAll 19.2µs ± 1% 18.6µs ± 2% -2.87% (p=0.000 n=10+10) |
Codecov Report
@@ Coverage Diff @@
## master #2847 +/- ##
=======================================
Coverage 98.73% 98.73%
=======================================
Files 41 41
Lines 3079 3080 +1
=======================================
+ Hits 3040 3041 +1
Misses 27 27
Partials 12 12
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
hi @qm012 If you have time, please help. Code review of my pr |
@appleboy @rw-access If you have time, please help. Code review of my pr |
Hi, the logic in |
Hi, @Bisstocuz This is the result of my retest The latest benchmarks:
The latest benchstat report: name old time/op new time/op delta
Gin_Param 62.2ns ± 5% 61.5ns ± 3% ~ (p=0.436 n=10+10)
Gin_Param5 114ns ± 2% 119ns ± 0% +4.17% (p=0.000 n=10+9)
Gin_Param20 304ns ± 2% 301ns ± 0% -0.98% (p=0.003 n=10+9)
Gin_ParamWrite 115ns ± 3% 116ns ± 3% ~ (p=0.366 n=10+9)
Gin_GithubStatic 76.0ns ± 0% 74.7ns ± 1% -1.72% (p=0.000 n=9+9)
Gin_GithubParam 132ns ± 1% 135ns ± 1% +2.38% (p=0.000 n=10+10)
Gin_GithubAll 27.5µs ± 1% 25.9µs ± 0% -5.83% (p=0.000 n=8+10)
Gin_GPlusStatic 58.6ns ± 3% 61.1ns ± 0% +4.29% (p=0.000 n=10+10)
Gin_GPlusParam 79.7ns ± 3% 88.3ns ± 0% +10.69% (p=0.000 n=9+10)
Gin_GPlus2Params 103ns ± 0% 115ns ± 0% +11.46% (p=0.000 n=9+9)
Gin_GPlusAll 1.15µs ± 1% 1.14µs ± 0% -0.69% (p=0.000 n=10+8)
Gin_ParseStatic 59.1ns ± 0% 62.0ns ± 1% +5.01% (p=0.000 n=10+10)
Gin_ParseParam 66.5ns ± 0% 69.3ns ± 0% +4.34% (p=0.000 n=10+10)
Gin_Parse2Params 81.8ns ± 0% 85.1ns ± 0% +3.99% (p=0.000 n=10+10)
Gin_ParseAll 1.95µs ± 1% 1.96µs ± 0% +0.40% (p=0.005 n=10+10)
Gin_StaticAll 19.2µs ± 1% 18.9µs ± 0% -1.51% (p=0.000 n=10+10) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@thinkerou help to review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
(cherry picked from commit 1c2aa59)
(cherry picked from commit 1c2aa59)
(cherry picked from commit 1c2aa59)
(cherry picked from commit 1c2aa59)
fix bug #2843