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

examples: add new example to show updating metadata in interceptors #5788

Merged
merged 16 commits into from Dec 6, 2022
Merged

examples: add new example to show updating metadata in interceptors #5788

merged 16 commits into from Dec 6, 2022

Conversation

richzw
Copy link
Contributor

@richzw richzw commented Nov 11, 2022

add updating metadata in the unary/stream interceptor to examples

RELEASE NOTES:

  • examples: add new example to show updating metadata in interceptors

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Nov 11, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

@richzw richzw changed the title examples: add updating metadata in the interceptor examples: add updating metadata in the unary/stream interceptor Nov 11, 2022
@richzw
Copy link
Contributor Author

richzw commented Nov 14, 2022

@dfawley Could you please review my PR at your available time?

@arvindbr8 arvindbr8 self-assigned this Nov 15, 2022
@arvindbr8 arvindbr8 added this to the 1.52 Release milestone Nov 16, 2022
@arvindbr8 arvindbr8 added the Type: Documentation Documentation or examples label Nov 16, 2022
Copy link
Member

@arvindbr8 arvindbr8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this.
The existing example seems to be long already. Do you mind moving the interceptor part to another shorter example of its own? That way it becomes more readable?

@arvindbr8 arvindbr8 assigned richzw and unassigned arvindbr8 Nov 16, 2022
@richzw
Copy link
Contributor Author

richzw commented Nov 17, 2022

Thank you for working on this. The existing example seems to be long already. Do you mind moving the interceptor part to another shorter example of its own? That way it becomes more readable?

@arvindbr8 Thank you for your review, I have moved the updating metadata in the interceptor into another folder named metadata_interceptor. Could you please review it again?

Copy link
Contributor

@easwars easwars left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't looked at the server code yet. Will have more comments soon. Thanks.

"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

pb "google.golang.org/grpc/examples/features/proto/echo"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this change?

Although we don't do it at all places, we try (as much as possible) to group the proto imports separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the typo, it was caused by the Goland. I will fix it.


## Updating metadata in the interceptor - server side

Server side metadata updating in the interceptor examples are available [here](../examples/features/metadata_interceptor/server/main.go).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wrap these lines at 80 cols width. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I will fix it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please update this line to look like:

An example for updating metadata from a server interceptor is 
available [here](../examples/features/metadata_interceptor/server/main.go).

Also, please ensure that this line is also wrapped to 80-cols width. Thanks.


```go
func SomeInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
// get the metadata from context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe replace with:

// Get the incoming metadata from the RPC context, and add a new 
// key-value pair to it.
md, ok := metadata.FromIncomingContext(ctx)
md.Append("key1", "value1")

// Create a context with the new metadata and pass it to handler.
ctx = metadata.NewIncomingContext(ctx, md)
return handler(ctx, req)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, sorry for my bad statement in the comment

Comment on lines 262 to 269
// get the metadata from context
md, ok := metadata.FromIncomingContext(ss.Context())
// update metadata
md.Append("key1", "value1")
// set the metadata to context
ctx := metadata.NewIncomingContext(ss.Context(), md)

return handler(srv, &wrappedStream{ss, ctx})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comments here as the one above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I will fix it

defer cancel()
resp, err := client.UnaryEcho(ctx, &pb.EchoRequest{Message: message})
if err != nil {
log.Fatalf("client.UnaryEcho(_) = _, %v: ", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we do this in many (or all) examples, but I think we can print a simpler/nicer error message. Something like:

log.Fatalf("UnaryEcho: %v", err)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I will fix it

break
}
if err != nil {
log.Fatalf("failed to receive response due to error: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log.Fatalf("Receiving echo response: %v", err)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I will fix it


func main() {
flag.Parse()
// Set up a connection to the server.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove this comment as it is saying the obvious.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I will fix it

// Set up a connection to the server.
conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("did not connect: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log.Fatalf("grpc.Dial(%q): %v", *addr, err)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I will fix it

ec := pb.NewEchoClient(conn)

callUnaryEcho(ec, message)
time.Sleep(1 * time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this sleep?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove this sleep.


var addr = flag.String("addr", "localhost:50051", "the address to connect to")

const message = "hello world"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Get rid of this const and move it inline to the calls to callUnaryEcho and callBidiStreamingEcho.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thank you for your view

@richzw
Copy link
Contributor Author

richzw commented Nov 19, 2022

Haven't looked at the server code yet. Will have more comments soon. Thanks.

@easwars Thank you very much for your detailed review. I have fixed the codes, comments and document. Could you review it again in your available time? I am looking forward for your server codes review.

Thank you again.

@richzw richzw requested review from easwars and arvindbr8 and removed request for arvindbr8 and easwars November 19, 2022 02:36
Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you!

@dfawley
Copy link
Member

dfawley commented Dec 1, 2022

Argh, whoops. Something reminded me when I was looking at the flaky test: we should add this to https://github.com/grpc/grpc-go/blob/master/examples/examples_test.sh#L51 so that it is covered in our regression testing.

Copy link
Member

@dfawley dfawley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ensure this is covered by examples_test (including adding expected server & client output).

@richzw
Copy link
Contributor Author

richzw commented Dec 2, 2022

@dfawley , thank you very much for your comment. The features/metadata_interceptor has been added to examples_test . Could you please review it again?

@dfawley
Copy link
Member

dfawley commented Dec 6, 2022

cc @temawi as FYI

@dfawley dfawley merged commit a205447 into grpc:master Dec 6, 2022
1 check passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Type: Documentation Documentation or examples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants