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

Fix grpc client connection leak (VersionGet, ProcessLog, ProcessWatch) #93

Merged
merged 1 commit into from
Jul 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions pkg/client/process_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,21 @@ func (cli *ProcessManagerClient) ProcessLog(name string) (*api.LogStream, error)
return nil, fmt.Errorf("failed to get process: missing required parameter name")
}

var err error
conn, err := grpc.Dial(cli.Address, grpc.WithInsecure())
if err != nil {
return nil, fmt.Errorf("cannot connect process manager service to %v: %v", cli.Address, err)
}

client := rpc.NewProcessManagerServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), types.GRPCServiceTimeout)
defer func() {
if err != nil {
cancel()
conn.Close()
}
}()

client := rpc.NewProcessManagerServiceClient(conn)
stream, err := client.ProcessLog(ctx, &rpc.LogRequest{
Name: name,
})
Expand All @@ -142,15 +150,23 @@ func (cli *ProcessManagerClient) ProcessLog(name string) (*api.LogStream, error)
}

func (cli *ProcessManagerClient) ProcessWatch() (*api.ProcessStream, error) {
var err error
conn, err := grpc.Dial(cli.Address, grpc.WithInsecure())
if err != nil {
return nil, fmt.Errorf("cannot connect process manager service to %v: %v", cli.Address, err)
}

ctx, cancel := context.WithCancel(context.Background())
defer func() {
if err != nil {
cancel()
conn.Close()
}
}()

// Don't cleanup the Client here, we don't know when the user will be done with the Stream. Pass it to the wrapper
// and allow the user to take care of it.
client := rpc.NewProcessManagerServiceClient(conn)
ctx, cancel := context.WithCancel(context.Background())
stream, err := client.ProcessWatch(ctx, &empty.Empty{})
if err != nil {
return nil, errors.Wrapf(err, "failed to open process update stream")
Expand Down Expand Up @@ -198,6 +214,7 @@ func (cli *ProcessManagerClient) VersionGet() (*meta.VersionOutput, error) {
if err != nil {
return nil, fmt.Errorf("cannot connect process manager service to %v: %v", cli.Address, err)
}
defer conn.Close()

client := rpc.NewProcessManagerServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), types.GRPCServiceTimeout)
Expand Down