Grpc-go: Context not passed from client to server

Created on 23 Mar 2015  Â·  11Comments  Â·  Source: grpc/grpc-go

Hi,

I tried to insert a key-value in the context to client, but didn't get it from server. And I also tried to cancel a context from client, but server didn't get it.

Can you help provide any advice here? Is it gonna be implemented in the future?

Most helpful comment

Here is a commit w/ an example of using metadata for anyone interested.

Works nicely for things such as traceID:
https://github.com/harlow/go-micro-services/commit/221a67bca4309aa12b69cd57ca6c1e9522c09cfc

All 11 comments

Contexts are not for passing arbitrary bits of data from client to server. That's what the RPC message itself is for. There will probably be a mechanism for attaching bits of data to an RPC via a context (e.g. trace IDs), but it's going to be tightly controlled, and almost definitely not what you want to use.

Please use metadata API to transmit your key-value pair. The example is
for unary rpc:
https://github.com/grpc/grpc-go/blob/master/test/end2end_test.go#L355
for streaming rpc:
https://github.com/grpc/grpc-go/blob/master/test/end2end_test.go#L504

Cancellation is propagated to server only when it is needed.

On Sun, Mar 22, 2015 at 4:43 PM, David Symonds [email protected]
wrote:

Contexts are not for passing arbitrary bits of data from client to server.
That's what the RPC message itself is for. There will probably be a
mechanism for attaching bits of data to an RPC via a context (e.g. trace
IDs), but it's going to be tightly controlled, and almost definitely not
what you want to use.

—
Reply to this email directly or view it on GitHub
https://github.com/grpc/grpc-go/issues/130#issuecomment-84733244.

There will probably be a mechanism for attaching bits of data to an RPC via a context (e.g. trace IDs), but it's going to be tightly controlled, and almost definitely not what you want to use.

@dsymonds any idea how far down the line this will be? It would be really nice to have Trace IDs as part of the context. It feels a bit clunky having to wrap all the Protos with Args/Req/Reply messages just to pass the Tracer around.

Update

Example of using gRPC metadata context:
https://medium.com/@harlow/grpc-context-for-client-server-metadata-91cec8729424

On Sat, Apr 11, 2015 at 9:15 AM, Harlow Ward [email protected]
wrote:

There will probably be a mechanism for attaching bits of data to an RPC
via a context (e.g. trace IDs), but it's going to be tightly controlled,
and almost definitely not what you want to use.

@dsymonds https://github.com/dsymonds any idea how far down the line
this will be? It would be really nice to have Trace IDs as part of the
context. It feels a bit clunky having to wrap all the Protos with
Args/Req/Reply messages just to pass the Tracer around.

grpc-go does not allow users attach metadata to the context directly (grpc
will ignore that). Users are required to use metadata API to attach the
info (e.g., trace ID) to the context.

—
Reply to this email directly or view it on GitHub
https://github.com/grpc/grpc-go/issues/130#issuecomment-91872428.

Please use metadata API to transmit your key-value pair. The example is
for unary rpc:
https://github.com/grpc/grpc-go/blob/master/test/end2end_test.go#L355
for streaming rpc:
https://github.com/grpc/grpc-go/blob/master/test/end2end_test.go#L504

For anyone looking for that example in the future, the above links won't work because master has since changed and the line numbers don't match up. Here are the correct links:

For unary rpc:

https://github.com/grpc/grpc-go/blob/51496073b86104a21479f1b0c09337ab76f0f883/test/end2end_test.go#L355

For streaming rpc:

https://github.com/grpc/grpc-go/blob/51496073b86104a21479f1b0c09337ab76f0f883/test/end2end_test.go#L504

Tip: You can press y when looking at a file to get a permalink:

image

Is there a way to set the metadata from the generated protobuf client code?

Can you take a look at the examples in my very first reply on this issue on
how to set and receive metadata and see whether it answers your question?

Copied/pasted for your convenience:

_Please use metadata API to transmit your key-value pair. The example is_
*for unary rpc: *

_https://github.com/grpc/grpc-go/blob/master/test/end2end_test.go#L355
https://github.com/grpc/grpc-go/blob/master/test/end2end_test.go#L355_
_for streaming rpc:_

_https://github.com/grpc/grpc-go/blob/master/test/end2end_test.go#L504
https://github.com/grpc/grpc-go/blob/master/test/end2end_test.go#L504_

On Mon, Apr 27, 2015 at 10:12 AM, dramdass [email protected] wrote:

Is there a way to set the metadata from the generated protobuf client code?

—
Reply to this email directly or view it on GitHub
https://github.com/grpc/grpc-go/issues/130#issuecomment-96743878.

Here is a commit w/ an example of using metadata for anyone interested.

Works nicely for things such as traceID:
https://github.com/harlow/go-micro-services/commit/221a67bca4309aa12b69cd57ca6c1e9522c09cfc

Thanks, Harlow!

On Fri, May 1, 2015 at 5:20 PM, Harlow Ward [email protected]
wrote:

Here is a commit for anyone interested in using the metadata. Works
nicely for things such as traceID:
harlow/go-micro-services@221a67b
https://github.com/harlow/go-micro-services/commit/221a67bca4309aa12b69cd57ca6c1e9522c09cfc

—
Reply to this email directly or view it on GitHub
https://github.com/grpc/grpc-go/issues/130#issuecomment-98266303.

@harlow you rock! Thank you.

@harlow just what I need! Tnank you!

Was this page helpful?
0 / 5 - 0 ratings