-
Notifications
You must be signed in to change notification settings - Fork 35
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
remove resolver.UnregisterForTesting usage #417
Open
bananacocodrilo
wants to merge
3
commits into
yarpc:dev
Choose a base branch
from
bananacocodrilo:dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,8 @@ package protobuf | |
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/jhump/protoreflect/desc" | ||
|
@@ -19,6 +19,8 @@ import ( | |
"google.golang.org/grpc/resolver/manual" | ||
) | ||
|
||
var resolverMutex sync.Mutex | ||
|
||
// ReflectionArgs are args for constructing a DescriptorProvider that reaches out to a reflection server. | ||
type ReflectionArgs struct { | ||
Caller string | ||
|
@@ -32,22 +34,24 @@ type ReflectionArgs struct { | |
// NewDescriptorProviderReflection returns a DescriptorProvider that reaches | ||
// out to a reflection server to access file descriptors. | ||
func NewDescriptorProviderReflection(args ReflectionArgs) (DescriptorProvider, error) { | ||
r, deregisterScheme := GenerateAndRegisterManualResolver() | ||
defer deregisterScheme() | ||
peers := make([]resolver.Address, len(args.Peers)) | ||
for i, p := range args.Peers { | ||
if strings.Contains(p, "://") { | ||
return nil, fmt.Errorf("peer contains scheme %q", p) | ||
} | ||
peers[i] = resolver.Address{Addr: p, Type: resolver.Backend} | ||
} | ||
r.InitialState(resolver.State{Addresses: peers}) | ||
resolverMutex.Lock() | ||
r := GetOrGenerateAndRegisterManualResolver(args.Service, peers) | ||
|
||
conn, err := grpc.DialContext(context.Background(), | ||
r.Scheme()+":///", // minimal target to dial registered host:port pairs | ||
grpc.WithTimeout(args.Timeout), | ||
grpc.WithBlock(), | ||
grpc.WithInsecure()) | ||
|
||
resolverMutex.Unlock() | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("could not reach reflection server: %s", err) | ||
} | ||
|
@@ -127,9 +131,21 @@ func wrapReflectionError(err error) error { | |
return fmt.Errorf("error in protobuf reflection: %v", err) | ||
} | ||
|
||
func GenerateAndRegisterManualResolver() (*manual.Resolver, func()) { | ||
scheme := strconv.FormatInt(time.Now().UnixNano(), 36) | ||
func GetOrGenerateAndRegisterManualResolver(service string, peers []resolver.Address) *manual.Resolver { | ||
scheme := "dest-" + service | ||
newState := resolver.State{Addresses: peers} | ||
|
||
rb := resolver.Get(scheme) | ||
if rb != nil { | ||
if r, ok := rb.(*manual.Resolver); ok { | ||
r.InitialState(newState) | ||
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. InitialState doesn't use mutex inside, so it's a potential data race. |
||
return r | ||
} | ||
} | ||
|
||
r := manual.NewBuilderWithScheme(scheme) | ||
resolver.Register(r) | ||
return r, func() { resolver.UnregisterForTesting(scheme) } | ||
r.InitialState(newState) | ||
|
||
return r | ||
} |
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 |
---|---|---|
|
@@ -196,6 +196,44 @@ func TestReflectionRoutingHeaders(t *testing.T) { | |
} | ||
} | ||
|
||
func TestResolverAlreadyExists(t *testing.T) { | ||
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. Test creating a new resolver for the same service with a different list of peers |
||
ln, err := net.Listen("tcp", "localhost:0") | ||
ln2, err := net.Listen("tcp", "127.0.0.1:0") | ||
require.NoError(t, err) | ||
require.NoError(t, err) | ||
|
||
defer ln.Close() | ||
defer ln2.Close() | ||
|
||
s := grpc.NewServer() | ||
reflection.Register(s) | ||
|
||
go s.Serve(ln) | ||
go s.Serve(ln2) | ||
|
||
// Ensure that all streams are closed by the end of the test. | ||
defer s.GracefulStop() | ||
|
||
provider, err := NewDescriptorProviderReflection(ReflectionArgs{ | ||
Timeout: time.Second, | ||
Peers: []string{ln.Addr().String()}, | ||
Service: "test", | ||
}) | ||
require.NoError(t, err, "failed to create reflection provider") | ||
_, err = provider.FindService("grpc.reflection.v1alpha.ServerReflection") | ||
assert.NoError(t, err, "unexpected error") | ||
|
||
provider, err = NewDescriptorProviderReflection(ReflectionArgs{ | ||
Timeout: time.Second, | ||
Peers: []string{ln2.Addr().String()}, | ||
Service: "test", | ||
}) | ||
require.NoError(t, err, "failed to create reflection provider") | ||
_, err = provider.FindService("grpc.reflection.v1alpha.ServerReflection") | ||
assert.NoError(t, err, "unexpected error") | ||
|
||
} | ||
|
||
func TestE2eErrors(t *testing.T) { | ||
ln, err := net.Listen("tcp", "127.0.0.1:0") | ||
require.NoError(t, err) | ||
|
@@ -212,6 +250,7 @@ func TestE2eErrors(t *testing.T) { | |
source, err := NewDescriptorProviderReflection(ReflectionArgs{ | ||
Timeout: time.Second, | ||
Peers: []string{ln.Addr().String()}, | ||
Service: "TestE2eErrors", | ||
}) | ||
require.NoError(t, err) | ||
defer source.Close() | ||
|
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.
Manual resolvers implement both, Resolver and Builder.
r.InitialState(newState) will set the new list of peers the next time that Build is called