Skip to content

Commit

Permalink
v3: Improve and simplify logic of ctx.Next() (#3063)
Browse files Browse the repository at this point in the history
* Simplify Next() handler in Ctx

* Add comments
  • Loading branch information
gaby authored Jul 5, 2024
1 parent dfdf964 commit c579a1a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
19 changes: 10 additions & 9 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,19 +1023,20 @@ func (c *DefaultCtx) ClientHelloInfo() *tls.ClientHelloInfo {
func (c *DefaultCtx) Next() error {
// Increment handler index
c.indexHandler++
var err error

// Did we execute all route handlers?
if c.indexHandler < len(c.route.Handlers) {
// Continue route stack
err = c.route.Handlers[c.indexHandler](c)
} else {
// Continue handler stack
if c.app.newCtxFunc != nil {
_, err = c.app.nextCustom(c)
} else {
_, err = c.app.next(c)
}
return c.route.Handlers[c.indexHandler](c)
}

// Continue handler stack
if c.app.newCtxFunc != nil {
_, err := c.app.nextCustom(c)
return err
}

_, err := c.app.next(c)
return err
}

Expand Down
21 changes: 21 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,27 @@ func Benchmark_Router_Next(b *testing.B) {
require.Equal(b, 4, c.indexRoute)
}

// go test -v ./... -run=^$ -bench=Benchmark_Router_Next_Default -benchmem -count=4
func Benchmark_Router_Next_Default(b *testing.B) {
app := New()
app.Get("/", func(_ Ctx) error {
return nil
})

h := app.Handler()

fctx := &fasthttp.RequestCtx{}
fctx.Request.Header.SetMethod(MethodGet)
fctx.Request.SetRequestURI("/")

b.ReportAllocs()
b.ResetTimer()

for n := 0; n < b.N; n++ {
h(fctx)
}
}

// go test -v ./... -run=^$ -bench=Benchmark_Route_Match -benchmem -count=4
func Benchmark_Route_Match(b *testing.B) {
var match bool
Expand Down

0 comments on commit c579a1a

Please sign in to comment.