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

Support for gRPC Server Response Headers and Trailers, adding ClientAfter handler #479

Merged
merged 7 commits into from
Mar 5, 2017

Conversation

basvanbeek
Copy link
Member

@basvanbeek basvanbeek commented Mar 3, 2017

This is a (minor) breaking change to the existing gRPC transport but imho is a better and more Go kit idiomatic approach to handling server response metadata.

Stuff changed:

  • no more Server wide context as it purely uses the per request context as provided by gRPC
    -changed ServerResponseFunc to provide you with empty header and trailer metadata pointers so you can fill them with chained middlewares
  • new ClientResponseFunc to read the received header and trailer metadata
  • before and after functions in the client transport can now be added multiple times (fixes Allow multiple calls of http.ClientBefore() and http.ClientAfter() (for server as well) #477 too)

@basvanbeek basvanbeek changed the title [WIP]: grpc ClientAfter handler and multiple calls to Client Before/After funcs. grpc ClientAfter handler and multiple calls to Client Before/After funcs. Mar 4, 2017
@basvanbeek basvanbeek changed the title grpc ClientAfter handler and multiple calls to Client Before/After funcs. WIP: grpc ClientAfter handler and multiple calls to Client Before/After funcs. Mar 5, 2017
@basvanbeek basvanbeek changed the title WIP: grpc ClientAfter handler and multiple calls to Client Before/After funcs. grpc ClientAfter handler and multiple calls to Client Before/After funcs. Mar 5, 2017
@basvanbeek basvanbeek changed the title grpc ClientAfter handler and multiple calls to Client Before/After funcs. Support for gRPC Server Response Headers and Trailers, adding ClientAfter handler Mar 5, 2017
@@ -222,7 +222,7 @@ func main() {
return
}

srv := addsvc.MakeGRPCServer(ctx, endpoints, tracer, logger)
srv := addsvc.MakeGRPCServer(endpoints, tracer, logger)
Copy link
Member

Choose a reason for hiding this comment

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

+1

@peterbourgon
Copy link
Member

Yep, this all scans perfectly, LGTM. In the merge commit, would you mind adding a small sentence or two about the changes required for consumers?

@basvanbeek
Copy link
Member Author

will do

- Separated ClientRequestFunc and ServerRequestFunc to highlight that request metadata in a ServerRequestFunc is supposed to be immutable.
- Return context in ServerResponseFuncs like with the HTTP transport, to allow passing data between chained serverAfter middlewares and
finally to the encoding step so it can be used to alter the response payload prior to sending this response to the client.
@basvanbeek basvanbeek merged commit 322a9e0 into go-kit:master Mar 5, 2017
jamesgist pushed a commit to jamesgist/kit that referenced this pull request Nov 1, 2024
Improved gRPC transport with some minor breaking changes.

gRPC Server Transport
=====================
Existing customers of the gRPC Server transport will have to get rid of the service scoped context as this has become kind of an anti-pattern.

This changes the gRPC server construction signature from:
  NewServer(ctx, endpoint, decoder, encoder, ...serverOptions)

to:
  NewServer(endpoint, decoder, encoder, ...serverOptions)

Server customers using `ResponseFunc` will need to update their signatures from:
  ResponseFunc func(context.Context, *metadata.MD)

to:
  ServerResponseFunc func(ctx context.Context, header *metadata.MD, trailer *metadata.MD) context.Context

PLEASE NOTE! Next to this signature update it might be that certain logic needs to move to the ServerRequestFunc if the logic was reading metadata from the originating request. This was incorrect behavior anyway as this is what ServerRequestFuncs are intended to handle. Use context to pass details if needing to manipulate response data based on incoming request metadata.


gRPC Client Transport
=====================
Customers of the gRPC Client transport will benefit from the newly added ClientResponseFunc, which will allow them to pick up metadata from both Headers and Trailers returned by a gRPC Server.

Consumers referencing RequestFunc will need to rename to ClientRequestFunc due to RequestFunc having been split up for Client and Server side.

ServerBefore() and ServerAfter() now append the referenced functions so next to variadic function parameters you can also call ServerBefore() and ServerAfter() multiple times without overwriting previous invocations. This is in line with the same change that already occurred server side.

Context
=======
Similar to the HTTP Server and Client Transport, the gRPC Server and Client Transport now feature passing of context.Context for all Before and After Funcs. This can be helpful if needing to pass data along to chained middlewares or when response header logic might need to signal changes needed in the regular response payload which now can be done by passing details in context and picking them up in the response encoder step.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow multiple calls of http.ClientBefore() and http.ClientAfter() (for server as well)
2 participants