Skip to content

How can I share some value without sharing Context.Key object instance? #7024

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

Closed
hojongs opened this issue May 8, 2020 · 3 comments
Closed
Labels

Comments

@hojongs
Copy link
Contributor

hojongs commented May 8, 2020

Summary

How can I store and get a value from Context without sharing Context.Key instance?

Detail

I want to share some value with grpc Context. but I can't get the stored value with Context.Key instance

I tried this snippet

Context.key("A") == Context.key("A")

The result was

false

But I expected

true

It means if I want to get value from a Context, I should use same Context.Key instance.
It is strange. Because, I'm trying to share some values with Context and when I get the stored value I should use same Context.Key.
To share some value, should share the key object instance. (not the key value)

For example (with kotlin)

// Function A
val myKey = Context.key("myKey")

Context.current()
    .withValue(key, "myVal")
    .attach()
// Function B, After call A
Context.key<String>("myKey").get() // == null // I think, It should be "myVal"

myKey.get() // == "myVal" 

If I can share myKey object instance, I don't need Context to share value.
What about multi-threaded case? It is more difficult to share the Context.Key object instance.

I can't understand use of Context and Context.key to share value.

In short, My question is.

How can I store and get a value from Context without sharing Context.Key instance?

@ericgribkoff
Copy link
Contributor

You're correct that multiple Context.Key can share the same debug name but still won't return true when tested for equality. From the doc at https://grpc.github.io/grpc-java/javadoc/io/grpc/Context.html#key-java.lang.String-:

Create a Context.Key with the given debug name. Multiple different keys may have the same name; the name is intended for debugging purposes and does not impact behavior.

You need to use the same Context.Key reference when inserting and retrieving values from the context. A simple way to achieve this is to create your key as essentially a global constant and use it when setting and retrieving values. I didn't look through it carefully but https://github.com/saturnism/grpc-by-example-java/tree/master/metadata-context-example shows example code that uses Context.Key to propagate values.

@hojongs
Copy link
Contributor Author

hojongs commented May 9, 2020

Thank you for your answer. I was missing the doc of Context.key API.
I understood of the way to use it for my purpose.
But I think it is confuse still.
Generally, key's name will be recognized it is identifier by people (In my opinion)
the name parameter of Context.key(name) is debugging name, so Let me give a suggestion.

  • Make Context.key(name) function have default parameter value
    • or overload key function without any parameter.
    • It represents the name parameter is optional.
  public static <T> Key<T> key() {
    return new Key<>(DEFAULT_KEY_NAME);
  }

@ericgribkoff
Copy link
Contributor

Since the question here is resolved, I'm going to mark this issue as closed (discussion on the updates can continue on the PR). Thanks!

ejona86 pushed a commit that referenced this issue May 13, 2020
dfawley pushed a commit to dfawley/grpc-java that referenced this issue Jan 15, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants