Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
appleboy committed May 9, 2020
1 parent a53322c commit f1b86cc
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 260 deletions.
33 changes: 1 addition & 32 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ type Engine struct {
noMethod HandlersChain
pool sync.Pool
trees methodTrees
// paramsPool sync.Pool
maxParams uint16
maxParams uint16
}

var _ IRouter = &Engine{}
Expand Down Expand Up @@ -254,7 +253,6 @@ func (engine *Engine) rebuild405Handlers() {
}

func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
// log.Println(method, path)
assert1(path[0] == '/', "path must begin with '/'")
assert1(method != "", "HTTP method can not be empty")
assert1(len(handlers) > 0, "there must be at least one handler")
Expand All @@ -271,19 +269,9 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
root.addRoute(path, handlers)

// Update maxParams
// log.Println("countParams(path):", countParams(path))
if paramsCount := countParams(path); paramsCount+varsCount > engine.maxParams {
engine.maxParams = paramsCount + varsCount
}

// Lazy-init paramsPool alloc func
// if engine.paramsPool.New == nil && engine.maxParams > 0 {
// engine.paramsPool.New = func() interface{} {
// // log.Println("engine.maxParams:", engine.maxParams)
// ps := make(Params, 0, engine.maxParams)
// return &ps
// }
// }
}

// Routes returns a slice of registered routes, including some useful information, such as:
Expand Down Expand Up @@ -402,21 +390,6 @@ func (engine *Engine) HandleContext(c *Context) {
c.index = oldIndexValue
}

// func (engine *Engine) getParams() *Params {
// ps := engine.paramsPool.Get().(*Params)
// *ps = (*ps)[0:0] // reset slice
// return ps
// }

// func (engine *Engine) putParams(ps *Params) {
// c := engine.pool.Get().(*Context)
// c.Params = *ps
// engine.pool.Put(c)
// if ps != nil {
// engine.paramsPool.Put(ps)
// }
// }

func (engine *Engine) handleHTTPRequest(c *Context) {
httpMethod := c.Request.Method
rPath := c.Request.URL.Path
Expand All @@ -440,14 +413,10 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
// Find route in tree
value := root.getValue(rPath, c.params, unescape)
if value.params != nil {
// engine.putParams(value.params)
c.Params = *value.params
}
if value.handlers != nil {
c.handlers = value.handlers
// if value.params != nil {
// c.Params = *value.params
// }
c.fullPath = value.fullPath
c.Next()
c.writermem.WriteHeaderNow()
Expand Down
Loading

0 comments on commit f1b86cc

Please sign in to comment.