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

v3: fix benchmark results related to handler, next #2130

Merged
merged 1 commit into from
Oct 10, 2022
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
14 changes: 12 additions & 2 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,16 +728,26 @@ func (c *DefaultCtx) Next() (err error) {
err = c.route.Handlers[c.indexHandler](c)
} else {
// Continue handler stack
_, err = c.app.next(c, c.app.newCtxFunc != nil)
if c.app.newCtxFunc != nil {
_, err = c.app.nextCustom(c)
} else {
_, err = c.app.next(c)
}
}
return err
}

// RestartRouting instead of going to the next handler. This may be usefull after
// changing the request path. Note that handlers might be executed again.
func (c *DefaultCtx) RestartRouting() error {
var err error

c.indexRoute = -1
_, err := c.app.next(c, c.app.newCtxFunc != nil)
if c.app.newCtxFunc != nil {
_, err = c.app.nextCustom(c)
} else {
_, err = c.app.next(c)
}
return err
}

Expand Down
63 changes: 55 additions & 8 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (r *Route) match(detectionPath, path string, params *[maxParams]string) (ma
return false
}

func (app *App) next(c CustomCtx, customCtx bool) (match bool, err error) {
func (app *App) nextCustom(c CustomCtx) (match bool, err error) {
// Get stack length
tree, ok := app.treeStack[c.getMethodINT()][c.getTreePath()]
if !ok {
Expand Down Expand Up @@ -136,22 +136,64 @@ func (app *App) next(c CustomCtx, customCtx bool) (match bool, err error) {
// If c.Next() does not match, return 404
err = NewError(StatusNotFound, "Cannot "+c.Method()+" "+c.getPathOriginal())

var isMethodExist bool
if customCtx {
isMethodExist = methodExistCustom(c)
} else {
isMethodExist = methodExist(c.(*DefaultCtx))
// If no match, scan stack again if other methods match the request
// Moved from app.handler because middleware may break the route chain
if !c.getMatched() && methodExistCustom(c) {
err = ErrMethodNotAllowed
}
return
}

func (app *App) next(c *DefaultCtx) (match bool, err error) {
// Get stack length
tree, ok := app.treeStack[c.methodINT][c.treePath]
if !ok {
tree = app.treeStack[c.methodINT][""]
}
lenr := len(tree) - 1

// Loop over the route stack starting from previous index
for c.indexRoute < lenr {
// Increment route index
c.indexRoute++

// Get *Route
route := tree[c.indexRoute]

// Check if it matches the request path
match = route.match(c.detectionPath, c.path, &c.values)

// No match, next route
if !match {
continue
}
// Pass route reference and param values
c.route = route

// Non use handler matched
if !c.matched && !route.use {
c.matched = true
}

// Execute first handler of route
c.indexHandler = 0
err = route.Handlers[0](c)
return match, err // Stop scanning the stack
}

// If c.Next() does not match, return 404
err = NewError(StatusNotFound, "Cannot "+c.method+" "+c.pathOriginal)

// If no match, scan stack again if other methods match the request
// Moved from app.handler because middleware may break the route chain
if !c.getMatched() && isMethodExist {
if !c.matched && methodExist(c) {
err = ErrMethodNotAllowed
}
return
}

func (app *App) handler(rctx *fasthttp.RequestCtx) {
// Handler for default ctxs
var c CustomCtx
if app.newCtxFunc != nil {
c = app.AcquireCtx().(CustomCtx)
Expand All @@ -173,7 +215,12 @@ func (app *App) handler(rctx *fasthttp.RequestCtx) {
}

// Find match in stack
_, err := app.next(c, app.newCtxFunc != nil)
var err error
if app.newCtxFunc != nil {
_, err = app.nextCustom(c)
} else {
_, err = app.next(c.(*DefaultCtx))
}
if err != nil {
if catch := c.App().ErrorHandler(c, err); catch != nil {
_ = c.SendStatus(StatusInternalServerError)
Expand Down
6 changes: 3 additions & 3 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ func Benchmark_Router_Next(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
c.indexRoute = -1
res, err = app.next(c, false)
res, err = app.next(c)
}
require.NoError(b, err)
require.True(b, res)
Expand Down Expand Up @@ -772,10 +772,10 @@ func Benchmark_Router_Github_API(b *testing.B) {
for n := 0; n < b.N; n++ {
c.URI().SetPath(routesFixture.TestRoutes[i].Path)

ctx := app.AcquireCtx().(CustomCtx)
ctx := app.AcquireCtx().(*DefaultCtx)
ctx.Reset(c)

match, err = app.next(ctx, false)
match, err = app.next(ctx)
app.ReleaseCtx(ctx)
}

Expand Down