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

Replace grpc.DialContext and grpc.WithBlock with grpc.NewClient #6026

Merged
merged 1 commit into from
Jan 15, 2025
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
36 changes: 23 additions & 13 deletions pkg/util/grpcconnection/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
grpccredentials "google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
Expand Down Expand Up @@ -102,11 +103,7 @@ func (s *ServerConfig) NewServer() (*grpc.Server, error) {

// DialWithTimeOut will attempt to create a client connection based on the given targets, one at a time, until a client connection is successfully established.
func (c *ClientConfig) DialWithTimeOut(paths []string, timeout time.Duration) (*grpc.ClientConn, error) {
opts := []grpc.DialOption{
// grpc.WithBlock is deprecated. TODO: Perhaps need to reconsider the approach in a future PR
//nolint:staticcheck
grpc.WithBlock(),
}
var opts []grpc.DialOption
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seanlaii Thanks for your work! Just a suggestion, function name DialWithTimeOut can be considered to be changed to NewClient

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with the DialWithTimeOut which clearly explains actions in this function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get it, please ignore this comment


var cred grpccredentials.TransportCredentials
if c.ServerAuthCAFile == "" && !c.InsecureSkipServerVerify {
Expand Down Expand Up @@ -136,9 +133,7 @@ func (c *ClientConfig) DialWithTimeOut(paths []string, timeout time.Duration) (*
}
cred = grpccredentials.NewTLS(config)
}

opts = append(opts, grpc.WithTransportCredentials(cred))

var cc *grpc.ClientConn
var err error
var allErrs []error
Expand All @@ -147,7 +142,7 @@ func (c *ClientConfig) DialWithTimeOut(paths []string, timeout time.Duration) (*
if err == nil {
return cc, nil
}
allErrs = append(allErrs, err)
allErrs = append(allErrs, fmt.Errorf("dial %s error: %v", path, err))
}

return nil, utilerrors.NewAggregate(allErrs)
Expand All @@ -157,12 +152,27 @@ func createGRPCConnection(path string, timeout time.Duration, opts ...grpc.DialO
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

// grpc.DialContext is deprecated. TODO: Perhaps need to reconsider the approach in a future PR
//nolint:staticcheck
cc, err := grpc.DialContext(ctx, path, opts...)
cc, err := grpc.NewClient(path, opts...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cc, err := grpc.NewClient(path, opts...)
conn, err = grpc.NewClient(path, opts...)

We can use the variable defined in return types, which makes the defer function clear about which error it referring to.

Copy link
Contributor Author

@seanlaii seanlaii Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it and encountered an issue: if there is an error, conn will be set to nil, resulting in nil dereference when trying to use conn.Close in defer function. Therefore, I changed it back, which also follows the implementation in here.
Please take a look. Thank you!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it and encountered an issue: if there is an error, conn will be set to nil, resulting in nil dereference when trying to use conn.Close in defer function.

Nice finding! Seems you tested the abnormal case, thanks.

if err != nil {
return nil, fmt.Errorf("dial %s error: %v", path, err)
return nil, err
}
defer func() {
if err != nil {
cc.Close()
}
}()

return cc, nil
// A blocking dial blocks until the clientConn is ready.
for {
state := cc.GetState()
if state == connectivity.Idle {
cc.Connect()
}
if state == connectivity.Ready {
return cc, nil
}
if !cc.WaitForStateChange(ctx, state) {
return nil, fmt.Errorf("timeout waiting for connection to %s, state is %s", path, state)
}
}
}