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

Question when developing a grpc proxy: how to specify the Codec method for only one end? #4335

Closed
ancientmodern opened this issue Apr 13, 2021 · 1 comment
Assignees

Comments

@ancientmodern
Copy link

ancientmodern commented Apr 13, 2021

We have a service that needs to use grpc proxy, so I implemented a custom one based on this repo. It works well as I use grpc.NewServer(grpc.CustomCodec(proxy.Codec()), ...) to receive the requests from client and then send them to the server with grpc.DialContext(ctx, target, grpc.WithCodec(proxy.Codec()), grpc.WithInsecure()), where proxy.Codec() is a custom Codec that "basically treats a gRPC message frame as raw bytes, however, if the server handler, or the client caller are not proxy-internal functions it will fall back to trying to decode the message using a fallback codec."

Then I found that grpc.Codec, grpc.WithCodec(), grpc.CustomCodec() had been deprecated and it's recommended to use encoding.RegisterCodec() to register codecs. I replaced them, however the client then reported an error failed to unmarshal, message is *proxy.frame, want proto.Message. After reading the encoding.md, I realized the problem was that my client used the default "proto" Codec, so the proxy will automatically chose using the "proto" codec instead of proxy.Codec() that I've registered. To deal with this, I have to add grpc.WithDefaultCallOptions(grpc.CallContentSubtype("MyCodec")) as the client 's dial option, which deviated from intention of using a user-unaware proxy.

Is there any method to force using a certain encoding.Codec for only one end like previously we used grpc.CustomCodec() and grpc.WithCodec()?

@dfawley
Copy link
Member

dfawley commented May 7, 2021

Sorry for the late reply. For this kind of situation, it sounds like you should use the newly-merged (#4205) https://pkg.go.dev/google.golang.org/grpc@master#ForceServerCodec in the proxy's server and https://pkg.go.dev/google.golang.org/grpc@master#ForceCodec in the proxy's clients. This will allow you to use the new, non-deprecated encoding.Codecs in a way that avoids using the registered codecs. Let us know if you encounter any problems with that approach.

@dfawley dfawley closed this as completed May 7, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants