Pool web response buffer at 1 MiB between responses

This commit is contained in:
DataHoarder 2023-05-25 14:48:11 +02:00
parent 493b482d90
commit 7fcad714be
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk

View file

@ -35,6 +35,7 @@ import (
"reflect"
"strconv"
"strings"
"sync"
"time"
)
@ -147,6 +148,11 @@ func toFloat64(t any) float64 {
func main() {
var responseBufferPool sync.Pool
responseBufferPool.New = func() any {
return make([]byte, 0, 1024*1024) //1 MiB allocations
}
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
//monerod related
@ -200,8 +206,9 @@ func main() {
log.Printf("Consensus id = %s", consensus.Id())
render := func(request *http.Request, writer http.ResponseWriter, template string, ctx map[string]stick.Value) {
w := bytes.NewBuffer(nil)
w := bytes.NewBuffer(responseBufferPool.Get().([]byte))
defer func() {
defer responseBufferPool.Put(w.Bytes()[:0])
_, _ = writer.Write(w.Bytes())
}()
defer func() {