[server] Minor updates that completely eliminate any allocations

This commit is contained in:
Ciprian Dorin Craciun 2018-11-13 17:38:43 +02:00
parent 72489042d6
commit e922169912

View file

@ -24,17 +24,20 @@ import . "github.com/cipriancraciun/go-cdb-http/lib/server"
type server struct { type server struct {
httpServer *fasthttp.Server
cdbReader *cdb.CDB cdbReader *cdb.CDB
debug bool debug bool
} }
func (_server *server) HandleHTTP (_context *fasthttp.RequestCtx) () { func (_server *server) HandleHTTP (_context *fasthttp.RequestCtx) () {
_uri := _context.URI () _uri := _context.URI ()
_request := &_context.Request // _request := &_context.Request
_requestHeaders := &_request.Header _requestHeaders := &_context.Request.Header
_response := &_context.Response _response := &_context.Response
_responseHeaders := &_response.Header _responseHeaders := &_context.Response.Header
_keyBuffer := [1024]byte {} _keyBuffer := [1024]byte {}
_pathNewBuffer := [1024]byte {} _pathNewBuffer := [1024]byte {}
@ -81,7 +84,7 @@ func (_server *server) HandleHTTP (_context *fasthttp.RequestCtx) () {
_key = append (_key, _namespace ...) _key = append (_key, _namespace ...)
_key = append (_key, ':') _key = append (_key, ':')
_key = append (_key, _path_0 ...) _key = append (_key, _path_0 ...)
if _value, _error := _server.cdbReader.Get (_key); _error == nil { if _value, _error := _server.cdbReader.GetWithCdbHash (_key); _error == nil {
if _value != nil { if _value != nil {
_fingerprint = _value _fingerprint = _value
if (_namespace == NamespaceFoldersContent) || (_namespace == NamespaceFoldersEntries) { if (_namespace == NamespaceFoldersContent) || (_namespace == NamespaceFoldersEntries) {
@ -109,7 +112,7 @@ func (_server *server) HandleHTTP (_context *fasthttp.RequestCtx) () {
} }
_key = append (_key, '/') _key = append (_key, '/')
_key = append (_key, _indexName ...) _key = append (_key, _indexName ...)
if _value, _error := _server.cdbReader.Get (_key); _error == nil { if _value, _error := _server.cdbReader.GetWithCdbHash (_key); _error == nil {
_fingerprint = _value _fingerprint = _value
break _found break _found
} else { } else {
@ -149,7 +152,7 @@ func (_server *server) HandleHTTP (_context *fasthttp.RequestCtx) () {
_key = append (_key, NamespaceDataContent ...) _key = append (_key, NamespaceDataContent ...)
_key = append (_key, ':') _key = append (_key, ':')
_key = append (_key, _fingerprint ...) _key = append (_key, _fingerprint ...)
if _value, _error := _server.cdbReader.Get (_key); _error == nil { if _value, _error := _server.cdbReader.GetWithCdbHash (_key); _error == nil {
if _value != nil { if _value != nil {
_data = _value _data = _value
} else { } else {
@ -163,16 +166,14 @@ func (_server *server) HandleHTTP (_context *fasthttp.RequestCtx) () {
} }
} }
var _metadata [][2]string
{ {
_key := _keyBuffer[:0] _key := _keyBuffer[:0]
_key = append (_key, NamespaceDataMetadata ...) _key = append (_key, NamespaceDataMetadata ...)
_key = append (_key, ':') _key = append (_key, ':')
_key = append (_key, _fingerprint ...) _key = append (_key, _fingerprint ...)
if _value, _error := _server.cdbReader.Get (_key); _error == nil { if _value, _error := _server.cdbReader.GetWithCdbHash (_key); _error == nil {
if _value != nil { if _value != nil {
if _metadata_0, _error := MetadataDecode (_value); _error == nil { if _error := MetadataDecodeIterate (_value, _responseHeaders.SetBytesKV); _error == nil {
_metadata = _metadata_0
} else { } else {
_server.ServeError (_context, http.StatusInternalServerError, _error) _server.ServeError (_context, http.StatusInternalServerError, _error)
return return
@ -192,9 +193,6 @@ func (_server *server) HandleHTTP (_context *fasthttp.RequestCtx) () {
// log.Printf ("[dd] [b15f3cad] serving for `%s`...\n", _path) // log.Printf ("[dd] [b15f3cad] serving for `%s`...\n", _path)
} }
for _, _metadata := range _metadata {
_responseHeaders.Set (_metadata[0], _metadata[1])
}
_responseHeaders.Set ("cache-control", "public, immutable, max-age=3600") _responseHeaders.Set ("cache-control", "public, immutable, max-age=3600")
_response.SetStatusCode (http.StatusOK) _response.SetStatusCode (http.StatusOK)
@ -206,7 +204,7 @@ func (_server *server) HandleHTTP (_context *fasthttp.RequestCtx) () {
func (_server *server) ServeRedirect (_context *fasthttp.RequestCtx, _status uint, _path []byte) () { func (_server *server) ServeRedirect (_context *fasthttp.RequestCtx, _status uint, _path []byte) () {
_response := &_context.Response _response := &_context.Response
_responseHeaders := &_response.Header _responseHeaders := &_context.Response.Header
_responseHeaders.Set ("content-type", MimeTypeText) _responseHeaders.Set ("content-type", MimeTypeText)
_responseHeaders.Set ("content-encoding", "identity") _responseHeaders.Set ("content-encoding", "identity")
@ -221,7 +219,7 @@ func (_server *server) ServeRedirect (_context *fasthttp.RequestCtx, _status uin
func (_server *server) ServeError (_context *fasthttp.RequestCtx, _status uint, _error error) () { func (_server *server) ServeError (_context *fasthttp.RequestCtx, _status uint, _error error) () {
_response := &_context.Response _response := &_context.Response
_responseHeaders := &_response.Header _responseHeaders := &_context.Response.Header
_responseHeaders.Set ("content-type", MimeTypeText) _responseHeaders.Set ("content-type", MimeTypeText)
_responseHeaders.Set ("content-encoding", "identity") _responseHeaders.Set ("content-encoding", "identity")