1f3ae3d6 by astaxie

Improve performance

1 parent ca1354e7
Showing 1 changed file with 49 additions and 6 deletions
...@@ -22,6 +22,7 @@ type controllerInfo struct { ...@@ -22,6 +22,7 @@ type controllerInfo struct {
22 params map[int]string 22 params map[int]string
23 controllerType reflect.Type 23 controllerType reflect.Type
24 methods map[string]string 24 methods map[string]string
25 hasMethod bool
25 } 26 }
26 27
27 type userHandler struct { 28 type userHandler struct {
...@@ -34,8 +35,12 @@ type userHandler struct { ...@@ -34,8 +35,12 @@ type userHandler struct {
34 type ControllerRegistor struct { 35 type ControllerRegistor struct {
35 routers []*controllerInfo 36 routers []*controllerInfo
36 fixrouters []*controllerInfo 37 fixrouters []*controllerInfo
38 enableFilter bool
37 filters []http.HandlerFunc 39 filters []http.HandlerFunc
40 afterFilters []http.HandlerFunc
41 enableUser bool
38 userHandlers map[string]*userHandler 42 userHandlers map[string]*userHandler
43 enableAuto bool
39 autoRouter map[string]map[string]reflect.Type //key:controller key:method value:reflect.type 44 autoRouter map[string]map[string]reflect.Type //key:controller key:method value:reflect.type
40 } 45 }
41 46
...@@ -130,6 +135,9 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM ...@@ -130,6 +135,9 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
130 route.pattern = pattern 135 route.pattern = pattern
131 route.controllerType = t 136 route.controllerType = t
132 route.methods = methods 137 route.methods = methods
138 if len(methods) > 0 {
139 route.hasMethod = true
140 }
133 p.fixrouters = append(p.fixrouters, route) 141 p.fixrouters = append(p.fixrouters, route)
134 } else { // add regexp routers 142 } else { // add regexp routers
135 //recreate the url pattern, with parameters replaced 143 //recreate the url pattern, with parameters replaced
...@@ -149,12 +157,16 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM ...@@ -149,12 +157,16 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
149 route.params = params 157 route.params = params
150 route.pattern = pattern 158 route.pattern = pattern
151 route.methods = methods 159 route.methods = methods
160 if len(methods) > 0 {
161 route.hasMethod = true
162 }
152 route.controllerType = t 163 route.controllerType = t
153 p.routers = append(p.routers, route) 164 p.routers = append(p.routers, route)
154 } 165 }
155 } 166 }
156 167
157 func (p *ControllerRegistor) AddAuto(c ControllerInterface) { 168 func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
169 p.enableAuto = true
158 reflectVal := reflect.ValueOf(c) 170 reflectVal := reflect.ValueOf(c)
159 rt := reflectVal.Type() 171 rt := reflectVal.Type()
160 ct := reflect.Indirect(reflectVal).Type() 172 ct := reflect.Indirect(reflectVal).Type()
...@@ -170,6 +182,7 @@ func (p *ControllerRegistor) AddAuto(c ControllerInterface) { ...@@ -170,6 +182,7 @@ func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
170 } 182 }
171 183
172 func (p *ControllerRegistor) AddHandler(pattern string, c http.Handler) { 184 func (p *ControllerRegistor) AddHandler(pattern string, c http.Handler) {
185 p.enableUser = true
173 parts := strings.Split(pattern, "/") 186 parts := strings.Split(pattern, "/")
174 187
175 j := 0 188 j := 0
...@@ -217,6 +230,7 @@ func (p *ControllerRegistor) AddHandler(pattern string, c http.Handler) { ...@@ -217,6 +230,7 @@ func (p *ControllerRegistor) AddHandler(pattern string, c http.Handler) {
217 230
218 // Filter adds the middleware filter. 231 // Filter adds the middleware filter.
219 func (p *ControllerRegistor) Filter(filter http.HandlerFunc) { 232 func (p *ControllerRegistor) Filter(filter http.HandlerFunc) {
233 p.enableFilter = true
220 p.filters = append(p.filters, filter) 234 p.filters = append(p.filters, filter)
221 } 235 }
222 236
...@@ -332,6 +346,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -332,6 +346,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
332 r.ParseMultipartForm(MaxMemory) 346 r.ParseMultipartForm(MaxMemory)
333 347
334 //user defined Handler 348 //user defined Handler
349 if p.enableUser {
335 for pattern, c := range p.userHandlers { 350 for pattern, c := range p.userHandlers {
336 if c.regex == nil && pattern == requestPath { 351 if c.regex == nil && pattern == requestPath {
337 c.h.ServeHTTP(rw, r) 352 c.h.ServeHTTP(rw, r)
...@@ -368,16 +383,11 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -368,16 +383,11 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
368 c.h.ServeHTTP(rw, r) 383 c.h.ServeHTTP(rw, r)
369 return 384 return
370 } 385 }
386 }
371 387
372 //first find path from the fixrouters to Improve Performance 388 //first find path from the fixrouters to Improve Performance
373 for _, route := range p.fixrouters { 389 for _, route := range p.fixrouters {
374 n := len(requestPath) 390 n := len(requestPath)
375 //route like "/"
376 //if n == 1 {
377 // else {
378 // continue
379 // }
380 //}
381 if requestPath == route.pattern { 391 if requestPath == route.pattern {
382 runrouter = route 392 runrouter = route
383 findrouter = true 393 findrouter = true
...@@ -392,6 +402,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -392,6 +402,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
392 } 402 }
393 } 403 }
394 404
405 //find regex's router
395 if !findrouter { 406 if !findrouter {
396 //find a matching Route 407 //find a matching Route
397 for _, route := range p.routers { 408 for _, route := range p.routers {
...@@ -466,6 +477,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -466,6 +477,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
466 //if response has written,yes don't run next 477 //if response has written,yes don't run next
467 if !w.started { 478 if !w.started {
468 if r.Method == "GET" { 479 if r.Method == "GET" {
480 if runrouter.hasMethod {
469 if m, ok := runrouter.methods["get"]; ok { 481 if m, ok := runrouter.methods["get"]; ok {
470 method = vc.MethodByName(m) 482 method = vc.MethodByName(m)
471 } else if m, ok = runrouter.methods["*"]; ok { 483 } else if m, ok = runrouter.methods["*"]; ok {
...@@ -473,8 +485,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -473,8 +485,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
473 } else { 485 } else {
474 method = vc.MethodByName("Get") 486 method = vc.MethodByName("Get")
475 } 487 }
488 } else {
489 method = vc.MethodByName("Get")
490 }
476 method.Call(in) 491 method.Call(in)
477 } else if r.Method == "HEAD" { 492 } else if r.Method == "HEAD" {
493 if runrouter.hasMethod {
478 if m, ok := runrouter.methods["head"]; ok { 494 if m, ok := runrouter.methods["head"]; ok {
479 method = vc.MethodByName(m) 495 method = vc.MethodByName(m)
480 } else if m, ok = runrouter.methods["*"]; ok { 496 } else if m, ok = runrouter.methods["*"]; ok {
...@@ -482,8 +498,13 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -482,8 +498,13 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
482 } else { 498 } else {
483 method = vc.MethodByName("Head") 499 method = vc.MethodByName("Head")
484 } 500 }
501 } else {
502 method = vc.MethodByName("Head")
503 }
504
485 method.Call(in) 505 method.Call(in)
486 } else if r.Method == "DELETE" || (r.Method == "POST" && r.Form.Get("_method") == "delete") { 506 } else if r.Method == "DELETE" || (r.Method == "POST" && r.Form.Get("_method") == "delete") {
507 if runrouter.hasMethod {
487 if m, ok := runrouter.methods["delete"]; ok { 508 if m, ok := runrouter.methods["delete"]; ok {
488 method = vc.MethodByName(m) 509 method = vc.MethodByName(m)
489 } else if m, ok = runrouter.methods["*"]; ok { 510 } else if m, ok = runrouter.methods["*"]; ok {
...@@ -491,8 +512,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -491,8 +512,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
491 } else { 512 } else {
492 method = vc.MethodByName("Delete") 513 method = vc.MethodByName("Delete")
493 } 514 }
515 } else {
516 method = vc.MethodByName("Delete")
517 }
494 method.Call(in) 518 method.Call(in)
495 } else if r.Method == "PUT" || (r.Method == "POST" && r.Form.Get("_method") == "put") { 519 } else if r.Method == "PUT" || (r.Method == "POST" && r.Form.Get("_method") == "put") {
520 if runrouter.hasMethod {
496 if m, ok := runrouter.methods["put"]; ok { 521 if m, ok := runrouter.methods["put"]; ok {
497 method = vc.MethodByName(m) 522 method = vc.MethodByName(m)
498 } else if m, ok = runrouter.methods["*"]; ok { 523 } else if m, ok = runrouter.methods["*"]; ok {
...@@ -500,8 +525,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -500,8 +525,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
500 } else { 525 } else {
501 method = vc.MethodByName("Put") 526 method = vc.MethodByName("Put")
502 } 527 }
528 } else {
529 method = vc.MethodByName("Put")
530 }
503 method.Call(in) 531 method.Call(in)
504 } else if r.Method == "POST" { 532 } else if r.Method == "POST" {
533 if runrouter.hasMethod {
505 if m, ok := runrouter.methods["post"]; ok { 534 if m, ok := runrouter.methods["post"]; ok {
506 method = vc.MethodByName(m) 535 method = vc.MethodByName(m)
507 } else if m, ok = runrouter.methods["*"]; ok { 536 } else if m, ok = runrouter.methods["*"]; ok {
...@@ -509,8 +538,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -509,8 +538,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
509 } else { 538 } else {
510 method = vc.MethodByName("Post") 539 method = vc.MethodByName("Post")
511 } 540 }
541 } else {
542 method = vc.MethodByName("Post")
543 }
512 method.Call(in) 544 method.Call(in)
513 } else if r.Method == "PATCH" { 545 } else if r.Method == "PATCH" {
546 if runrouter.hasMethod {
514 if m, ok := runrouter.methods["patch"]; ok { 547 if m, ok := runrouter.methods["patch"]; ok {
515 method = vc.MethodByName(m) 548 method = vc.MethodByName(m)
516 } else if m, ok = runrouter.methods["*"]; ok { 549 } else if m, ok = runrouter.methods["*"]; ok {
...@@ -518,8 +551,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -518,8 +551,12 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
518 } else { 551 } else {
519 method = vc.MethodByName("Patch") 552 method = vc.MethodByName("Patch")
520 } 553 }
554 } else {
555 method = vc.MethodByName("Patch")
556 }
521 method.Call(in) 557 method.Call(in)
522 } else if r.Method == "OPTIONS" { 558 } else if r.Method == "OPTIONS" {
559 if runrouter.hasMethod {
523 if m, ok := runrouter.methods["options"]; ok { 560 if m, ok := runrouter.methods["options"]; ok {
524 method = vc.MethodByName(m) 561 method = vc.MethodByName(m)
525 } else if m, ok = runrouter.methods["*"]; ok { 562 } else if m, ok = runrouter.methods["*"]; ok {
...@@ -527,6 +564,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -527,6 +564,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
527 } else { 564 } else {
528 method = vc.MethodByName("Options") 565 method = vc.MethodByName("Options")
529 } 566 }
567 } else {
568 method = vc.MethodByName("Options")
569 }
530 method.Call(in) 570 method.Call(in)
531 } 571 }
532 gotofunc := vc.Elem().FieldByName("gotofunc").String() 572 gotofunc := vc.Elem().FieldByName("gotofunc").String()
...@@ -553,6 +593,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -553,6 +593,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
553 593
554 //start autorouter 594 //start autorouter
555 595
596 if p.enableAuto {
556 if !findrouter { 597 if !findrouter {
557 for cName, methodmap := range p.autoRouter { 598 for cName, methodmap := range p.autoRouter {
558 599
...@@ -619,6 +660,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -619,6 +660,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
619 } 660 }
620 } 661 }
621 } 662 }
663 }
664
622 //if no matches to url, throw a not found exception 665 //if no matches to url, throw a not found exception
623 if !findrouter { 666 if !findrouter {
624 if h, ok := ErrorMaps["404"]; ok { 667 if h, ok := ErrorMaps["404"]; ok {
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!