From 8660a54facfff34b6f6297921fbe4808c8a7e8bc Mon Sep 17 00:00:00 2001 From: JessonChan Date: Tue, 15 Mar 2016 11:49:23 +0800 Subject: [PATCH 1/5] make router fast --- admin.go | 2 +- namespace.go | 4 ++-- router.go | 55 +++++++++++++++++++++++++--------------------------- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/admin.go b/admin.go index 031e6421..cf5bc63a 100644 --- a/admin.go +++ b/admin.go @@ -196,7 +196,7 @@ func listConf(rw http.ResponseWriter, r *http.Request) { BeforeExec: "Before Exec", AfterExec: "After Exec", FinishRouter: "Finish Router"} { - if bf, ok := BeeApp.Handlers.filters[k]; ok { + if bf := BeeApp.Handlers.filters[k]; len(bf) > 0 { filterType = fr filterTypes = append(filterTypes, filterType) resultList := new([][]string) diff --git a/namespace.go b/namespace.go index 4007d44c..cfde0111 100644 --- a/namespace.go +++ b/namespace.go @@ -44,7 +44,7 @@ func NewNamespace(prefix string, params ...LinkNamespace) *Namespace { return ns } -// Cond set condtion function +// Cond set condition function // if cond return true can run this namespace, else can't // usage: // ns.Cond(func (ctx *context.Context) bool{ @@ -60,7 +60,7 @@ func (n *Namespace) Cond(cond namespaceCond) *Namespace { exception("405", ctx) } } - if v, ok := n.handlers.filters[BeforeRouter]; ok { + if v := n.handlers.filters[BeforeRouter]; len(v) > 0 { mr := new(FilterRouter) mr.tree = NewTree() mr.pattern = "*" diff --git a/router.go b/router.go index 5b4b1ff9..597acbb9 100644 --- a/router.go +++ b/router.go @@ -114,7 +114,7 @@ type controllerInfo struct { type ControllerRegister struct { routers map[string]*Tree enableFilter bool - filters map[int][]*FilterRouter + filters [5][]*FilterRouter pool sync.Pool } @@ -122,7 +122,6 @@ type ControllerRegister struct { func NewControllerRegister() *ControllerRegister { cr := &ControllerRegister{ routers: make(map[string]*Tree), - filters: make(map[int][]*FilterRouter), } cr.pool.New = func() interface{} { return beecontext.NewContext() @@ -408,7 +407,6 @@ func (p *ControllerRegister) AddAutoPrefix(prefix string, c ControllerInterface) // InsertFilter Add a FilterFunc with pattern rule and action constant. // The bool params is for setting the returnOnOutput value (false allows multiple filters to execute) func (p *ControllerRegister) InsertFilter(pattern string, pos int, filter FilterFunc, params ...bool) error { - mr := new(FilterRouter) mr.tree = NewTree() mr.pattern = pattern @@ -426,9 +424,13 @@ func (p *ControllerRegister) InsertFilter(pattern string, pos int, filter Filter } // add Filter into -func (p *ControllerRegister) insertFilterRouter(pos int, mr *FilterRouter) error { - p.filters[pos] = append(p.filters[pos], mr) +func (p *ControllerRegister) insertFilterRouter(pos int, mr *FilterRouter) (err error) { + if pos < BeforeStatic || pos > FinishRouter { + err = fmt.Errorf("can not find your filter postion") + return + } p.enableFilter = true + p.filters[pos] = append(p.filters[pos], mr) return nil } @@ -577,20 +579,16 @@ func (p *ControllerRegister) geturl(t *Tree, url, controllName, methodName strin return false, "" } -func (p *ControllerRegister) execFilter(context *beecontext.Context, pos int, urlPath string) (started bool) { - if p.enableFilter { - if l, ok := p.filters[pos]; ok { - for _, filterR := range l { - if filterR.returnOnOutput && context.ResponseWriter.Started { - return true - } - if ok := filterR.ValidRouter(urlPath, context); ok { - filterR.filterFunc(context) - } - if filterR.returnOnOutput && context.ResponseWriter.Started { - return true - } - } +func (p *ControllerRegister) execFilter(context *beecontext.Context, urlPath string, l []*FilterRouter) (started bool) { + for _, filterR := range l { + if filterR.returnOnOutput && context.ResponseWriter.Started { + return true + } + if ok := filterR.ValidRouter(urlPath, context); ok { + filterR.filterFunc(context) + } + if filterR.returnOnOutput && context.ResponseWriter.Started { + return true } } return false @@ -617,11 +615,10 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) context.Output.Header("Server", BConfig.ServerName) } - var urlPath string + var urlPath = r.URL.Path + if !BConfig.RouterCaseSensitive { urlPath = strings.ToLower(r.URL.Path) - } else { - urlPath = r.URL.Path } // filter wrong http method @@ -631,7 +628,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) } // filter for static file - if p.execFilter(context, BeforeStatic, urlPath) { + if fs := p.filters[BeforeStatic]; len(fs) > 0 && p.execFilter(context, urlPath, fs) { goto Admin } @@ -663,8 +660,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) } }() } - - if p.execFilter(context, BeforeRouter, urlPath) { + if fs := p.filters[BeforeRouter]; len(fs) > 0 && p.execFilter(context, urlPath, fs) { goto Admin } @@ -693,7 +689,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) if findRouter { //execute middleware filters - if p.execFilter(context, BeforeExec, urlPath) { + if fs := p.filters[BeforeExec]; len(fs) > 0 && p.execFilter(context, urlPath, fs) { goto Admin } isRunnable := false @@ -794,12 +790,13 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) } //execute middleware filters - if p.execFilter(context, AfterExec, urlPath) { + if fs := p.filters[AfterExec]; len(fs) > 0 && p.execFilter(context, urlPath, fs) { goto Admin } } - - p.execFilter(context, FinishRouter, urlPath) + if fs := p.filters[FinishRouter]; len(fs) > 0 && p.execFilter(context, urlPath, fs) { + goto Admin + } Admin: //admin module record QPS From c51bc86d3fd0dc38596017f8869b0bb2ff2b6733 Mon Sep 17 00:00:00 2001 From: JessonChan Date: Tue, 15 Mar 2016 16:51:21 +0800 Subject: [PATCH 2/5] goto bug fixed --- router.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/router.go b/router.go index 597acbb9..9b62c4cc 100644 --- a/router.go +++ b/router.go @@ -652,7 +652,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) if err != nil { Error(err) exception("503", context) - return + goto Admin } defer func() { if context.Input.CruSession != nil { From 34615ee8fcbb485574b181bd2bf9be86e3a97e87 Mon Sep 17 00:00:00 2001 From: JessonChan Date: Tue, 15 Mar 2016 18:37:54 +0800 Subject: [PATCH 3/5] add router filter enable flag --- router.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/router.go b/router.go index 9b62c4cc..9e75e013 100644 --- a/router.go +++ b/router.go @@ -114,7 +114,8 @@ type controllerInfo struct { type ControllerRegister struct { routers map[string]*Tree enableFilter bool - filters [5][]*FilterRouter + filters [FinishRouter + 1][]*FilterRouter + filterFlag [FinishRouter + 1]bool pool sync.Pool } @@ -430,6 +431,7 @@ func (p *ControllerRegister) insertFilterRouter(pos int, mr *FilterRouter) (err return } p.enableFilter = true + p.filterFlag[pos] = true p.filters[pos] = append(p.filters[pos], mr) return nil } @@ -579,8 +581,8 @@ func (p *ControllerRegister) geturl(t *Tree, url, controllName, methodName strin return false, "" } -func (p *ControllerRegister) execFilter(context *beecontext.Context, urlPath string, l []*FilterRouter) (started bool) { - for _, filterR := range l { +func (p *ControllerRegister) execFilter(context *beecontext.Context, urlPath string, pos int) (started bool) { + for _, filterR := range p.filters[pos] { if filterR.returnOnOutput && context.ResponseWriter.Started { return true } @@ -628,11 +630,12 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) } // filter for static file - if fs := p.filters[BeforeStatic]; len(fs) > 0 && p.execFilter(context, urlPath, fs) { + if p.filterFlag[BeforeStatic] && p.execFilter(context, urlPath, BeforeStatic) { goto Admin } serverStaticRouter(context) + if context.ResponseWriter.Started { findRouter = true goto Admin @@ -660,7 +663,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) } }() } - if fs := p.filters[BeforeRouter]; len(fs) > 0 && p.execFilter(context, urlPath, fs) { + if p.filterFlag[BeforeRouter] && p.execFilter(context, urlPath, BeforeRouter) { goto Admin } @@ -689,7 +692,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) if findRouter { //execute middleware filters - if fs := p.filters[BeforeExec]; len(fs) > 0 && p.execFilter(context, urlPath, fs) { + if p.filterFlag[BeforeExec] && p.execFilter(context, urlPath, BeforeExec) { goto Admin } isRunnable := false @@ -790,11 +793,11 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) } //execute middleware filters - if fs := p.filters[AfterExec]; len(fs) > 0 && p.execFilter(context, urlPath, fs) { + if p.filterFlag[AfterExec] && p.execFilter(context, urlPath, AfterExec) { goto Admin } } - if fs := p.filters[FinishRouter]; len(fs) > 0 && p.execFilter(context, urlPath, fs) { + if p.filterFlag[FinishRouter] && p.execFilter(context, urlPath, FinishRouter) { goto Admin } From 565c4a4d592cf196610a3759d88b00822c87fef8 Mon Sep 17 00:00:00 2001 From: JessonChan Date: Tue, 15 Mar 2016 18:50:18 +0800 Subject: [PATCH 4/5] make the code run more fast --- router.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/router.go b/router.go index 9e75e013..0da56464 100644 --- a/router.go +++ b/router.go @@ -115,7 +115,6 @@ type ControllerRegister struct { routers map[string]*Tree enableFilter bool filters [FinishRouter + 1][]*FilterRouter - filterFlag [FinishRouter + 1]bool pool sync.Pool } @@ -431,7 +430,6 @@ func (p *ControllerRegister) insertFilterRouter(pos int, mr *FilterRouter) (err return } p.enableFilter = true - p.filterFlag[pos] = true p.filters[pos] = append(p.filters[pos], mr) return nil } @@ -630,7 +628,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) } // filter for static file - if p.filterFlag[BeforeStatic] && p.execFilter(context, urlPath, BeforeStatic) { + if len(p.filters[BeforeStatic]) > 0 && p.execFilter(context, urlPath, BeforeStatic) { goto Admin } @@ -663,7 +661,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) } }() } - if p.filterFlag[BeforeRouter] && p.execFilter(context, urlPath, BeforeRouter) { + if len(p.filters[BeforeRouter]) > 0 && p.execFilter(context, urlPath, BeforeRouter) { goto Admin } @@ -692,7 +690,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) if findRouter { //execute middleware filters - if p.filterFlag[BeforeExec] && p.execFilter(context, urlPath, BeforeExec) { + if len(p.filters[BeforeExec]) > 0 && p.execFilter(context, urlPath, BeforeExec) { goto Admin } isRunnable := false @@ -793,11 +791,11 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) } //execute middleware filters - if p.filterFlag[AfterExec] && p.execFilter(context, urlPath, AfterExec) { + if len(p.filters[AfterExec]) > 0 && p.execFilter(context, urlPath, AfterExec) { goto Admin } } - if p.filterFlag[FinishRouter] && p.execFilter(context, urlPath, FinishRouter) { + if len(p.filters[FinishRouter]) > 0 && p.execFilter(context, urlPath, FinishRouter) { goto Admin } From 94599013fc8a932de20bba7ce2fce91f61634c16 Mon Sep 17 00:00:00 2001 From: JessonChan Date: Wed, 16 Mar 2016 07:53:36 +0800 Subject: [PATCH 5/5] url to lower case --- router.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/router.go b/router.go index 0da56464..71be4c16 100644 --- a/router.go +++ b/router.go @@ -618,7 +618,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) var urlPath = r.URL.Path if !BConfig.RouterCaseSensitive { - urlPath = strings.ToLower(r.URL.Path) + urlPath = strings.ToLower(urlPath) } // filter wrong http method