add pool for buf #153

This commit is contained in:
Gorbunov Kirill Andreevich 2024-03-11 12:09:14 +03:00
parent da92725f10
commit 0cd96ef47e

View File

@ -27,6 +27,12 @@ var gzipPool = sync.Pool{
}, },
} }
var bufPool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(nil)
},
}
// guard to fail early // guard to fail early
var _ MeterServiceServer = (*Handler)(nil) var _ MeterServiceServer = (*Handler)(nil)
@ -78,7 +84,11 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb.
if !ok { if !ok {
log = logger.DefaultLogger() log = logger.DefaultLogger()
} }
buf := bytes.NewBuffer(nil)
buf := bufPool.Get().(*bytes.Buffer)
defer bufPool.Put(buf)
buf.Reset()
w := io.Writer(buf) w := io.Writer(buf)
if md, ok := metadata.FromContext(ctx); gzipAccepted(md) && ok { if md, ok := metadata.FromContext(ctx); gzipAccepted(md) && ok {
@ -97,7 +107,7 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb.
return nil return nil
} }
rsp.Data = buf.Bytes() w.Write(rsp.Data)
return nil return nil
} }