breaking change: modify API for working with response metadata (#213)
* implement functions to append/get metadata and set/get status code * сhanged behavior to return nil instead of empty metadata for getResponseMetadata() * сhanged work with HTTP headers to use direct array assignment instead of for-range * fix linters * fix meter handler * fix uninitialized response metadata for incoming context * removed a useless test * metrics handler has been fixed to work with compressed data
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"sync"
|
||||
|
||||
codecpb "go.unistack.org/micro-proto/v4/codec"
|
||||
httpsrv "go.unistack.org/micro-server-http/v4"
|
||||
"go.unistack.org/micro/v4/logger"
|
||||
"go.unistack.org/micro/v4/metadata"
|
||||
"go.unistack.org/micro/v4/meter"
|
||||
@@ -96,9 +97,9 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb.
|
||||
|
||||
w := io.Writer(buf)
|
||||
|
||||
if md, ok := metadata.FromOutgoingContext(ctx); gzipAccepted(md) && ok && !h.Options.DisableCompress {
|
||||
omd, _ := metadata.FromOutgoingContext(ctx)
|
||||
omd.Set(contentEncodingHeader, "gzip")
|
||||
if md, ok := metadata.FromIncomingContext(ctx); ok && gzipAccepted(md) && !h.Options.DisableCompress {
|
||||
httpsrv.AppendResponseMetadata(ctx, metadata.Pairs(contentEncodingHeader, "gzip"))
|
||||
|
||||
gz := gzipPool.Get().(*gzip.Writer)
|
||||
defer gzipPool.Put(gz)
|
||||
|
||||
@@ -106,7 +107,6 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb.
|
||||
defer gz.Close()
|
||||
|
||||
w = gz
|
||||
gz.Flush()
|
||||
}
|
||||
|
||||
if err := h.Options.Meter.Write(w, h.Options.MeterOptions...); err != nil {
|
||||
@@ -114,6 +114,11 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb.
|
||||
return nil
|
||||
}
|
||||
|
||||
// gz.Flush() must be called after writing metrics to ensure buffered data is written to the underlying writer.
|
||||
if gz, ok := w.(*gzip.Writer); ok {
|
||||
gz.Flush()
|
||||
}
|
||||
|
||||
rsp.Data = buf.Bytes()
|
||||
|
||||
return nil
|
||||
|
Reference in New Issue
Block a user