Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] go.micro.client.transport write: broken pipe #2686

Open
uyoaix opened this issue Dec 31, 2023 · 0 comments
Open

[BUG] go.micro.client.transport write: broken pipe #2686

uyoaix opened this issue Dec 31, 2023 · 0 comments

Comments

@uyoaix
Copy link

uyoaix commented Dec 31, 2023

Describe the bug

send rpc request with client.Call(ctx, req, rsp, opts ....) method, one is ok ,one failed

first request ok , second request failed with : {"id":"go.micro.client.transport","code":500,"detail":"readfrom tcp 192.168.240.26:51266-\u003e192.168.240.32:45054: write tcp 192.168.240.26:51266-\u003e192.168.240.32:45054: write: broken pipe","status":"Internal Server Error"}

then third request ok , fourth failed with same err {"id":"go.micro.client.transport","code":500,"detail":"readfrom tcp 192.168.240.26:51266-\u003e192.168.240.32:45054: write tcp 192.168.240.26:51266-\u003e192.168.240.32:45054: write: broken pipe","status":"Internal Server Error"}

fifth is ok ,and sixth failed with same err {"id":"go.micro.client.transport","code":500,"detail":"readfrom tcp 192.168.240.26:51266-\u003e192.168.240.32:45054: write tcp 192.168.240.26:51266-\u003e192.168.240.32:45054: write: broken pipe","status":"Internal Server Error"}
then same result ...

  1. What are you trying to do?
    when version v2.9.2: local and centos7.9 docker all ok
    then upgrade to v4.10.2 : local ok , but centos7.9 docker failed
    same code , in my local wiindows matchine is ok , then deploy in centos7.9 with docker , occoured that error

  2. What did you expect to happen?
    expect every request is ok

  3. What happens instead?
    one is ok , one failed

How to reproduce the bug

upgrade from v2.9.2 to v4.10.2
browser http request -> custom gateway -> rpc call service A

gateway service send rpc with client.Call method

         api "go-micro.dev/v4/api/proto"
	"go-micro.dev/v4/client"
	"go-micro.dev/v4/selector"
	"go-micro.dev/v4/util/ctx"

// convert http request params to rpc protorequest
func RequestToProto(r *http.Request) (*api.Request, error) {
	if err := r.ParseForm(); err != nil {
		return nil, fmt.Errorf("Error parsing form: %v", err)
	}
	req := &api.Request{
		Path:   r.URL.Path,
		Method: r.Method,
		Header: make(map[string]*api.Pair),
		Get:    make(map[string]*api.Pair),
		Post:   make(map[string]*api.Pair),
		Url:    r.URL.String(),
	}
	ct, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
	if err != nil {
		ct = "application/x-www-form-urlencoded"
		r.Header.Set("Content-Type", ct)
	}
	switch ct {
	case "application/x-www-form-urlencoded":
	default:
		data, _ := io.ReadAll(r.Body)
		req.Body = string(data)
	}
	if ip, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
		if prior, ok := r.Header["X-Forwarded-For"]; ok {
			ip = strings.Join(prior, ", ") + ", " + ip
		}
		req.Header["X-Forwarded-For"] = &api.Pair{
			Key:    "X-Forwarded-For",
			Values: []string{ip},
		}
	}
	req.Header["Host"] = &api.Pair{
		Key:    "Host",
		Values: []string{r.Host},
	}
	for key, vals := range r.URL.Query() {
		header, ok := req.Get[key]
		if !ok {
			header = &api.Pair{
				Key: key,
			}
			req.Get[key] = header
		}
		header.Values = vals
	}
	for key, vals := range r.PostForm {
		header, ok := req.Post[key]
		if !ok {
			header = &api.Pair{
				Key: key,
			}
			req.Post[key] = header
		}
		header.Values = vals
	}

	for key, vals := range r.Header {
		header, ok := req.Header[key]
		if !ok {
			header = &api.Pair{
				Key: key,
			}
			req.Header[key] = header
		}
		header.Values = vals
	}

	return req, nil
}

// api: 127.0.0.1:8080/backend/serviceA/methodA
// remote rpc server name in consul registry: go.micro.api.serverA
// remote  serverA method:  methodA


func DefaultHandler(writer http.ResponseWriter, request *http.Request) {
  requestBodyBytes, err := io.ReadAll(request.Body)
  if err != nil {
   logs.Error(err)
  }

  // trim gateway prefix: /backend
  request.Body = io.NopCloser(bytes.NewReader(requestBodyBytes))
  request.URL.Path = strings.TrimPrefix(request.RequestURI, "/backend")
  request.RequestURI = strings.TrimPrefix(request.RequestURI, "/backend")
  
  protorequest := RequestToProto(request)

  req, rsp := client.NewRequest(service.Name, service.Endpoint.Name, protorequest), &api.Response{}
  err = client.Call(ctx.FromRequest(request), req, rsp,
    client.WithSelectOption(selector.WithStrategy(microutil.Strategy(service.Versions))),
    client.WithCallWrapper(),
    client.WithRequestTimeout(time.Second*60),
 )
if err != nil {
  fmt.Println("", "rpc failed %v: ", service.Name, err)
  responseData, _ := json.Marshal(response.NewResponse(http.StatusInternalServerError, err.Error(), nil))
  writer.WriteHeader(http.StatusInternalServerError)
  writer.Write([]byte(responseData))
  return
 }
}

// http request
service.HandleFunc("/", handle.DefaultHandler)

If possible, please include a minimal code snippet here.

Environment

Go Version: please paste go version output here

please paste `go env` output here
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\xxx\AppData\Local\go-build
set GOENV=C:\Users\xxx\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOMODCACHE=D:\work\goProject\pkg\mod
set GOOS=windows
set GOPATH=D:\work\goProject
set GOPRIVATE=private-gitlab*
set GOPROXY=https://goproxy.cn,direct
set GOROOT=D:\Program Files\Go
set GOSUMDB=off
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=D:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.21.5
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant