mirror of https://github.com/gin-gonic/gin.git
Adds NewWithConfig() and CacheStress()
This commit is contained in:
parent
10979dd862
commit
30ea9c06fc
42
gin.go
42
gin.go
|
@ -31,6 +31,11 @@ type (
|
||||||
|
|
||||||
ErrorMsgs []ErrorMsg
|
ErrorMsgs []ErrorMsg
|
||||||
|
|
||||||
|
Config struct {
|
||||||
|
CacheSize int
|
||||||
|
Preallocated int
|
||||||
|
}
|
||||||
|
|
||||||
// Context is the most important part of gin. It allows us to pass variables between middleware,
|
// Context is the most important part of gin. It allows us to pass variables between middleware,
|
||||||
// manage the flow, validate the JSON of a request and render a JSON response for example.
|
// manage the flow, validate the JSON of a request and render a JSON response for example.
|
||||||
Context struct {
|
Context struct {
|
||||||
|
@ -39,8 +44,8 @@ type (
|
||||||
Keys map[string]interface{}
|
Keys map[string]interface{}
|
||||||
Errors ErrorMsgs
|
Errors ErrorMsgs
|
||||||
Params httprouter.Params
|
Params httprouter.Params
|
||||||
|
Engine *Engine
|
||||||
handlers []HandlerFunc
|
handlers []HandlerFunc
|
||||||
engine *Engine
|
|
||||||
index int8
|
index int8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,24 +77,35 @@ func (a ErrorMsgs) String() string {
|
||||||
return buffer.String()
|
return buffer.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a new blank Engine instance without any middleware attached.
|
func NewWithConfig(config Config) *Engine {
|
||||||
// The most basic configuration
|
if config.CacheSize < 2 {
|
||||||
func New() *Engine {
|
panic("CacheSize must be at least 2")
|
||||||
cacheSize := 1024
|
}
|
||||||
|
if config.Preallocated > config.CacheSize {
|
||||||
|
panic("Preallocated must be less or equal to CacheSize")
|
||||||
|
}
|
||||||
engine := &Engine{}
|
engine := &Engine{}
|
||||||
engine.RouterGroup = &RouterGroup{nil, "/", nil, engine}
|
engine.RouterGroup = &RouterGroup{nil, "/", nil, engine}
|
||||||
engine.router = httprouter.New()
|
engine.router = httprouter.New()
|
||||||
engine.router.NotFound = engine.handle404
|
engine.router.NotFound = engine.handle404
|
||||||
engine.cache = make(chan *Context, cacheSize)
|
engine.cache = make(chan *Context, config.CacheSize)
|
||||||
|
|
||||||
// Fill it with empty contexts
|
// Fill it with empty contexts
|
||||||
for i := 0; i < cacheSize/2; i++ {
|
for i := 0; i < config.Preallocated; i++ {
|
||||||
engine.cache <- &Context{engine: engine}
|
engine.cache <- &Context{Engine: engine}
|
||||||
}
|
}
|
||||||
return engine
|
return engine
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns a new blank Engine instance without any middleware attached.
|
||||||
|
// The most basic configuration
|
||||||
|
func New() *Engine {
|
||||||
|
return NewWithConfig(Config{
|
||||||
|
CacheSize: 1024,
|
||||||
|
Preallocated: 512,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Returns a Engine instance with the Logger and Recovery already attached.
|
// Returns a Engine instance with the Logger and Recovery already attached.
|
||||||
func Default() *Engine {
|
func Default() *Engine {
|
||||||
engine := New()
|
engine := New()
|
||||||
|
@ -106,6 +122,10 @@ func (engine *Engine) NotFound404(handlers ...HandlerFunc) {
|
||||||
engine.handlers404 = handlers
|
engine.handlers404 = handlers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) CacheStress() float32 {
|
||||||
|
return 1.0 - float32(len(engine.cache))/float32(cap(engine.cache))
|
||||||
|
}
|
||||||
|
|
||||||
func (engine *Engine) handle404(w http.ResponseWriter, req *http.Request) {
|
func (engine *Engine) handle404(w http.ResponseWriter, req *http.Request) {
|
||||||
handlers := engine.combineHandlers(engine.handlers404)
|
handlers := engine.combineHandlers(engine.handlers404)
|
||||||
c := engine.createContext(w, req, nil, handlers)
|
c := engine.createContext(w, req, nil, handlers)
|
||||||
|
@ -162,7 +182,7 @@ func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, pa
|
||||||
Params: params,
|
Params: params,
|
||||||
handlers: handlers,
|
handlers: handlers,
|
||||||
index: -1,
|
index: -1,
|
||||||
engine: engine,
|
Engine: engine,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -385,7 +405,7 @@ func (c *Context) HTML(code int, name string, data interface{}) {
|
||||||
if code >= 0 {
|
if code >= 0 {
|
||||||
c.Writer.WriteHeader(code)
|
c.Writer.WriteHeader(code)
|
||||||
}
|
}
|
||||||
if err := c.engine.HTMLTemplates.ExecuteTemplate(c.Writer, name, data); err != nil {
|
if err := c.Engine.HTMLTemplates.ExecuteTemplate(c.Writer, name, data); err != nil {
|
||||||
c.Error(err, map[string]interface{}{
|
c.Error(err, map[string]interface{}{
|
||||||
"name": name,
|
"name": name,
|
||||||
"data": data,
|
"data": data,
|
||||||
|
|
Loading…
Reference in New Issue