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

goctl rpc client generate go file not reuse connection lead to goroutine leak #4642

Open
zou-xinhua opened this issue Feb 11, 2025 · 0 comments

Comments

@zou-xinhua
Copy link

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior, if applicable:

  1. The code is

    {{.head}}

package {{.filePackage}}

import (
"context"

{{.pbPackage}}
{{if ne .pbPackage .protoGoPackage}}{{.protoGoPackage}}{{end}}

"github.com/zeromicro/go-zero/zrpc"
"google.golang.org/grpc"

)

type (
{{.alias}}

{{.serviceName}} interface {
	{{.interface}}
}

default{{.serviceName}} struct {
	cli zrpc.Client
}

)

func New{{.serviceName}}(cli zrpc.Client) {{.serviceName}} {
return &default{{.serviceName}}{
cli: cli,
}
}

{{if .hasComment}}{{.comment}}{{end}}
func (m *default{{.serviceName}}) {{.method}}(ctx context.Context{{if .hasReq}}, in {{.pbRequest}}{{end}}, opts ...grpc.CallOption) ({{if .notStream}}{{.pbResponse}}, {{else}}{{.streamBody}},{{end}} error) {
client := {{.package}}.New{{.rpcServiceName}}Client(m.cli.Conn())
return client.{{.method}}(ctx{{if .hasReq}}, in{{end}}, opts...)
}


2. The error is


**Expected behavior**
A clear and concise description of what you expected to happen.
、、、
type defaultManager struct {
 cli    zrpc.Client
 client ManagerClient // 复用 gRPC 客户端
}

func NewManager(cli zrpc.Client) Manager {
 return &defaultManager{
     cli:    cli,
     client: NewManagerClient(cli.Conn()), // 初始化时创建客户端
 }
}

func (m *defaultManager) SetBaseHostResource(ctx context.Context, in *SetBaseHostResourceRequest, opts ...grpc.CallOption) (*SetBaseHostResourceResponse, error) {
 return m.client.SetBaseHostResource(ctx, in, opts...) // 直接使用复用的客户端
}
、、、
**Screenshots**
If applicable, add screenshots to help explain your problem.

**Environments (please complete the following information):**
- OS: [e.g. Linux]
- go-zero version [v1.3.2]
- goctl version [goctl version 1.3.2 darwin/amd64]

**More description**
Add any other context about the problem here.

![Image](https://github.com/user-attachments/assets/c35d39b1-0359-4f5e-9962-872bb1321e65)

@zou-xinhua zou-xinhua changed the title goctl rpc client go file not reuse connection lead to goroutine leak goctl rpc client generate go file not reuse connection lead to goroutine leak Feb 11, 2025
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