mirror of
https://github.com/astaxie/beego.git
synced 2024-11-23 00:00:54 +00:00
Improve performance
This commit is contained in:
parent
ca1354e77f
commit
1f3ae3d682
55
router.go
55
router.go
@ -22,6 +22,7 @@ type controllerInfo struct {
|
|||||||
params map[int]string
|
params map[int]string
|
||||||
controllerType reflect.Type
|
controllerType reflect.Type
|
||||||
methods map[string]string
|
methods map[string]string
|
||||||
|
hasMethod bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type userHandler struct {
|
type userHandler struct {
|
||||||
@ -34,8 +35,12 @@ type userHandler struct {
|
|||||||
type ControllerRegistor struct {
|
type ControllerRegistor struct {
|
||||||
routers []*controllerInfo
|
routers []*controllerInfo
|
||||||
fixrouters []*controllerInfo
|
fixrouters []*controllerInfo
|
||||||
|
enableFilter bool
|
||||||
filters []http.HandlerFunc
|
filters []http.HandlerFunc
|
||||||
|
afterFilters []http.HandlerFunc
|
||||||
|
enableUser bool
|
||||||
userHandlers map[string]*userHandler
|
userHandlers map[string]*userHandler
|
||||||
|
enableAuto bool
|
||||||
autoRouter map[string]map[string]reflect.Type //key:controller key:method value:reflect.type
|
autoRouter map[string]map[string]reflect.Type //key:controller key:method value:reflect.type
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,6 +135,9 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
|
|||||||
route.pattern = pattern
|
route.pattern = pattern
|
||||||
route.controllerType = t
|
route.controllerType = t
|
||||||
route.methods = methods
|
route.methods = methods
|
||||||
|
if len(methods) > 0 {
|
||||||
|
route.hasMethod = true
|
||||||
|
}
|
||||||
p.fixrouters = append(p.fixrouters, route)
|
p.fixrouters = append(p.fixrouters, route)
|
||||||
} else { // add regexp routers
|
} else { // add regexp routers
|
||||||
//recreate the url pattern, with parameters replaced
|
//recreate the url pattern, with parameters replaced
|
||||||
@ -149,12 +157,16 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
|
|||||||
route.params = params
|
route.params = params
|
||||||
route.pattern = pattern
|
route.pattern = pattern
|
||||||
route.methods = methods
|
route.methods = methods
|
||||||
|
if len(methods) > 0 {
|
||||||
|
route.hasMethod = true
|
||||||
|
}
|
||||||
route.controllerType = t
|
route.controllerType = t
|
||||||
p.routers = append(p.routers, route)
|
p.routers = append(p.routers, route)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
|
func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
|
||||||
|
p.enableAuto = true
|
||||||
reflectVal := reflect.ValueOf(c)
|
reflectVal := reflect.ValueOf(c)
|
||||||
rt := reflectVal.Type()
|
rt := reflectVal.Type()
|
||||||
ct := reflect.Indirect(reflectVal).Type()
|
ct := reflect.Indirect(reflectVal).Type()
|
||||||
@ -170,6 +182,7 @@ func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *ControllerRegistor) AddHandler(pattern string, c http.Handler) {
|
func (p *ControllerRegistor) AddHandler(pattern string, c http.Handler) {
|
||||||
|
p.enableUser = true
|
||||||
parts := strings.Split(pattern, "/")
|
parts := strings.Split(pattern, "/")
|
||||||
|
|
||||||
j := 0
|
j := 0
|
||||||
@ -217,6 +230,7 @@ func (p *ControllerRegistor) AddHandler(pattern string, c http.Handler) {
|
|||||||
|
|
||||||
// Filter adds the middleware filter.
|
// Filter adds the middleware filter.
|
||||||
func (p *ControllerRegistor) Filter(filter http.HandlerFunc) {
|
func (p *ControllerRegistor) Filter(filter http.HandlerFunc) {
|
||||||
|
p.enableFilter = true
|
||||||
p.filters = append(p.filters, filter)
|
p.filters = append(p.filters, filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -332,6 +346,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
r.ParseMultipartForm(MaxMemory)
|
r.ParseMultipartForm(MaxMemory)
|
||||||
|
|
||||||
//user defined Handler
|
//user defined Handler
|
||||||
|
if p.enableUser {
|
||||||
for pattern, c := range p.userHandlers {
|
for pattern, c := range p.userHandlers {
|
||||||
if c.regex == nil && pattern == requestPath {
|
if c.regex == nil && pattern == requestPath {
|
||||||
c.h.ServeHTTP(rw, r)
|
c.h.ServeHTTP(rw, r)
|
||||||
@ -368,16 +383,11 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
c.h.ServeHTTP(rw, r)
|
c.h.ServeHTTP(rw, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//first find path from the fixrouters to Improve Performance
|
//first find path from the fixrouters to Improve Performance
|
||||||
for _, route := range p.fixrouters {
|
for _, route := range p.fixrouters {
|
||||||
n := len(requestPath)
|
n := len(requestPath)
|
||||||
//route like "/"
|
|
||||||
//if n == 1 {
|
|
||||||
// else {
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
if requestPath == route.pattern {
|
if requestPath == route.pattern {
|
||||||
runrouter = route
|
runrouter = route
|
||||||
findrouter = true
|
findrouter = true
|
||||||
@ -392,6 +402,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//find regex's router
|
||||||
if !findrouter {
|
if !findrouter {
|
||||||
//find a matching Route
|
//find a matching Route
|
||||||
for _, route := range p.routers {
|
for _, route := range p.routers {
|
||||||
@ -466,6 +477,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
//if response has written,yes don't run next
|
//if response has written,yes don't run next
|
||||||
if !w.started {
|
if !w.started {
|
||||||
if r.Method == "GET" {
|
if r.Method == "GET" {
|
||||||
|
if runrouter.hasMethod {
|
||||||
if m, ok := runrouter.methods["get"]; ok {
|
if m, ok := runrouter.methods["get"]; ok {
|
||||||
method = vc.MethodByName(m)
|
method = vc.MethodByName(m)
|
||||||
} else if m, ok = runrouter.methods["*"]; ok {
|
} else if m, ok = runrouter.methods["*"]; ok {
|
||||||
@ -473,8 +485,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
} else {
|
} else {
|
||||||
method = vc.MethodByName("Get")
|
method = vc.MethodByName("Get")
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
method = vc.MethodByName("Get")
|
||||||
|
}
|
||||||
method.Call(in)
|
method.Call(in)
|
||||||
} else if r.Method == "HEAD" {
|
} else if r.Method == "HEAD" {
|
||||||
|
if runrouter.hasMethod {
|
||||||
if m, ok := runrouter.methods["head"]; ok {
|
if m, ok := runrouter.methods["head"]; ok {
|
||||||
method = vc.MethodByName(m)
|
method = vc.MethodByName(m)
|
||||||
} else if m, ok = runrouter.methods["*"]; ok {
|
} else if m, ok = runrouter.methods["*"]; ok {
|
||||||
@ -482,8 +498,13 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
} else {
|
} else {
|
||||||
method = vc.MethodByName("Head")
|
method = vc.MethodByName("Head")
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
method = vc.MethodByName("Head")
|
||||||
|
}
|
||||||
|
|
||||||
method.Call(in)
|
method.Call(in)
|
||||||
} else if r.Method == "DELETE" || (r.Method == "POST" && r.Form.Get("_method") == "delete") {
|
} else if r.Method == "DELETE" || (r.Method == "POST" && r.Form.Get("_method") == "delete") {
|
||||||
|
if runrouter.hasMethod {
|
||||||
if m, ok := runrouter.methods["delete"]; ok {
|
if m, ok := runrouter.methods["delete"]; ok {
|
||||||
method = vc.MethodByName(m)
|
method = vc.MethodByName(m)
|
||||||
} else if m, ok = runrouter.methods["*"]; ok {
|
} else if m, ok = runrouter.methods["*"]; ok {
|
||||||
@ -491,8 +512,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
} else {
|
} else {
|
||||||
method = vc.MethodByName("Delete")
|
method = vc.MethodByName("Delete")
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
method = vc.MethodByName("Delete")
|
||||||
|
}
|
||||||
method.Call(in)
|
method.Call(in)
|
||||||
} else if r.Method == "PUT" || (r.Method == "POST" && r.Form.Get("_method") == "put") {
|
} else if r.Method == "PUT" || (r.Method == "POST" && r.Form.Get("_method") == "put") {
|
||||||
|
if runrouter.hasMethod {
|
||||||
if m, ok := runrouter.methods["put"]; ok {
|
if m, ok := runrouter.methods["put"]; ok {
|
||||||
method = vc.MethodByName(m)
|
method = vc.MethodByName(m)
|
||||||
} else if m, ok = runrouter.methods["*"]; ok {
|
} else if m, ok = runrouter.methods["*"]; ok {
|
||||||
@ -500,8 +525,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
} else {
|
} else {
|
||||||
method = vc.MethodByName("Put")
|
method = vc.MethodByName("Put")
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
method = vc.MethodByName("Put")
|
||||||
|
}
|
||||||
method.Call(in)
|
method.Call(in)
|
||||||
} else if r.Method == "POST" {
|
} else if r.Method == "POST" {
|
||||||
|
if runrouter.hasMethod {
|
||||||
if m, ok := runrouter.methods["post"]; ok {
|
if m, ok := runrouter.methods["post"]; ok {
|
||||||
method = vc.MethodByName(m)
|
method = vc.MethodByName(m)
|
||||||
} else if m, ok = runrouter.methods["*"]; ok {
|
} else if m, ok = runrouter.methods["*"]; ok {
|
||||||
@ -509,8 +538,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
} else {
|
} else {
|
||||||
method = vc.MethodByName("Post")
|
method = vc.MethodByName("Post")
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
method = vc.MethodByName("Post")
|
||||||
|
}
|
||||||
method.Call(in)
|
method.Call(in)
|
||||||
} else if r.Method == "PATCH" {
|
} else if r.Method == "PATCH" {
|
||||||
|
if runrouter.hasMethod {
|
||||||
if m, ok := runrouter.methods["patch"]; ok {
|
if m, ok := runrouter.methods["patch"]; ok {
|
||||||
method = vc.MethodByName(m)
|
method = vc.MethodByName(m)
|
||||||
} else if m, ok = runrouter.methods["*"]; ok {
|
} else if m, ok = runrouter.methods["*"]; ok {
|
||||||
@ -518,8 +551,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
} else {
|
} else {
|
||||||
method = vc.MethodByName("Patch")
|
method = vc.MethodByName("Patch")
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
method = vc.MethodByName("Patch")
|
||||||
|
}
|
||||||
method.Call(in)
|
method.Call(in)
|
||||||
} else if r.Method == "OPTIONS" {
|
} else if r.Method == "OPTIONS" {
|
||||||
|
if runrouter.hasMethod {
|
||||||
if m, ok := runrouter.methods["options"]; ok {
|
if m, ok := runrouter.methods["options"]; ok {
|
||||||
method = vc.MethodByName(m)
|
method = vc.MethodByName(m)
|
||||||
} else if m, ok = runrouter.methods["*"]; ok {
|
} else if m, ok = runrouter.methods["*"]; ok {
|
||||||
@ -527,6 +564,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
} else {
|
} else {
|
||||||
method = vc.MethodByName("Options")
|
method = vc.MethodByName("Options")
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
method = vc.MethodByName("Options")
|
||||||
|
}
|
||||||
method.Call(in)
|
method.Call(in)
|
||||||
}
|
}
|
||||||
gotofunc := vc.Elem().FieldByName("gotofunc").String()
|
gotofunc := vc.Elem().FieldByName("gotofunc").String()
|
||||||
@ -553,6 +593,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
|
|
||||||
//start autorouter
|
//start autorouter
|
||||||
|
|
||||||
|
if p.enableAuto {
|
||||||
if !findrouter {
|
if !findrouter {
|
||||||
for cName, methodmap := range p.autoRouter {
|
for cName, methodmap := range p.autoRouter {
|
||||||
|
|
||||||
@ -619,6 +660,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//if no matches to url, throw a not found exception
|
//if no matches to url, throw a not found exception
|
||||||
if !findrouter {
|
if !findrouter {
|
||||||
if h, ok := ErrorMaps["404"]; ok {
|
if h, ok := ErrorMaps["404"]; ok {
|
||||||
|
Loading…
Reference in New Issue
Block a user