From bee12a2f9cb94a7a451ab01e69a18deaffbe3b81 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Mon, 11 Mar 2024 00:06:45 +0300 Subject: [PATCH 1/6] add gzip rsp #153 --- handler/meter/meter.go | 53 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/handler/meter/meter.go b/handler/meter/meter.go index cd31576..25a14de 100644 --- a/handler/meter/meter.go +++ b/handler/meter/meter.go @@ -2,14 +2,31 @@ package meter // import "go.unistack.org/micro-server-http/v4/handler/meter" import ( "bytes" + "compress/gzip" "context" + "strings" + "sync" codecpb "go.unistack.org/micro-proto/v4/codec" "go.unistack.org/micro/v4/errors" + "go.unistack.org/micro/v4/logger" + "go.unistack.org/micro/v4/metadata" "go.unistack.org/micro/v4/meter" - options "go.unistack.org/micro/v4/options" + "go.unistack.org/micro/v4/options" ) +const ( + contentTypeHeader = "Content-Type" + contentEncodingHeader = "Content-Encoding" + acceptEncodingHeader = "Accept-Encoding" +) + +var gzipPool = sync.Pool{ + New: func() interface{} { + return gzip.NewWriter(nil) + }, +} + // guard to fail early var _ MeterServiceServer = (*Handler)(nil) @@ -57,12 +74,44 @@ func NewHandler(opts ...Option) *Handler { } func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb.Frame) error { + log := logger.DefaultLogger() buf := bytes.NewBuffer(nil) + + if md, ok := metadata.FromContext(ctx); gzipAccepted(md) && ok { + md.Set(contentEncodingHeader, "gzip") + gz := gzipPool.Get().(*gzip.Writer) + defer gzipPool.Put(gz) + + gz.Reset(buf) + defer gz.Close() + + zw := gzip.NewWriter(buf) + defer zw.Close() + *zw = *gz + } + if err := h.opts.Meter.Write(buf, h.opts.MeterOptions...); err != nil { - return errors.InternalServerError(h.opts.Name, "%v", err) + log.Error(ctx, errors.InternalServerError(h.opts.Name, "%v", err)) + return nil } rsp.Data = buf.Bytes() return nil } + +// gzipAccepted returns whether the client will accept gzip-encoded content. +func gzipAccepted(md metadata.Metadata) bool { + a, ok := md.Get(acceptEncodingHeader) + if !ok { + return false + } + parts := strings.Split(a, ",") + for _, part := range parts { + part = strings.TrimSpace(part) + if part == "gzip" || strings.HasPrefix(part, "gzip;") { + return true + } + } + return false +} -- 2.45.2 From cdb4b785f64ff38e584cd82be97b0a66d07e7f15 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Mon, 11 Mar 2024 00:09:55 +0300 Subject: [PATCH 2/6] delete content type #153 --- handler/meter/meter.go | 1 - 1 file changed, 1 deletion(-) diff --git a/handler/meter/meter.go b/handler/meter/meter.go index 25a14de..996f8c0 100644 --- a/handler/meter/meter.go +++ b/handler/meter/meter.go @@ -16,7 +16,6 @@ import ( ) const ( - contentTypeHeader = "Content-Type" contentEncodingHeader = "Content-Encoding" acceptEncodingHeader = "Accept-Encoding" ) -- 2.45.2 From aa6c32026f14b6726808b90748a88f283b689e64 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Mon, 11 Mar 2024 11:18:57 +0300 Subject: [PATCH 3/6] update #153 --- handler/meter/meter.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/handler/meter/meter.go b/handler/meter/meter.go index 996f8c0..16941c6 100644 --- a/handler/meter/meter.go +++ b/handler/meter/meter.go @@ -4,6 +4,7 @@ import ( "bytes" "compress/gzip" "context" + "io" "strings" "sync" @@ -73,8 +74,12 @@ func NewHandler(opts ...Option) *Handler { } func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb.Frame) error { - log := logger.DefaultLogger() + log, ok := logger.FromContext(ctx) + if !ok { + log = logger.DefaultLogger() + } buf := bytes.NewBuffer(nil) + w := io.Writer(buf) if md, ok := metadata.FromContext(ctx); gzipAccepted(md) && ok { md.Set(contentEncodingHeader, "gzip") @@ -84,12 +89,10 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb. gz.Reset(buf) defer gz.Close() - zw := gzip.NewWriter(buf) - defer zw.Close() - *zw = *gz + w = gz } - if err := h.opts.Meter.Write(buf, h.opts.MeterOptions...); err != nil { + if err := h.opts.Meter.Write(w, h.opts.MeterOptions...); err != nil { log.Error(ctx, errors.InternalServerError(h.opts.Name, "%v", err)) return nil } @@ -105,12 +108,8 @@ func gzipAccepted(md metadata.Metadata) bool { if !ok { return false } - parts := strings.Split(a, ",") - for _, part := range parts { - part = strings.TrimSpace(part) - if part == "gzip" || strings.HasPrefix(part, "gzip;") { - return true - } + if strings.Contains(a, "gzip") { + return true } return false } -- 2.45.2 From da92725f105de323565d326e6815cde503585a37 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Mon, 11 Mar 2024 11:20:31 +0300 Subject: [PATCH 4/6] update #153 --- handler/meter/meter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handler/meter/meter.go b/handler/meter/meter.go index 16941c6..b91f777 100644 --- a/handler/meter/meter.go +++ b/handler/meter/meter.go @@ -86,7 +86,7 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb. gz := gzipPool.Get().(*gzip.Writer) defer gzipPool.Put(gz) - gz.Reset(buf) + gz.Reset(w) defer gz.Close() w = gz -- 2.45.2 From 0cd96ef47ec0102f8da063ef4ae1fe57680d6378 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Mon, 11 Mar 2024 12:09:14 +0300 Subject: [PATCH 5/6] add pool for buf #153 --- handler/meter/meter.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/handler/meter/meter.go b/handler/meter/meter.go index b91f777..e9882e3 100644 --- a/handler/meter/meter.go +++ b/handler/meter/meter.go @@ -27,6 +27,12 @@ var gzipPool = sync.Pool{ }, } +var bufPool = sync.Pool{ + New: func() interface{} { + return bytes.NewBuffer(nil) + }, +} + // guard to fail early var _ MeterServiceServer = (*Handler)(nil) @@ -78,7 +84,11 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb. if !ok { log = logger.DefaultLogger() } - buf := bytes.NewBuffer(nil) + + buf := bufPool.Get().(*bytes.Buffer) + defer bufPool.Put(buf) + buf.Reset() + w := io.Writer(buf) 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 } - rsp.Data = buf.Bytes() + w.Write(rsp.Data) return nil } -- 2.45.2 From 3e1b68f0fe559fcbcf1a58f90c597deb6dfc592a Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Mon, 11 Mar 2024 12:25:27 +0300 Subject: [PATCH 6/6] change rsp #153 --- handler/meter/meter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handler/meter/meter.go b/handler/meter/meter.go index e9882e3..037fdc7 100644 --- a/handler/meter/meter.go +++ b/handler/meter/meter.go @@ -107,7 +107,7 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb. return nil } - w.Write(rsp.Data) + rsp.Data = buf.Bytes() return nil } -- 2.45.2