add gzip rsp #153 #182

Merged
vtolstov merged 6 commits from kgorbunov/micro-server-http:#153 into master 2024-03-11 12:31:20 +03:00
Showing only changes of commit 0cd96ef47e - Show all commits

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
} }