From 923f3ef1d39e53abe7aa0a2f7e3bf60f1b02b8f2 Mon Sep 17 00:00:00 2001 From: Evstigneev Denis Date: Fri, 20 Sep 2024 18:45:01 +0300 Subject: [PATCH 1/2] update deps Signed-off-by: Vasiliy Tolstov --- .gitignore | 16 ++++++++++++++++ go.mod | 15 +++++++++------ go.sum | 16 ++++++++++++++++ http.go | 8 +------- 4 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0ca2a00 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib +.idea + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ diff --git a/go.mod b/go.mod index 8e2693e..3b3eea2 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,15 @@ module go.unistack.org/micro-client-http/v3 -go 1.20 +go 1.21 -require go.unistack.org/micro/v3 v3.10.69 +toolchain go1.23.1 + +require go.unistack.org/micro/v3 v3.10.91 require ( - golang.org/x/sys v0.19.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect - google.golang.org/grpc v1.63.2 // indirect - google.golang.org/protobuf v1.33.0 // indirect + go.unistack.org/micro-proto/v3 v3.4.1 // indirect + golang.org/x/sys v0.25.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect + google.golang.org/grpc v1.67.0 // indirect + google.golang.org/protobuf v1.34.2 // indirect ) diff --git a/go.sum b/go.sum index 3483f31..b6a368d 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,29 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q= +go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo= go.unistack.org/micro/v3 v3.10.69 h1:V4g9LqUhzGab73U2aevnjtffCdJqGTuMfWY7fy4qGwI= go.unistack.org/micro/v3 v3.10.69/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg= +go.unistack.org/micro/v3 v3.10.88 h1:MxlzP+77Y6Kphb3lzHxROL4XfE/WdCQMQpnPv4D9Z8U= +go.unistack.org/micro/v3 v3.10.88/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg= +go.unistack.org/micro/v3 v3.10.91 h1:vuJY4tXwpqimwIkEJ3TozMYNVQQs+C5QMlQWPgSY/YM= +go.unistack.org/micro/v3 v3.10.91/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg= golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be h1:LG9vZxsWGOmUKieR8wPAUR3u3MpnYFQZROPIMaXh7/A= google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 h1:pPJltXNxVzT4pK9yD8vR9X75DaWYYmLGMsEvBfFQZzQ= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU= google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM= google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA= +google.golang.org/grpc v1.66.2 h1:3QdXkuq3Bkh7w+ywLdLvM56cmGvQHUMZpiCzt6Rqaoo= +google.golang.org/grpc v1.66.2/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y= +google.golang.org/grpc v1.67.0 h1:IdH9y6PF5MPSdAntIcpjQ+tXO41pcQsfZV2RxtQgVcw= +google.golang.org/grpc v1.67.0/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA= google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= diff --git a/http.go b/http.go index 29ff3fa..cae307c 100644 --- a/http.go +++ b/http.go @@ -51,7 +51,7 @@ type httpClient struct { func newRequest(ctx context.Context, log logger.Logger, addr string, req client.Request, ct string, cf codec.Codec, msg interface{}, opts client.CallOptions) (*http.Request, error) { var tags []string - var parameters map[string]map[string]string + parameters := map[string]map[string]string{} scheme := "http" method := http.MethodPost body := "*" // as like google api http annotation @@ -82,9 +82,6 @@ func newRequest(ctx context.Context, log logger.Logger, addr string, req client. tags = t } if k, ok := opts.Context.Value(headerKey{}).([]string); ok && len(k) > 0 { - if parameters == nil { - parameters = make(map[string]map[string]string) - } m, ok := parameters["header"] if !ok { m = make(map[string]string) @@ -95,9 +92,6 @@ func newRequest(ctx context.Context, log logger.Logger, addr string, req client. } } if k, ok := opts.Context.Value(cookieKey{}).([]string); ok && len(k) > 0 { - if parameters == nil { - parameters = make(map[string]map[string]string) - } m, ok := parameters["cookie"] if !ok { m = make(map[string]string) -- 2.45.2 From 0677ac1b863e2d39fa9d135b5e4c777c9edc5a06 Mon Sep 17 00:00:00 2001 From: Evstigneev Denis Date: Wed, 18 Dec 2024 15:43:35 +0300 Subject: [PATCH 2/2] lintering && fix call hook --- .gitignore | 31 +++++++++++++++++++++++++++---- http.go | 53 +++++++++++++++++++++++++++++------------------------ stream.go | 8 ++++---- util.go | 8 ++++---- 4 files changed, 64 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 0ca2a00..c2fff38 100644 --- a/.gitignore +++ b/.gitignore @@ -1,16 +1,39 @@ +# Develop tools +/.vscode/ +/.idea/ +.idea +.vscode + # Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib -.idea -# Test binary, built with `go test -c` +# Folders +_obj +_test +_build +.DS_Store + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +# Test binary, build with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out -# Dependency directories (remove the comment below to include it) -# vendor/ +# vim temp files +*~ +*.swp +*.swo diff --git a/http.go b/http.go index cae307c..25b9e0d 100644 --- a/http.go +++ b/http.go @@ -6,7 +6,7 @@ import ( "bytes" "context" "fmt" - "io/ioutil" + "io" "net" "net/http" "net/url" @@ -46,12 +46,11 @@ type httpClient struct { httpcli *http.Client opts client.Options sync.RWMutex - init bool } func newRequest(ctx context.Context, log logger.Logger, addr string, req client.Request, ct string, cf codec.Codec, msg interface{}, opts client.CallOptions) (*http.Request, error) { var tags []string - parameters := map[string]map[string]string{} + var parameters map[string]map[string]string scheme := "http" method := http.MethodPost body := "*" // as like google api http annotation @@ -82,6 +81,9 @@ func newRequest(ctx context.Context, log logger.Logger, addr string, req client. tags = t } if k, ok := opts.Context.Value(headerKey{}).([]string); ok && len(k) > 0 { + if parameters == nil { + parameters = make(map[string]map[string]string) + } m, ok := parameters["header"] if !ok { m = make(map[string]string) @@ -92,6 +94,9 @@ func newRequest(ctx context.Context, log logger.Logger, addr string, req client. } } if k, ok := opts.Context.Value(cookieKey{}).([]string); ok && len(k) > 0 { + if parameters == nil { + parameters = make(map[string]map[string]string) + } m, ok := parameters["cookie"] if !ok { m = make(map[string]string) @@ -118,7 +123,7 @@ func newRequest(ctx context.Context, log logger.Logger, addr string, req client. u, err = u.Parse(path) if err != nil { - return nil, errors.BadRequest("go.micro.client", err.Error()) + return nil, errors.BadRequest("go.micro.client", "%+v", err) } var nmsg interface{} @@ -129,12 +134,12 @@ func newRequest(ctx context.Context, log logger.Logger, addr string, req client. } if err != nil { - return nil, errors.BadRequest("go.micro.client", err.Error()) + return nil, errors.BadRequest("go.micro.client", "%+v", err) } u, err = url.Parse(fmt.Sprintf("%s://%s%s", scheme, host, path)) if err != nil { - return nil, errors.BadRequest("go.micro.client", err.Error()) + return nil, errors.BadRequest("go.micro.client", "%+v", err) } var cookies []*http.Cookie @@ -177,11 +182,11 @@ func newRequest(ctx context.Context, log logger.Logger, addr string, req client. for k, required := range vm { v, err = rutil.StructFieldByPath(msg, k) if err != nil { - return nil, errors.BadRequest("go.micro.client", err.Error()) + return nil, errors.BadRequest("go.micro.client", "%+v", err) } if rutil.IsZero(v) { if required == "true" { - return nil, errors.BadRequest("go.micro.client", fmt.Sprintf("required field %s not set", k)) + return nil, errors.BadRequest("go.micro.client", "required field %s not set", k) } continue } @@ -197,12 +202,12 @@ func newRequest(ctx context.Context, log logger.Logger, addr string, req client. b, err := cf.Marshal(nmsg) if err != nil { - return nil, errors.BadRequest("go.micro.client", err.Error()) + return nil, errors.BadRequest("go.micro.client", "%+v", err) } var hreq *http.Request if len(b) > 0 { - hreq, err = http.NewRequestWithContext(ctx, method, u.String(), ioutil.NopCloser(bytes.NewBuffer(b))) + hreq, err = http.NewRequestWithContext(ctx, method, u.String(), io.NopCloser(bytes.NewBuffer(b))) hreq.ContentLength = int64(len(b)) header.Set("Content-Length", fmt.Sprintf("%d", hreq.ContentLength)) } else { @@ -210,7 +215,7 @@ func newRequest(ctx context.Context, log logger.Logger, addr string, req client. } if err != nil { - return nil, errors.BadRequest("go.micro.client", err.Error()) + return nil, errors.BadRequest("go.micro.client", "%+v", err) } hreq.Header = header @@ -233,7 +238,7 @@ func (c *httpClient) call(ctx context.Context, addr string, req client.Request, cf, err := c.newCodec(ct) if err != nil { - return errors.BadRequest("go.micro.client", err.Error()) + return errors.BadRequest("go.micro.client", "%+v", err) } hreq, err := newRequest(ctx, c.opts.Logger, addr, req, ct, cf, req.Body(), opts) if err != nil { @@ -246,14 +251,14 @@ func (c *httpClient) call(ctx context.Context, addr string, req client.Request, switch err := err.(type) { case *url.Error: if err, ok := err.Err.(net.Error); ok && err.Timeout() { - return errors.Timeout("go.micro.client", err.Error()) + return errors.Timeout("go.micro.client", "%+v", err) } case net.Error: if err.Timeout() { - return errors.Timeout("go.micro.client", err.Error()) + return errors.Timeout("go.micro.client", "%+v", err) } } - return errors.InternalServerError("go.micro.client", err.Error()) + return errors.InternalServerError("go.micro.client", "%+v", err) } defer hrsp.Body.Close() @@ -270,12 +275,12 @@ func (c *httpClient) stream(ctx context.Context, addr string, req client.Request // get codec cf, err := c.newCodec(ct) if err != nil { - return nil, errors.BadRequest("go.micro.client", err.Error()) + return nil, errors.BadRequest("go.micro.client", "%+v", err) } cc, err := (c.httpcli.Transport).(*http.Transport).DialContext(ctx, "tcp", addr) if err != nil { - return nil, errors.InternalServerError("go.micro.client", fmt.Sprintf("Error dialing: %v", err)) + return nil, errors.InternalServerError("go.micro.client", "Error dialing: %v", err) } return &httpStream{ @@ -318,7 +323,7 @@ func (c *httpClient) Init(opts ...client.Option) error { c.funcPublish = c.fnPublish c.funcBatchPublish = c.fnBatchPublish - c.opts.Hooks.EachNext(func(hook options.Hook) { + c.opts.Hooks.EachPrev(func(hook options.Hook) { switch h := hook.(type) { case client.HookCall: c.funcCall = h(c.funcCall) @@ -424,7 +429,7 @@ func (c *httpClient) fnCall(ctx context.Context, req client.Request, rsp interfa // call backoff first. Someone may want an initial start delay t, err := callOpts.Backoff(ctx, req, i) if err != nil { - return errors.InternalServerError("go.micro.client", err.Error()) + return errors.InternalServerError("go.micro.client", "%+v", err) } // only sleep if greater than 0 @@ -438,7 +443,7 @@ func (c *httpClient) fnCall(ctx context.Context, req client.Request, rsp interfa // TODO apply any filtering here routes, err = c.opts.Lookup(ctx, req, callOpts) if err != nil { - return errors.InternalServerError("go.micro.client", err.Error()) + return errors.InternalServerError("go.micro.client", "%+v", err) } // balance the list of nodes @@ -583,7 +588,7 @@ func (c *httpClient) fnStream(ctx context.Context, req client.Request, opts ...c // call backoff first. Someone may want an initial start delay t, cerr := callOpts.Backoff(ctx, req, i) if cerr != nil { - return nil, errors.InternalServerError("go.micro.client", cerr.Error()) + return nil, errors.InternalServerError("go.micro.client", "%+v", cerr) } // only sleep if greater than 0 @@ -597,7 +602,7 @@ func (c *httpClient) fnStream(ctx context.Context, req client.Request, opts ...c // TODO apply any filtering here routes, err = c.opts.Lookup(ctx, req, callOpts) if err != nil { - return nil, errors.InternalServerError("go.micro.client", err.Error()) + return nil, errors.InternalServerError("go.micro.client", "%+v", err) } // balance the list of nodes @@ -722,12 +727,12 @@ func (c *httpClient) publish(ctx context.Context, ps []client.Message, opts ...c // use codec for payload cf, err := c.newCodec(p.ContentType()) if err != nil { - return errors.InternalServerError("go.micro.client", err.Error()) + return errors.InternalServerError("go.micro.client", "%+v", err) } // set the body b, err := cf.Marshal(p.Payload()) if err != nil { - return errors.InternalServerError("go.micro.client", err.Error()) + return errors.InternalServerError("go.micro.client", "%+v", err) } body = b } diff --git a/stream.go b/stream.go index c69ea44..3ba9cec 100644 --- a/stream.go +++ b/stream.go @@ -90,7 +90,7 @@ func (h *httpStream) Recv(msg interface{}) error { hrsp, err := http.ReadResponse(h.reader, new(http.Request)) if err != nil { - return errors.InternalServerError("go.micro.client", err.Error()) + return errors.InternalServerError("go.micro.client", "%+v", err) } defer hrsp.Body.Close() @@ -136,7 +136,7 @@ func (h *httpStream) parseRsp(ctx context.Context, log logger.Logger, hrsp *http if log.V(logger.ErrorLevel) { log.Error(ctx, "failed to read body", err) } - return errors.InternalServerError("go.micro.client", string(buf)) + return errors.InternalServerError("go.micro.client", "%s", buf) } } @@ -146,7 +146,7 @@ func (h *httpStream) parseRsp(ctx context.Context, log logger.Logger, hrsp *http if hrsp.StatusCode < 400 { if err = cf.Unmarshal(buf, rsp); err != nil { - return errors.InternalServerError("go.micro.client", err.Error()) + return errors.InternalServerError("go.micro.client", "%+v", err) } return nil } @@ -163,7 +163,7 @@ func (h *httpStream) parseRsp(ctx context.Context, log logger.Logger, hrsp *http } if cerr := cf.Unmarshal(buf, rerr); cerr != nil { - return errors.InternalServerError("go.micro.client", cerr.Error()) + return errors.InternalServerError("go.micro.client", "%+v", cerr) } if err, ok = rerr.(error); !ok { diff --git a/util.go b/util.go index 95e0e6b..ea8778b 100644 --- a/util.go +++ b/util.go @@ -334,7 +334,7 @@ func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp inte if h.opts.Logger.V(logger.ErrorLevel) { h.opts.Logger.Error(ctx, "failed to read body", err) } - return errors.InternalServerError("go.micro.client", string(buf)) + return errors.InternalServerError("go.micro.client", "%s", buf) } } @@ -343,7 +343,7 @@ func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp inte if h.opts.Logger.V(logger.DebugLevel) { h.opts.Logger.Debug(ctx, fmt.Sprintf("response with %v unknown content-type %s %s", hrsp.Header, ct, buf)) } - return errors.InternalServerError("go.micro.client", cerr.Error()) + return errors.InternalServerError("go.micro.client", "%+v", cerr) } if h.opts.Logger.V(logger.DebugLevel) { @@ -353,7 +353,7 @@ func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp inte // succeseful response if hrsp.StatusCode < 400 { if err = cf.Unmarshal(buf, rsp); err != nil { - return errors.InternalServerError("go.micro.client", err.Error()) + return errors.InternalServerError("go.micro.client", "%+v", err) } return nil } @@ -373,7 +373,7 @@ func (h *httpClient) parseRsp(ctx context.Context, hrsp *http.Response, rsp inte } if cerr := cf.Unmarshal(buf, rerr); cerr != nil { - return errors.InternalServerError("go.micro.client", cerr.Error()) + return errors.InternalServerError("go.micro.client", "%+v", cerr) } if err, ok = rerr.(error); !ok { -- 2.45.2