From 71e740b6e8b756f2419809068948d1832c3103ca Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Mon, 18 Mar 2024 16:00:49 +0300 Subject: [PATCH 1/7] add Flush --- handler/meter/meter.go | 2 ++ handler/meter/meter_test.go | 49 +++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 handler/meter/meter_test.go diff --git a/handler/meter/meter.go b/handler/meter/meter.go index 4157cd9..3cbec16 100644 --- a/handler/meter/meter.go +++ b/handler/meter/meter.go @@ -92,6 +92,7 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb. if md, ok := metadata.FromContext(ctx); gzipAccepted(md) && ok { md.Set(contentEncodingHeader, "gzip") + ctx = metadata.NewIncomingContext(ctx, md) gz := gzipPool.Get().(*gzip.Writer) defer gzipPool.Put(gz) @@ -99,6 +100,7 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb. defer gz.Close() w = gz + gz.Flush() } if err := h.opts.Meter.Write(w, h.opts.MeterOptions...); err != nil { diff --git a/handler/meter/meter_test.go b/handler/meter/meter_test.go new file mode 100644 index 0000000..a3b7f51 --- /dev/null +++ b/handler/meter/meter_test.go @@ -0,0 +1,49 @@ +package meter + +import ( + "context" + "testing" + + codecpb "go.unistack.org/micro-proto/v4/codec" +) + +func TestHandler_Metrics(t *testing.T) { + type fields struct { + opts Options + } + type args struct { + ctx context.Context + req *codecpb.Frame + rsp *codecpb.Frame + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + { + "Test #1", + fields{ + opts: NewOptions(), + }, + args{ + context.Background(), + &codecpb.Frame{Data: []byte("gzip")}, + &codecpb.Frame{}, + }, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h := &Handler{ + opts: tt.fields.opts, + } + if err := h.Metrics(tt.args.ctx, tt.args.req, tt.args.rsp); (err != nil) != tt.wantErr { + t.Errorf("Metrics() error = %v, wantErr %v", err, tt.wantErr) + } + t.Logf("RSP: %v", tt.args.rsp.Data) + }) + } +} -- 2.45.2 From ebd953de1cbae987d0ad7366f3d6846d659b77f1 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Mon, 18 Mar 2024 16:03:26 +0300 Subject: [PATCH 2/7] New metadata --- handler.go | 50 +++++++++++++++++++++++++------------------------- http.go | 4 ++-- util.go | 10 ++++------ 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/handler.go b/handler.go index 23cb470..803a1c7 100644 --- a/handler.go +++ b/handler.go @@ -109,20 +109,20 @@ func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error) md = metadata.New(len(r.Header) + 8) } for k, v := range r.Header { - md[k] = v + md[k] = v[0] } - md["RemoteAddr"] = []string{r.RemoteAddr} - md["Method"] = []string{r.Method} - md["URL"] = []string{r.URL.String()} - md["Proto"] = []string{r.Proto} - md["Content-Length"] = []string{fmt.Sprintf("%d", r.ContentLength)} - md["Transfer-Encoding"] = r.TransferEncoding - md["Host"] = []string{r.Host} - md["RequestURI"] = []string{r.RequestURI} + md["RemoteAddr"] = r.RemoteAddr + md["Method"] = r.Method + md["URL"] = r.URL.String() + md["Proto"] = r.Proto + md["Content-Length"] = fmt.Sprintf("%d", r.ContentLength) + md["Transfer-Encoding"] = r.TransferEncoding[0] + md["Host"] = r.Host + md["RequestURI"] = r.RequestURI if r.TLS != nil { - md["TLS"] = []string{"true"} - md["TLS-ALPN"] = []string{r.TLS.NegotiatedProtocol} - md["TLS-ServerName"] = []string{r.TLS.ServerName} + md["TLS"] = "true" + md["TLS-ALPN"] = r.TLS.NegotiatedProtocol + md["TLS-ServerName"] = r.TLS.ServerName } ctx = metadata.NewIncomingContext(ctx, md) @@ -290,7 +290,7 @@ func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error) w.Header().Set(metadata.HeaderContentType, ct) if md, ok := metadata.FromOutgoingContext(ctx); ok { for k, v := range md { - w.Header()[k] = v + w.Header()[k] = []string{v} } } if md := getRspHeader(ctx); md != nil { @@ -349,23 +349,23 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { md = metadata.New(len(r.Header) + 8) } for k, v := range r.Header { - md[k] = v + md[k] = v[0] } - md["RemoteAddr"] = []string{r.RemoteAddr} + md["RemoteAddr"] = r.RemoteAddr if r.TLS != nil { - md["Scheme"] = []string{"https"} + md["Scheme"] = "https" } else { - md["Scheme"] = []string{"http"} + md["Scheme"] = "http" } - md["Method"] = []string{r.Method} - md["URL"] = []string{r.URL.String()} - md["Proto"] = []string{r.Proto} - md["Content-Length"] = []string{fmt.Sprintf("%d", r.ContentLength)} + md["Method"] = r.Method + md["URL"] = r.URL.String() + md["Proto"] = r.Proto + md["Content-Length"] = fmt.Sprintf("%d", r.ContentLength) if len(r.TransferEncoding) > 0 { - md["Transfer-Encoding"] = r.TransferEncoding + md["Transfer-Encoding"] = r.TransferEncoding[0] } - md["Host"] = []string{r.Host} - md["RequestURI"] = []string{r.RequestURI} + md["Host"] = r.Host + md["RequestURI"] = r.RequestURI ctx = metadata.NewIncomingContext(ctx, md) path := r.URL.Path @@ -550,7 +550,7 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Set(metadata.HeaderContentType, ct) if md, ok := metadata.FromOutgoingContext(ctx); ok { for k, v := range md { - w.Header()[k] = v + w.Header()[k] = []string{v} } } if md := getRspHeader(ctx); md != nil { diff --git a/http.go b/http.go index d8f41e9..9dfdffd 100644 --- a/http.go +++ b/http.go @@ -217,7 +217,7 @@ func (h *Server) newHTTPHandler(handler interface{}, opts ...options.Option) *ht pth := &patHandler{mtype: mtype, name: name, rcvr: rcvr} hdlr.name = name - if err := hdlr.handlers.Insert(md["Method"], md["Path"][0], pth); err != nil { + if err := hdlr.handlers.Insert([]string{md["Method"]}, md["Path"], pth); err != nil { h.opts.Logger.Error(h.opts.Context, fmt.Sprintf("cant add handler for %s %s", md["Method"][0], md["Path"][0])) } @@ -468,7 +468,7 @@ func (h *Server) Start() error { go func() { if cerr := hs.Serve(ts); cerr != nil && !errors.Is(cerr, net.ErrClosed) { - h.opts.Logger.Error(h.opts.Context, cerr) + h.opts.Logger.Error(h.opts.Context, cerr.Error()) } }() diff --git a/util.go b/util.go index d856146..d07d3ad 100644 --- a/util.go +++ b/util.go @@ -33,13 +33,11 @@ func FillRequest(ctx context.Context, req interface{}, opts ...FillRequestOption cookies := md["Cookie"] cmd := make(map[string]string, len(cookies)) - for _, cookie := range cookies { - kv := strings.Split(cookie, "=") - if len(kv) != 2 { - continue - } - cmd[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1]) + kv := strings.Split(cookies, "=") + if len(kv) != 2 { + return nil } + cmd[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1]) for idx := 0; idx < len(options.cookies)/2; idx += 2 { k := http.CanonicalHeaderKey(options.cookies[idx]) v, ok := cmd[k] -- 2.45.2 From 6ceffb2d2d42587f2ccfa216979f307fa091503e Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Mon, 18 Mar 2024 16:19:18 +0300 Subject: [PATCH 3/7] change set md --- handler/meter/meter.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/handler/meter/meter.go b/handler/meter/meter.go index 3cbec16..d062884 100644 --- a/handler/meter/meter.go +++ b/handler/meter/meter.go @@ -90,9 +90,8 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb. w := io.Writer(buf) - if md, ok := metadata.FromContext(ctx); gzipAccepted(md) && ok { + if md, ok := metadata.FromOutgoingContext(ctx); gzipAccepted(md) && ok { md.Set(contentEncodingHeader, "gzip") - ctx = metadata.NewIncomingContext(ctx, md) gz := gzipPool.Get().(*gzip.Writer) defer gzipPool.Put(gz) -- 2.45.2 From 71acc995de332e6eaf22dedd3e4740c73a0c932b Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Mon, 18 Mar 2024 16:21:24 +0300 Subject: [PATCH 4/7] change set md --- handler/meter/meter.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/handler/meter/meter.go b/handler/meter/meter.go index d062884..11c5931 100644 --- a/handler/meter/meter.go +++ b/handler/meter/meter.go @@ -90,8 +90,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 { + if md, ok := metadata.FromIncomingContext(ctx); gzipAccepted(md) && ok { md.Set(contentEncodingHeader, "gzip") + ctx = metadata.NewOutgoingContext(ctx, md) gz := gzipPool.Get().(*gzip.Writer) defer gzipPool.Put(gz) -- 2.45.2 From 3e79e73eaa6314af7c6a6898e4be7158af5e74c0 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Mon, 18 Mar 2024 16:36:08 +0300 Subject: [PATCH 5/7] change set md --- handler/meter/meter.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/handler/meter/meter.go b/handler/meter/meter.go index 11c5931..d062884 100644 --- a/handler/meter/meter.go +++ b/handler/meter/meter.go @@ -90,9 +90,8 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb. w := io.Writer(buf) - if md, ok := metadata.FromIncomingContext(ctx); gzipAccepted(md) && ok { + if md, ok := metadata.FromOutgoingContext(ctx); gzipAccepted(md) && ok { md.Set(contentEncodingHeader, "gzip") - ctx = metadata.NewOutgoingContext(ctx, md) gz := gzipPool.Get().(*gzip.Writer) defer gzipPool.Put(gz) -- 2.45.2 From 83fc7c54caeafa02fdbebb8317848356b18646db Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Mon, 18 Mar 2024 16:46:54 +0300 Subject: [PATCH 6/7] change set md --- handler/meter/meter.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/handler/meter/meter.go b/handler/meter/meter.go index d062884..fe0ae5f 100644 --- a/handler/meter/meter.go +++ b/handler/meter/meter.go @@ -45,6 +45,7 @@ type Options struct { Meter meter.Meter Name string MeterOptions []options.Option + Gzip bool } func Meter(m meter.Meter) Option { @@ -59,6 +60,12 @@ func Name(name string) Option { } } +func Gzip(g bool) Option { + return func(o *Options) { + o.Gzip = g + } +} + func MeterOptions(opts ...options.Option) Option { return func(o *Options) { o.MeterOptions = append(o.MeterOptions, opts...) @@ -66,7 +73,7 @@ func MeterOptions(opts ...options.Option) Option { } func NewOptions(opts ...Option) Options { - options := Options{Meter: meter.DefaultMeter} + options := Options{Meter: meter.DefaultMeter, Gzip: true} for _, o := range opts { o(&options) } @@ -90,8 +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 { - md.Set(contentEncodingHeader, "gzip") + if md, ok := metadata.FromIncomingContext(ctx); gzipAccepted(md) && ok && h.opts.Gzip { + omd, _ := metadata.FromOutgoingContext(ctx) + omd.Set(contentEncodingHeader, "gzip") gz := gzipPool.Get().(*gzip.Writer) defer gzipPool.Put(gz) -- 2.45.2 From 68738013ccad9efd3b8add0da33a2783e02b1981 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Mon, 18 Mar 2024 16:49:42 +0300 Subject: [PATCH 7/7] change set md --- handler/meter/meter.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/handler/meter/meter.go b/handler/meter/meter.go index fe0ae5f..30461d9 100644 --- a/handler/meter/meter.go +++ b/handler/meter/meter.go @@ -42,10 +42,10 @@ type Handler struct { type Option func(*Options) type Options struct { - Meter meter.Meter - Name string - MeterOptions []options.Option - Gzip bool + Meter meter.Meter + Name string + MeterOptions []options.Option + DisableCompress bool } func Meter(m meter.Meter) Option { @@ -60,9 +60,9 @@ func Name(name string) Option { } } -func Gzip(g bool) Option { +func DisableCompress(g bool) Option { return func(o *Options) { - o.Gzip = g + o.DisableCompress = g } } @@ -73,7 +73,7 @@ func MeterOptions(opts ...options.Option) Option { } func NewOptions(opts ...Option) Options { - options := Options{Meter: meter.DefaultMeter, Gzip: true} + options := Options{Meter: meter.DefaultMeter, DisableCompress: false} for _, o := range opts { o(&options) } @@ -97,7 +97,7 @@ func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb. w := io.Writer(buf) - if md, ok := metadata.FromIncomingContext(ctx); gzipAccepted(md) && ok && h.opts.Gzip { + if md, ok := metadata.FromIncomingContext(ctx); gzipAccepted(md) && ok && !h.opts.DisableCompress { omd, _ := metadata.FromOutgoingContext(ctx) omd.Set(contentEncodingHeader, "gzip") gz := gzipPool.Get().(*gzip.Writer) -- 2.45.2