-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
chore: expose NewClient
method to end users
#7010
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3575d42
chore: expose `NewClient` method to end users
bruuuuuuuce 5455592
chore: fix build, update usages to use new NewClient naming
bruuuuuuuce 06bb15f
fix: use dns resolver when calling NewClient directly
bruuuuuuuce 3db24a8
chore: fix spelling error
bruuuuuuuce fec57e6
chore: GetDefaultSchemeOrDns -> GetDefaultSchemeOrDNS
bruuuuuuuce 50f2e9b
fix: remove all user visisble changes aside from new NewClient func
bruuuuuuuce 20d1967
Merge branch 'grpc:master' into patch-1
bruuuuuuuce 5b9e5d3
fix: add internal.UserSetDefaultScheme to clientconn.go
bruuuuuuuce 461c791
fix: unit tests failing due to default resolver being dns
bruuuuuuuce 4884fef
Merge branch 'master' into patch-1
bruuuuuuuce 9b16013
fix: set internal.UserSetDefaultScheme inside of SetDefaultScheme()
bruuuuuuuce 87a7cb7
chore: run make deps
bruuuuuuuce d405471
chore: fix import ordering to fix build
bruuuuuuuce bfcfd81
chore: fix import ordering for credentials/google/xds.go
bruuuuuuuce 6ad5013
fix: add package comment to internal/xds/xds.go file
bruuuuuuuce ed128db
chore: pr nits, clean up comments, reset resolver state for connectio…
bruuuuuuuce 5938a72
Merge branch 'grpc:master' into patch-1
bruuuuuuuce File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,34 +28,87 @@ import ( | |
|
||
"github.com/google/go-cmp/cmp" | ||
"google.golang.org/grpc/credentials/insecure" | ||
"google.golang.org/grpc/internal" | ||
"google.golang.org/grpc/internal/testutils" | ||
|
||
"google.golang.org/grpc/resolver" | ||
) | ||
|
||
func generateTarget(scheme string, target string) resolver.Target { | ||
return resolver.Target{URL: *testutils.MustParseURL(fmt.Sprintf("%s:///%s", scheme, target))} | ||
} | ||
|
||
// This is here just in case another test calls the SetDefaultScheme method. | ||
func resetInitialResolverState() { | ||
resolver.SetDefaultScheme("passthrough") | ||
internal.UserSetDefaultScheme = false | ||
} | ||
|
||
func (s) TestParsedTarget_Success_WithoutCustomDialer(t *testing.T) { | ||
defScheme := resolver.GetDefaultScheme() | ||
resetInitialResolverState() | ||
dialScheme := resolver.GetDefaultScheme() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this should probably try to re-create the initial state in case another test calls |
||
newClientScheme := "dns" | ||
tests := []struct { | ||
target string | ||
wantParsed resolver.Target | ||
target string | ||
wantDialParse resolver.Target | ||
wantNewClientParse resolver.Target | ||
}{ | ||
// No scheme is specified. | ||
{target: "://a/b", wantParsed: resolver.Target{URL: *testutils.MustParseURL(fmt.Sprintf("%s:///%s", defScheme, "://a/b"))}}, | ||
{target: "a//b", wantParsed: resolver.Target{URL: *testutils.MustParseURL(fmt.Sprintf("%s:///%s", defScheme, "a//b"))}}, | ||
{ | ||
target: "://a/b", | ||
wantDialParse: generateTarget(dialScheme, "://a/b"), | ||
wantNewClientParse: generateTarget(newClientScheme, "://a/b"), | ||
}, | ||
{ | ||
target: "a//b", | ||
wantDialParse: generateTarget(dialScheme, "a//b"), | ||
wantNewClientParse: generateTarget(newClientScheme, "a//b"), | ||
}, | ||
|
||
// An unregistered scheme is specified. | ||
{target: "a:///", wantParsed: resolver.Target{URL: *testutils.MustParseURL(fmt.Sprintf("%s:///%s", defScheme, "a:///"))}}, | ||
{target: "a:b", wantParsed: resolver.Target{URL: *testutils.MustParseURL(fmt.Sprintf("%s:///%s", defScheme, "a:b"))}}, | ||
{ | ||
target: "a:///", | ||
wantDialParse: generateTarget(dialScheme, "a:///"), | ||
wantNewClientParse: generateTarget(newClientScheme, "a:///"), | ||
}, | ||
{ | ||
target: "a:b", | ||
wantDialParse: generateTarget(dialScheme, "a:b"), | ||
wantNewClientParse: generateTarget(newClientScheme, "a:b"), | ||
}, | ||
|
||
// A registered scheme is specified. | ||
{target: "dns://a.server.com/google.com", wantParsed: resolver.Target{URL: *testutils.MustParseURL("dns://a.server.com/google.com")}}, | ||
{target: "unix-abstract:/ a///://::!@#$%25^&*()b", wantParsed: resolver.Target{URL: *testutils.MustParseURL("unix-abstract:/ a///://::!@#$%25^&*()b")}}, | ||
{target: "unix-abstract:passthrough:abc", wantParsed: resolver.Target{URL: *testutils.MustParseURL("unix-abstract:passthrough:abc")}}, | ||
{target: "passthrough:///unix:///a/b/c", wantParsed: resolver.Target{URL: *testutils.MustParseURL("passthrough:///unix:///a/b/c")}}, | ||
{ | ||
target: "dns://a.server.com/google.com", | ||
wantDialParse: resolver.Target{URL: *testutils.MustParseURL("dns://a.server.com/google.com")}, | ||
wantNewClientParse: resolver.Target{URL: *testutils.MustParseURL("dns://a.server.com/google.com")}, | ||
}, | ||
{ | ||
target: "unix-abstract:/ a///://::!@#$%25^&*()b", | ||
wantDialParse: resolver.Target{URL: *testutils.MustParseURL("unix-abstract:/ a///://::!@#$%25^&*()b")}, | ||
wantNewClientParse: resolver.Target{URL: *testutils.MustParseURL("unix-abstract:/ a///://::!@#$%25^&*()b")}, | ||
}, | ||
{ | ||
target: "unix-abstract:passthrough:abc", | ||
wantDialParse: resolver.Target{URL: *testutils.MustParseURL("unix-abstract:passthrough:abc")}, | ||
wantNewClientParse: resolver.Target{URL: *testutils.MustParseURL("unix-abstract:passthrough:abc")}, | ||
}, | ||
{ | ||
target: "passthrough:///unix:///a/b/c", | ||
wantDialParse: resolver.Target{URL: *testutils.MustParseURL("passthrough:///unix:///a/b/c")}, | ||
wantNewClientParse: resolver.Target{URL: *testutils.MustParseURL("passthrough:///unix:///a/b/c")}, | ||
}, | ||
|
||
// Cases for `scheme:absolute-path`. | ||
{target: "dns:/a/b/c", wantParsed: resolver.Target{URL: *testutils.MustParseURL("dns:/a/b/c")}}, | ||
{target: "unregistered:/a/b/c", wantParsed: resolver.Target{URL: *testutils.MustParseURL(fmt.Sprintf("%s:///%s", defScheme, "unregistered:/a/b/c"))}}, | ||
{ | ||
target: "dns:/a/b/c", | ||
wantDialParse: resolver.Target{URL: *testutils.MustParseURL("dns:/a/b/c")}, | ||
wantNewClientParse: resolver.Target{URL: *testutils.MustParseURL("dns:/a/b/c")}, | ||
}, | ||
{ | ||
target: "unregistered:/a/b/c", | ||
wantDialParse: generateTarget(dialScheme, "unregistered:/a/b/c"), | ||
wantNewClientParse: generateTarget(newClientScheme, "unregistered:/a/b/c"), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
|
@@ -66,8 +119,18 @@ func (s) TestParsedTarget_Success_WithoutCustomDialer(t *testing.T) { | |
} | ||
defer cc.Close() | ||
|
||
if !cmp.Equal(cc.parsedTarget, test.wantParsed) { | ||
t.Errorf("cc.parsedTarget for dial target %q = %+v, want %+v", test.target, cc.parsedTarget, test.wantParsed) | ||
if !cmp.Equal(cc.parsedTarget, test.wantDialParse) { | ||
t.Errorf("cc.parsedTarget for dial target %q = %+v, want %+v", test.target, cc.parsedTarget, test.wantDialParse) | ||
} | ||
|
||
cc, err = NewClient(test.target, WithTransportCredentials(insecure.NewCredentials())) | ||
if err != nil { | ||
t.Fatalf("NewClient(%q) failed: %v", test.target, err) | ||
} | ||
defer cc.Close() | ||
|
||
if !cmp.Equal(cc.parsedTarget, test.wantNewClientParse) { | ||
t.Errorf("cc.parsedTarget for newClient target %q = %+v, want %+v", test.target, cc.parsedTarget, test.wantNewClientParse) | ||
} | ||
}) | ||
} | ||
|
@@ -93,6 +156,7 @@ func (s) TestParsedTarget_Failure_WithoutCustomDialer(t *testing.T) { | |
} | ||
|
||
func (s) TestParsedTarget_WithCustomDialer(t *testing.T) { | ||
resetInitialResolverState() | ||
defScheme := resolver.GetDefaultScheme() | ||
tests := []struct { | ||
target string | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just FYI / for the next reviewer's reference:
After this PR, I plan to rewrite things a little bit. I'd like this to become the primary API for users, and to call
Dial
andDialContext
both "deprecated" in preference of this. As such, this will contain a bit more documentation, and the others will state they call it and then initiate and wait for a connection.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.
@dfawley ok now with the newest release both are deprecated. But how do I add a timeout?
When I try the follwing:
the linter tells me that
grpc.WithTimeout
is deprecated I should usegrpc.DialContext
(which is deprecated too). So how do I usegrpc.NewClient
withgrpc.WithBlock
that doesn't block forever?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.
Setting a timeout when creating a client is an anti-pattern, and is not possible in any other gRPC implementations.
See https://github.com/grpc/grpc-go/blob/master/Documentation/anti-patterns.md#dialing-in-grpc for more info.
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 agree that connection failures should always be checked, not only on start-up.
But if you're working with setups such as Kubernetes, you don't want your new pod to pass liveness/readiness checks in case it has an invalid connection string. It's way safer to have this "ping" check to confirm there are no misconfigurations in your new deployment, before you terminate your previous pods.
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.
There are other, better, more robust ways of doing that. For example, health checking: https://github.com/grpc/proposal/blob/master/A17-client-side-health-checking.md
You can also use the connectivity state API if you just want to see if the client was able to connect: https://pkg.go.dev/google.golang.org/grpc#ClientConn.GetState and https://pkg.go.dev/google.golang.org/grpc#ClientConn.WaitForStateChange