Skip to content

Commit 7f44549

Browse files
committed
Separate handling for pre middleware to improve performance
Signed-off-by: Vishal Rana <[email protected]>
1 parent f4dde46 commit 7f44549

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

echo.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -557,24 +557,31 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
557557
c := e.pool.Get().(*context)
558558
c.Reset(r, w)
559559

560-
// Middleware
561-
h := func(c Context) error {
562-
method := r.Method
563-
path := r.URL.RawPath
564-
if path == "" {
565-
path = r.URL.Path
566-
}
560+
method := r.Method
561+
path := r.URL.RawPath
562+
if path == "" {
563+
path = r.URL.Path
564+
}
565+
h := NotFoundHandler
566+
567+
if e.premiddleware == nil {
567568
e.router.Find(method, path, c)
568-
h := c.Handler()
569+
h = c.Handler()
569570
for i := len(e.middleware) - 1; i >= 0; i-- {
570571
h = e.middleware[i](h)
571572
}
572-
return h(c)
573-
}
574-
575-
// Premiddleware
576-
for i := len(e.premiddleware) - 1; i >= 0; i-- {
577-
h = e.premiddleware[i](h)
573+
} else {
574+
h = func(c Context) error {
575+
e.router.Find(method, path, c)
576+
h := c.Handler()
577+
for i := len(e.middleware) - 1; i >= 0; i-- {
578+
h = e.middleware[i](h)
579+
}
580+
return h(c)
581+
}
582+
for i := len(e.premiddleware) - 1; i >= 0; i-- {
583+
h = e.premiddleware[i](h)
584+
}
578585
}
579586

580587
// Execute chain

0 commit comments

Comments
 (0)