1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 23:01:28 +00:00

Improve performance

This commit is contained in:
astaxie 2013-08-11 23:14:30 +08:00
parent ca1354e77f
commit 1f3ae3d682

277
router.go
View File

@ -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,52 +346,48 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
r.ParseMultipartForm(MaxMemory) r.ParseMultipartForm(MaxMemory)
//user defined Handler //user defined Handler
for pattern, c := range p.userHandlers { if p.enableUser {
if c.regex == nil && pattern == requestPath { for pattern, c := range p.userHandlers {
if c.regex == nil && pattern == requestPath {
c.h.ServeHTTP(rw, r)
return
} else if c.regex == nil {
continue
}
//check if Route pattern matches url
if !c.regex.MatchString(requestPath) {
continue
}
//get submatches (params)
matches := c.regex.FindStringSubmatch(requestPath)
//double check that the Route matches the URL pattern.
if len(matches[0]) != len(requestPath) {
continue
}
if len(c.params) > 0 {
//add url parameters to the query param map
values := r.URL.Query()
for i, match := range matches[1:] {
values.Add(c.params[i], match)
r.Form.Add(c.params[i], match)
params[c.params[i]] = match
}
//reassemble query params and add to RawQuery
r.URL.RawQuery = url.Values(values).Encode() + "&" + r.URL.RawQuery
//r.URL.RawQuery = url.Values(values).Encode()
}
c.h.ServeHTTP(rw, r) c.h.ServeHTTP(rw, r)
return return
} else if c.regex == nil {
continue
} }
//check if Route pattern matches url
if !c.regex.MatchString(requestPath) {
continue
}
//get submatches (params)
matches := c.regex.FindStringSubmatch(requestPath)
//double check that the Route matches the URL pattern.
if len(matches[0]) != len(requestPath) {
continue
}
if len(c.params) > 0 {
//add url parameters to the query param map
values := r.URL.Query()
for i, match := range matches[1:] {
values.Add(c.params[i], match)
r.Form.Add(c.params[i], match)
params[c.params[i]] = match
}
//reassemble query params and add to RawQuery
r.URL.RawQuery = url.Values(values).Encode() + "&" + r.URL.RawQuery
//r.URL.RawQuery = url.Values(values).Encode()
}
c.h.ServeHTTP(rw, r)
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,64 +477,93 @@ 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 m, ok := runrouter.methods["get"]; ok { if runrouter.hasMethod {
method = vc.MethodByName(m) if m, ok := runrouter.methods["get"]; ok {
} else if m, ok = runrouter.methods["*"]; ok { method = vc.MethodByName(m)
method = vc.MethodByName(m) } else if m, ok = runrouter.methods["*"]; ok {
method = vc.MethodByName(m)
} else {
method = vc.MethodByName("Get")
}
} else { } else {
method = vc.MethodByName("Get") method = vc.MethodByName("Get")
} }
method.Call(in) method.Call(in)
} else if r.Method == "HEAD" { } else if r.Method == "HEAD" {
if m, ok := runrouter.methods["head"]; ok { if runrouter.hasMethod {
method = vc.MethodByName(m) if m, ok := runrouter.methods["head"]; ok {
} else if m, ok = runrouter.methods["*"]; ok { method = vc.MethodByName(m)
method = vc.MethodByName(m) } else if m, ok = runrouter.methods["*"]; ok {
method = vc.MethodByName(m)
} else {
method = vc.MethodByName("Head")
}
} else { } else {
method = vc.MethodByName("Head") 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 m, ok := runrouter.methods["delete"]; ok { if runrouter.hasMethod {
method = vc.MethodByName(m) if m, ok := runrouter.methods["delete"]; ok {
} else if m, ok = runrouter.methods["*"]; ok { method = vc.MethodByName(m)
method = vc.MethodByName(m) } else if m, ok = runrouter.methods["*"]; ok {
method = vc.MethodByName(m)
} else {
method = vc.MethodByName("Delete")
}
} else { } else {
method = vc.MethodByName("Delete") 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 m, ok := runrouter.methods["put"]; ok { if runrouter.hasMethod {
method = vc.MethodByName(m) if m, ok := runrouter.methods["put"]; ok {
} else if m, ok = runrouter.methods["*"]; ok { method = vc.MethodByName(m)
method = vc.MethodByName(m) } else if m, ok = runrouter.methods["*"]; ok {
method = vc.MethodByName(m)
} else {
method = vc.MethodByName("Put")
}
} else { } else {
method = vc.MethodByName("Put") method = vc.MethodByName("Put")
} }
method.Call(in) method.Call(in)
} else if r.Method == "POST" { } else if r.Method == "POST" {
if m, ok := runrouter.methods["post"]; ok { if runrouter.hasMethod {
method = vc.MethodByName(m) if m, ok := runrouter.methods["post"]; ok {
} else if m, ok = runrouter.methods["*"]; ok { method = vc.MethodByName(m)
method = vc.MethodByName(m) } else if m, ok = runrouter.methods["*"]; ok {
method = vc.MethodByName(m)
} else {
method = vc.MethodByName("Post")
}
} else { } else {
method = vc.MethodByName("Post") method = vc.MethodByName("Post")
} }
method.Call(in) method.Call(in)
} else if r.Method == "PATCH" { } else if r.Method == "PATCH" {
if m, ok := runrouter.methods["patch"]; ok { if runrouter.hasMethod {
method = vc.MethodByName(m) if m, ok := runrouter.methods["patch"]; ok {
} else if m, ok = runrouter.methods["*"]; ok { method = vc.MethodByName(m)
method = vc.MethodByName(m) } else if m, ok = runrouter.methods["*"]; ok {
method = vc.MethodByName(m)
} else {
method = vc.MethodByName("Patch")
}
} else { } else {
method = vc.MethodByName("Patch") method = vc.MethodByName("Patch")
} }
method.Call(in) method.Call(in)
} else if r.Method == "OPTIONS" { } else if r.Method == "OPTIONS" {
if m, ok := runrouter.methods["options"]; ok { if runrouter.hasMethod {
method = vc.MethodByName(m) if m, ok := runrouter.methods["options"]; ok {
} else if m, ok = runrouter.methods["*"]; ok { method = vc.MethodByName(m)
method = vc.MethodByName(m) } else if m, ok = runrouter.methods["*"]; ok {
method = vc.MethodByName(m)
} else {
method = vc.MethodByName("Options")
}
} else { } else {
method = vc.MethodByName("Options") method = vc.MethodByName("Options")
} }
@ -553,72 +593,75 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
//start autorouter //start autorouter
if !findrouter { if p.enableAuto {
for cName, methodmap := range p.autoRouter { if !findrouter {
for cName, methodmap := range p.autoRouter {
if strings.ToLower(requestPath) == "/"+cName { if strings.ToLower(requestPath) == "/"+cName {
http.Redirect(w, r, requestPath+"/", 301) http.Redirect(w, r, requestPath+"/", 301)
return return
} }
if strings.ToLower(requestPath) == "/"+cName+"/" { if strings.ToLower(requestPath) == "/"+cName+"/" {
requestPath = requestPath + "index" requestPath = requestPath + "index"
} }
if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/") { if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/") {
for mName, controllerType := range methodmap { for mName, controllerType := range methodmap {
if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/"+strings.ToLower(mName)) { if strings.HasPrefix(strings.ToLower(requestPath), "/"+cName+"/"+strings.ToLower(mName)) {
//parse params //parse params
otherurl := requestPath[len("/"+cName+"/"+strings.ToLower(mName)):] otherurl := requestPath[len("/"+cName+"/"+strings.ToLower(mName)):]
if len(otherurl) > 1 { if len(otherurl) > 1 {
plist := strings.Split(otherurl, "/") plist := strings.Split(otherurl, "/")
for k, v := range plist[1:] { for k, v := range plist[1:] {
params[strconv.Itoa(k)] = v params[strconv.Itoa(k)] = v
}
} }
} //Invoke the request handler
//Invoke the request handler vc := reflect.New(controllerType)
vc := reflect.New(controllerType)
//call the controller init function //call the controller init function
init := vc.MethodByName("Init") init := vc.MethodByName("Init")
in := make([]reflect.Value, 2) in := make([]reflect.Value, 2)
ct := &Context{ResponseWriter: w, Request: r, Params: params, RequestBody: requestbody} ct := &Context{ResponseWriter: w, Request: r, Params: params, RequestBody: requestbody}
in[0] = reflect.ValueOf(ct) in[0] = reflect.ValueOf(ct)
in[1] = reflect.ValueOf(controllerType.Name()) in[1] = reflect.ValueOf(controllerType.Name())
init.Call(in) init.Call(in)
//call prepare function //call prepare function
in = make([]reflect.Value, 0) in = make([]reflect.Value, 0)
method := vc.MethodByName("Prepare") method := vc.MethodByName("Prepare")
method.Call(in)
method = vc.MethodByName(mName)
method.Call(in)
//if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
if EnableXSRF {
method = vc.MethodByName("XsrfToken")
method.Call(in) method.Call(in)
if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" || method = vc.MethodByName(mName)
(r.Method == "POST" && (r.Form.Get("_method") == "delete" || r.Form.Get("_method") == "put")) { method.Call(in)
method = vc.MethodByName("CheckXsrfCookie") //if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
if EnableXSRF {
method = vc.MethodByName("XsrfToken")
method.Call(in) method.Call(in)
if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" ||
(r.Method == "POST" && (r.Form.Get("_method") == "delete" || r.Form.Get("_method") == "put")) {
method = vc.MethodByName("CheckXsrfCookie")
method.Call(in)
}
} }
} if !w.started {
if !w.started { if AutoRender {
if AutoRender { method = vc.MethodByName("Render")
method = vc.MethodByName("Render") method.Call(in)
method.Call(in) }
} }
method = vc.MethodByName("Finish")
method.Call(in)
method = vc.MethodByName("Destructor")
method.Call(in)
// set find
findrouter = true
} }
method = vc.MethodByName("Finish")
method.Call(in)
method = vc.MethodByName("Destructor")
method.Call(in)
// set find
findrouter = true
} }
} }
} }
} }
} }
//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 {