-
Notifications
You must be signed in to change notification settings - Fork 46
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(internal/gengapic): add workaround for operation collision #1397
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
// internalFooClient is an interface that defines the methods available from Awesome Foo API. | ||
type internalFooClient interface { | ||
Close() error | ||
setGoogleClientInfo(...string) | ||
Connection() *grpc.ClientConn | ||
Zip(context.Context, *mypackagepb.Bar, ...gax.CallOption) (*ZipRegistryOperation, error) | ||
ZipRegistryOperation(name string) *ZipRegistryOperation | ||
ListOperations(context.Context, *longrunningpb.ListOperationsRequest, ...gax.CallOption) *OperationIterator | ||
GetOperation(context.Context, *longrunningpb.GetOperationRequest, ...gax.CallOption) (*longrunningpb.Operation, error) | ||
DeleteOperation(context.Context, *longrunningpb.DeleteOperationRequest, ...gax.CallOption) error | ||
CancelOperation(context.Context, *longrunningpb.CancelOperationRequest, ...gax.CallOption) error | ||
WaitOperation(context.Context, *longrunningpb.WaitOperationRequest, ...gax.CallOption) (*longrunningpb.Operation, error) | ||
} | ||
|
||
// FooClient is a client for interacting with Awesome Foo API. | ||
// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. | ||
// | ||
// Foo service does stuff. | ||
type FooClient struct { | ||
// The internal transport-dependent client. | ||
internalClient internalFooClient | ||
|
||
// The call options for this service. | ||
CallOptions *FooCallOptions | ||
|
||
// LROClient is used internally to handle long-running operations. | ||
// It is exposed so that its CallOptions can be modified if required. | ||
// Users should not Close this client. | ||
LROClient *lroauto.OperationsClient | ||
|
||
} | ||
|
||
// Wrapper methods routed to the internal client. | ||
|
||
// Close closes the connection to the API service. The user should invoke this when | ||
// the client is no longer required. | ||
func (c *FooClient) Close() error { | ||
return c.internalClient.Close() | ||
} | ||
|
||
// setGoogleClientInfo sets the name and version of the application in | ||
// the `x-goog-api-client` header passed on each request. Intended for | ||
// use by Google-written clients. | ||
func (c *FooClient) setGoogleClientInfo(keyval ...string) { | ||
c.internalClient.setGoogleClientInfo(keyval...) | ||
} | ||
|
||
// Connection returns a connection to the API service. | ||
// | ||
// Deprecated: Connections are now pooled so this method does not always | ||
// return the same resource. | ||
func (c *FooClient) Connection() *grpc.ClientConn { | ||
return c.internalClient.Connection() | ||
} | ||
|
||
// Zip does some stuff. | ||
func (c *FooClient) Zip(ctx context.Context, req *mypackagepb.Bar, opts ...gax.CallOption) (*ZipRegistryOperation, error) { | ||
return c.internalClient.Zip(ctx, req, opts...) | ||
} | ||
|
||
// ZipRegistryOperation returns a new ZipRegistryOperation from a given name. | ||
// The name must be that of a previously created ZipRegistryOperation, possibly from a different process. | ||
func (c *FooClient) ZipRegistryOperation(name string) *ZipRegistryOperation { | ||
return c.internalClient.ZipRegistryOperation(name) | ||
} | ||
|
||
func (c *FooClient) ListOperations(ctx context.Context, req *longrunningpb.ListOperationsRequest, opts ...gax.CallOption) *OperationIterator { | ||
return c.internalClient.ListOperations(ctx, req, opts...) | ||
} | ||
|
||
func (c *FooClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) { | ||
return c.internalClient.GetOperation(ctx, req, opts...) | ||
} | ||
|
||
func (c *FooClient) DeleteOperation(ctx context.Context, req *longrunningpb.DeleteOperationRequest, opts ...gax.CallOption) error { | ||
return c.internalClient.DeleteOperation(ctx, req, opts...) | ||
} | ||
|
||
func (c *FooClient) CancelOperation(ctx context.Context, req *longrunningpb.CancelOperationRequest, opts ...gax.CallOption) error { | ||
return c.internalClient.CancelOperation(ctx, req, opts...) | ||
} | ||
|
||
func (c *FooClient) WaitOperation(ctx context.Context, req *longrunningpb.WaitOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) { | ||
return c.internalClient.WaitOperation(ctx, req, opts...) | ||
} | ||
|
||
// fooGRPCClient is a client for interacting with Awesome Foo API over gRPC transport. | ||
// | ||
// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. | ||
type fooGRPCClient struct { | ||
// Connection pool of gRPC connections to the service. | ||
connPool gtransport.ConnPool | ||
|
||
// Points back to the CallOptions field of the containing FooClient | ||
CallOptions **FooCallOptions | ||
|
||
// The gRPC API client. | ||
fooClient mypackagepb.FooClient | ||
|
||
// LROClient is used internally to handle long-running operations. | ||
// It is exposed so that its CallOptions can be modified if required. | ||
// Users should not Close this client. | ||
LROClient **lroauto.OperationsClient | ||
|
||
operationsClient longrunningpb.OperationsClient | ||
|
||
// The x-goog-* metadata to be sent with each request. | ||
xGoogHeaders []string | ||
} | ||
|
||
// NewFooClient creates a new foo client based on gRPC. | ||
// The returned client must be Closed when it is done being used to clean up its underlying connections. | ||
// | ||
// Foo service does stuff. | ||
func NewFooClient(ctx context.Context, opts ...option.ClientOption) (*FooClient, error) { | ||
clientOpts := defaultFooGRPCClientOptions() | ||
if newFooClientHook != nil { | ||
hookOpts, err := newFooClientHook(ctx, clientHookParams{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
clientOpts = append(clientOpts, hookOpts...) | ||
} | ||
|
||
connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
client := FooClient{CallOptions: defaultFooCallOptions()} | ||
|
||
c := &fooGRPCClient{ | ||
connPool: connPool, | ||
fooClient: mypackagepb.NewFooClient(connPool), | ||
CallOptions: &client.CallOptions, | ||
operationsClient: longrunningpb.NewOperationsClient(connPool), | ||
|
||
} | ||
c.setGoogleClientInfo() | ||
|
||
client.internalClient = c | ||
|
||
client.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool)) | ||
if err != nil { | ||
// This error "should not happen", since we are just reusing old connection pool | ||
// and never actually need to dial. | ||
// If this does happen, we could leak connp. However, we cannot close conn: | ||
// If the user invoked the constructor with option.WithGRPCConn, | ||
// we would close a connection that's still in use. | ||
// TODO: investigate error conditions. | ||
return nil, err | ||
} | ||
c.LROClient = &client.LROClient | ||
return &client, nil | ||
} | ||
|
||
// Connection returns a connection to the API service. | ||
// | ||
// Deprecated: Connections are now pooled so this method does not always | ||
// return the same resource. | ||
func (c *fooGRPCClient) Connection() *grpc.ClientConn { | ||
return c.connPool.Conn() | ||
} | ||
|
||
// setGoogleClientInfo sets the name and version of the application in | ||
// the `x-goog-api-client` header passed on each request. Intended for | ||
// use by Google-written clients. | ||
func (c *fooGRPCClient) setGoogleClientInfo(keyval ...string) { | ||
kv := append([]string{"gl-go", gax.GoVersion}, keyval...) | ||
kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version) | ||
c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)} | ||
} | ||
|
||
// Close closes the connection to the API service. The user should invoke this when | ||
// the client is no longer required. | ||
func (c *fooGRPCClient) Close() error { | ||
return c.connPool.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.
It seems at least a tiny bit possible that someday another RPC will match this pattern...
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.
It may, but I believe it is unlikely. Ideally this code gets deleted in the next couple of months. If we end up having to do this for the v1 service we can make the logic tighter at that time. I was trying to avoid passing a lot more info throughout the stack if possible so this is easy to unwind.