-
Notifications
You must be signed in to change notification settings - Fork 907
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
Conversation
@zhzhuang-zju please help take a look. Thank you so much! |
/assign |
Codecov ReportAttention: Patch coverage is
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #6026 +/- ##
==========================================
+ Coverage 48.35% 48.36% +0.01%
==========================================
Files 665 666 +1
Lines 54831 54838 +7
==========================================
+ Hits 26512 26521 +9
+ Misses 26599 26597 -2
Partials 1720 1720
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
/retest |
pkg/util/grpcconnection/config.go
Outdated
cc, err = createGRPCConnection(path, timeout, opts...) | ||
if err == nil { | ||
return cc, nil | ||
cc, err = grpc.NewClient(path, opts...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@seanlaii I found that grpc.NewClient
also returns a client when path doesn't exist, with no errors reported. That's not what we expected.
Can we proceed to refine according to the method of validating grpc configuration mentioned in the document?
Some users of Dial use it as a way to validate the configuration of their system. If you wish to maintain this behavior but migrate to NewClient, you can call GetState, then Connect if the state is Idle and WaitForStateChange until the channel is connected. However, if this fails, it does not mean that your configuration was bad - it could also mean the service is not reachable by the client due to connectivity reasons.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! Thanks for the advice!
9ced984
to
e611c35
Compare
// 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...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
Signed-off-by: wei-chenglai <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
@chaosi-zju @zhzhuang-zju
Do you have any further comments?
// 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...) |
There was a problem hiding this comment.
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.
/remove-kind deprecation |
/lgtm, thanks @seanlaii |
//nolint:staticcheck | ||
grpc.WithBlock(), | ||
} | ||
var opts []grpc.DialOption |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/approve
/lgtm PS: re-trigger prow bot |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: RainbowMango The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
What type of PR is this?
/kind deprecation
What this PR does / why we need it:
grpc.DialContext
andgrpc.WithBlock
are deprecatedWhich issue(s) this PR fixes:
Part of #5796
Special notes for your reviewer:
Replace
grpc.DialContext
andgrpc.WithBlock
withgrpc.NewClient
to follow the recommended pattern in https://github.com/grpc/grpc-go/blob/master/Documentation/anti-patterns.md.Since we only allow users to configure the authentication setting of the grpc client without any custom dial options, I think we should be safe to perform the replacement.
Does this PR introduce a user-facing change?: