googleapis / gax-java Public
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
feat: add custom options to ApiCallContext #1435
Conversation
Hi @vam-google , creating this pr so we don't need to use |
gax/src/main/java/com/google/api/gax/rpc/internal/ApiCallContextUtil.java
Outdated
Show resolved
Hide resolved
gax-grpc/src/main/java/com/google/api/gax/grpc/GrpcCallContext.java
Outdated
Show resolved
Hide resolved
9939551
to
eae5b9e
Compare
gax/src/main/java/com/google/api/gax/rpc/internal/ApiCallCustomContexts.java
Outdated
Show resolved
Hide resolved
gax/src/main/java/com/google/api/gax/rpc/internal/ApiCallCustomContexts.java
Outdated
Show resolved
Hide resolved
gax/src/main/java/com/google/api/gax/rpc/internal/ApiCallCustomContexts.java
Outdated
Show resolved
Hide resolved
gax/src/main/java/com/google/api/gax/rpc/internal/ApiCallCustomContexts.java
Outdated
Show resolved
Hide resolved
gax/src/main/java/com/google/api/gax/rpc/internal/ApiCallCustomContexts.java
Outdated
Show resolved
Hide resolved
LGTM, if we really need the Key<T>
abstraction here (please clarify why we can't just use String or Object there).
@@ -491,6 +508,29 @@ public GrpcCallContext withTracer(@Nonnull ApiTracer tracer) { | |||
return withCallOptions(callOptions.withOption(TRACER_KEY, tracer)); | |||
} | |||
|
|||
/** {@inheritDoc} */ | |||
@Override | |||
public <T> GrpcCallContext withOption(Key<T> key, T value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BetaApi
or is it not worth it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BetaApi
is added to ApiCallContext
, do we still need it here? This class is also annotated with @BetaApi
.
<T> T getOption(Key<T> key); | ||
|
||
/** Key for api call context options key-value pair. */ | ||
final class Key<T> { |
There was a problem hiding this comment.
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? What is the use of type parameter T here? It does not seem it is being used in class itself (the name field is String explicitly). Can we just have "String" directly as key?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type is to make sure when withOption(Key<T> key, T value)
is called, the correct value type is passed in to avoid casting problems.
if (!options.containsKey(key)) { | ||
builder.putAll(options).put(key, value); | ||
} else { | ||
for (Key oldKey : options.keySet()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really feels like there must be a more elegant way of doing it, but Ok =)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the code a bit, does it look better now?
🤖 I have created a release \*beep\* \*boop\* --- ## [2.3.0](https://www.github.com/googleapis/gax-java/compare/v2.2.0...v2.3.0) (2021-08-16) ### Features * add custom options to ApiCallContext ([#1435](https://www.github.com/googleapis/gax-java/issues/1435)) ([0fe20f3](https://www.github.com/googleapis/gax-java/commit/0fe20f379feba1570e562e60e3f0bf7cc4e485bd)) * add UseJwtAccessWithScope to GoogleCredentialsProvider ([#1420](https://www.github.com/googleapis/gax-java/issues/1420)) ([ed39c34](https://www.github.com/googleapis/gax-java/commit/ed39c34693783460fc03effb47e7027914cfb5bc)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
Adding a
withOption
method toApiCallContext
so additional contexts can be passed in the callable chains. The idea is similar to https://github.com/grpc/grpc-java/blob/master/api/src/main/java/io/grpc/CallOptions.java#L318. The custom options are encapsulated inApiCallCustomOptions
.An example usage is to pass batch throttled time to a tracer #1413 (comment).