mirror of https://github.com/gofiber/fiber.git
v3: Improve and simplify logic of ctx.Next() (#3063)
* Simplify Next() handler in Ctx * Add commentspull/3066/head
parent
dfdf9647e1
commit
c579a1a0b3
15
ctx.go
15
ctx.go
|
@ -1023,19 +1023,20 @@ func (c *DefaultCtx) ClientHelloInfo() *tls.ClientHelloInfo {
|
||||||
func (c *DefaultCtx) Next() error {
|
func (c *DefaultCtx) Next() error {
|
||||||
// Increment handler index
|
// Increment handler index
|
||||||
c.indexHandler++
|
c.indexHandler++
|
||||||
var err error
|
|
||||||
// Did we execute all route handlers?
|
// Did we execute all route handlers?
|
||||||
if c.indexHandler < len(c.route.Handlers) {
|
if c.indexHandler < len(c.route.Handlers) {
|
||||||
// Continue route stack
|
// Continue route stack
|
||||||
err = c.route.Handlers[c.indexHandler](c)
|
return c.route.Handlers[c.indexHandler](c)
|
||||||
} else {
|
}
|
||||||
|
|
||||||
// Continue handler stack
|
// Continue handler stack
|
||||||
if c.app.newCtxFunc != nil {
|
if c.app.newCtxFunc != nil {
|
||||||
_, err = c.app.nextCustom(c)
|
_, err := c.app.nextCustom(c)
|
||||||
} else {
|
return err
|
||||||
_, err = c.app.next(c)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, err := c.app.next(c)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -542,6 +542,27 @@ func Benchmark_Router_Next(b *testing.B) {
|
||||||
require.Equal(b, 4, c.indexRoute)
|
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
|
// go test -v ./... -run=^$ -bench=Benchmark_Route_Match -benchmem -count=4
|
||||||
func Benchmark_Route_Match(b *testing.B) {
|
func Benchmark_Route_Match(b *testing.B) {
|
||||||
var match bool
|
var match bool
|
||||||
|
|
Loading…
Reference in New Issue