Skip to content

Commit

Permalink
🕜 perf: Reduce priority for any route
Browse files Browse the repository at this point in the history
  • Loading branch information
sohaha committed Aug 7, 2024
1 parent 8f9b429 commit 8fdfb3d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
8 changes: 2 additions & 6 deletions znet/bind_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (e *Engine) BindStruct(prefix string, s interface{}, handle ...Handler) err
return nil
}
path, method, key := "", "", ""
methods := `ANY|GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS`
methods := strings.Join(methodsKeys, "|")
regex := `^(?i)(` + methods + `)(.*)$`
info, err := zstring.RegexExtract(regex, m.Name)
infoLen := len(info)
Expand Down Expand Up @@ -118,11 +118,7 @@ func (e *Engine) BindStruct(prefix string, s interface{}, handle ...Handler) err
ok bool
)

if method == "ANY" {
p, l, ok = g.handleAny(path, Utils.ParseHandlerFunc(fn), nil, nil)
} else {
p, l, ok = g.addHandle(method, path, Utils.ParseHandlerFunc(fn), nil, nil)
}
p, l, ok = g.addHandle(method, path, Utils.ParseHandlerFunc(fn), nil, nil)

if ok && e.IsDebug() {
f := fmt.Sprintf("%%s %%-40s -> %s (%d handlers)", handleName, l)
Expand Down
13 changes: 7 additions & 6 deletions znet/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package znet
import (
"fmt"
"html/template"
"net/http"
"reflect"
"runtime"

Expand All @@ -22,17 +23,17 @@ func routeLog(log *zlog.Logger, tf, method, path string) string {
}

switch method {
case "GET":
case http.MethodGet:
method = log.ColorTextWrap(zlog.ColorLightCyan, mtd)
case "POST":
case http.MethodPost:
method = log.ColorTextWrap(zlog.ColorLightBlue, mtd)
case "PUT":
case http.MethodPut:
method = log.ColorTextWrap(zlog.ColorLightGreen, mtd)
case "DELETE":
case http.MethodDelete:
method = log.ColorTextWrap(zlog.ColorRed, mtd)
case "ANY":
case anyMethod:
method = log.ColorTextWrap(zlog.ColorLightMagenta, mtd)
case "OPTIONS":
case http.MethodOptions:
method = log.ColorTextWrap(zlog.ColorLightMagenta, mtd)
case "FILE":
method = log.ColorTextWrap(zlog.ColorLightMagenta, mtd)
Expand Down
46 changes: 23 additions & 23 deletions znet/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/sohaha/zlsgo/zfile"
)

const anyMethod = "ANY"

var (
// ErrGenerateParameters is returned when generating a route withRequestLog wrong parameters.
ErrGenerateParameters = errors.New("params contains wrong parameters")
Expand All @@ -34,9 +36,17 @@ var (
http.MethodOptions: {},
http.MethodConnect: {},
http.MethodTrace: {},
anyMethod: {},
}
methodsKeys = make([]string, 0, len(methods))
)

func init() {
for k := range methods {
methodsKeys = append(methodsKeys, k)
}
}

type (
// contextKeyType Private Value Structure for Each Request
contextKeyType struct{}
Expand Down Expand Up @@ -112,14 +122,7 @@ func (e *Engine) StaticFile(relativePath, filepath string) {
}

func (e *Engine) Any(path string, action Handler, moreHandler ...Handler) *Engine {
middleware, firstMiddleware := handlerFuncs(moreHandler)
_, l, ok := e.handleAny(path, Utils.ParseHandlerFunc(action), middleware, firstMiddleware)

if ok {
routeAddLog(e, "ANY", Utils.CompletionPath(path, e.router.prefix), action, l)
}

return e
return e.Handle(anyMethod, path, action, moreHandler...)
}

func (e *Engine) Customize(method, path string, action Handler, moreHandler ...Handler) *Engine {
Expand Down Expand Up @@ -385,16 +388,6 @@ func (e *Engine) addHandle(method string, path string, handle handlerFn, beforeh
return path, len(middleware) + 1, true
}

func (e *Engine) handleAny(path string, handle handlerFn, beforehandle []handlerFn, moreHandler []handlerFn) (p string, l int, ok bool) {
for key := range methods {
p, l, ok = e.addHandle(key, path, handle, beforehandle, moreHandler)
if !ok {
return p, l, false
}
}
return
}

func (e *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
p := req.URL.Path
if !e.ShowFavicon && p == "/favicon.ico" {
Expand Down Expand Up @@ -436,23 +429,30 @@ func (e *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}

if _, ok := e.router.trees[req.Method]; !ok {
e.handleNotFound(c)
return
}

if e.FindHandle(c, req, p, true) {
e.handleNotFound(c)
}
}

func (e *Engine) FindHandle(rw *Context, req *http.Request, requestURL string, applyMiddleware bool) (not bool) {
var anyTrees bool
t, ok := e.router.trees[req.Method]
if !ok {
t, ok = e.router.trees[anyMethod]
anyTrees = true
}
if !ok {
return true
}

handler, middleware, ok := Utils.TreeFind(t, requestURL)
if !ok && !anyTrees {
t, ok = e.router.trees[anyMethod]
if ok {
handler, middleware, ok = Utils.TreeFind(t, requestURL)
}
}

if !ok {
return true
}
Expand Down

0 comments on commit 8fdfb3d

Please sign in to comment.