Improved performance
This commit is contained in:
parent
80dc3f87eb
commit
594cad69db
11
Context.go
11
Context.go
@ -40,17 +40,6 @@ type ctx struct {
|
||||
handlerCount int
|
||||
}
|
||||
|
||||
// newContext returns a new context from the pool.
|
||||
func newContext(req *http.Request, res http.ResponseWriter, server *Server) *ctx {
|
||||
ctx := contextPool.Get().(*ctx)
|
||||
ctx.request = req
|
||||
ctx.response = res
|
||||
ctx.server = server
|
||||
ctx.paramCount = 0
|
||||
ctx.handlerCount = 0
|
||||
return ctx
|
||||
}
|
||||
|
||||
// Bytes responds with a raw byte slice.
|
||||
func (ctx *ctx) Bytes(body []byte) error {
|
||||
_, err := ctx.response.Write(body)
|
||||
|
@ -50,8 +50,8 @@ coverage: 100.0% of statements
|
||||
## Benchmarks
|
||||
|
||||
```
|
||||
BenchmarkHello-12 31128832 38.41 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGitHub-12 17406580 68.56 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkHello-12 35983602 33.28 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGitHub-12 18320769 68.66 ns/op 0 B/op 0 allocs/op
|
||||
```
|
||||
|
||||
## License
|
||||
|
11
Server.go
11
Server.go
@ -61,15 +61,20 @@ func (server *Server) Put(path string, handler Handler) {
|
||||
|
||||
// ServeHTTP responds to the given request.
|
||||
func (server *Server) ServeHTTP(response http.ResponseWriter, request *http.Request) {
|
||||
ctx := newContext(request, response, server)
|
||||
defer contextPool.Put(ctx)
|
||||
|
||||
ctx := contextPool.Get().(*ctx)
|
||||
ctx.request = request
|
||||
ctx.response = response
|
||||
ctx.server = server
|
||||
err := server.handlers[0](ctx)
|
||||
|
||||
if err != nil {
|
||||
response.(io.StringWriter).WriteString(err.Error())
|
||||
log.Println(request.URL, err)
|
||||
}
|
||||
|
||||
ctx.paramCount = 0
|
||||
ctx.handlerCount = 0
|
||||
contextPool.Put(ctx)
|
||||
}
|
||||
|
||||
// Run starts the server on the given address.
|
||||
|
Loading…
Reference in New Issue
Block a user