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

Add Clients.newContextCaptor() #2344

Merged
merged 17 commits into from
Dec 31, 2019
Merged

Conversation

trustin
Copy link
Member

@trustin trustin commented Dec 26, 2019

Motivation:

It'd be nice if a user can capture the ClientRequestContext of the
request being sent without using a decorator.

Modifications:

  • Add Clients.newContextCaptor()
  • Add ClientThreadLocalState which replaces the functional
    composition of Consumer<ClientRequestContext>s.
  • Copy the customizer list when a DefaultClientRequestContext is
    constructed, so that thread-local customizers are evaluated even if
    an actual request starts from a different thread.
  • Add ? super to some type parameters for user convenience.
  • Miscellaneous:
    • Fix a bug in the gRPC client where a wrong path (usually /) is set to the context.
    • Make ClientRequestContext.ownAttr() nullable.

Result:

Motivation:

It'd be nice if a user can capture the `ClientRequestContext` of the
request being sent without using a decorator.

Modifications:

- Add `Clients.captureNextContext()` and `capturedContext()`
- Add `ClientRequestContextCustomizers` which replaces the functional
  composition of `Consumer<ClientRequestContext>`s.
- Copy the customizer list when a `DefaultClientRequestContext` is
  constructed, so that thread-local customizers are evaluated even if
  an actual request starts from a different thread.
- Add `? super` to some type parameters for user convenience.

Result:

- Closes line#1739
- Fixed a bug where `Clients.withHttpHeaders()` and `withContextCustomizer()`
  don't work for gRPC clients.
@codecov
Copy link

codecov bot commented Dec 26, 2019

Codecov Report

❗ No coverage uploaded for pull request base (master@69744ef). Click here to learn what that means.
The diff coverage is 55.93%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #2344   +/-   ##
=========================================
  Coverage          ?   73.62%           
  Complexity        ?    10483           
=========================================
  Files             ?      928           
  Lines             ?    40101           
  Branches          ?     4944           
=========================================
  Hits              ?    29524           
  Misses            ?     8068           
  Partials          ?     2509
Impacted Files Coverage Δ Complexity Δ
...inecorp/armeria/client/endpoint/EndpointGroup.java 73.52% <ø> (ø) 15 <0> (?)
...corp/armeria/unsafe/grpc/GrpcUnsafeBufferUtil.java 78.57% <ø> (ø) 4 <0> (?)
...armeria/server/tomcat/Tomcat90ProtocolHandler.java 52% <ø> (ø) 11 <0> (?)
...com/linecorp/armeria/server/auth/OAuth1aToken.java 59.01% <ø> (ø) 19 <0> (?)
...a/com/linecorp/armeria/spring/ArmeriaSettings.java 75.38% <ø> (ø) 13 <0> (?)
...linecorp/armeria/server/ServiceRequestContext.java 85% <ø> (ø) 9 <0> (?)
.../connector/proxy/ArmeriaProxyConnectorFactory.java 100% <ø> (ø) 7 <0> (?)
...linecorp/armeria/server/docs/DocServiceFilter.java 77.77% <ø> (ø) 24 <0> (?)
.../com/linecorp/armeria/server/auth/OAuth2Token.java 53.84% <ø> (ø) 4 <0> (?)
...a/com/linecorp/armeria/server/auth/BasicToken.java 61.53% <ø> (ø) 10 <0> (?)
... and 33 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 69744ef...1de2333. Read the comment docs.

Comment on lines -169 to +170
uri().getRawPath(),
uri().getRawQuery(),
req.path(),
null,
Copy link
Member Author

Choose a reason for hiding this comment

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

This fixes a bug in the gRPC client where a wrong path (usually /) is set to the context. /cc @anuraaga

Copy link
Member

@minwoox minwoox left a comment

Choose a reason for hiding this comment

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

👍

@@ -91,6 +86,36 @@ void deriveContext() {
assertThat(derivedCtx.attr(bar).get()).isEqualTo(null);
}

@Test
void derivedContextMustNotCallCustomizers() {
Copy link
Member

Choose a reason for hiding this comment

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

Thanks!

Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

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

Awesome work! Updated two nit indentations in Javadoc 8eca4db because of your day off. 😉

Comment on lines -51 to -54
return execute(null, req);
}

private HttpResponse execute(@Nullable EventLoop eventLoop, HttpRequest req) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! 👍

@trustin
Copy link
Member Author

trustin commented Dec 30, 2019

@anuraaga @minwoox @ikhoon PTAL again.

@trustin trustin changed the title Add Clients.captureNextContext() and capturedContext() Add Clients.newContextCaptor() Dec 30, 2019
Copy link
Member

@minwoox minwoox left a comment

Choose a reason for hiding this comment

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

It looks better than before. 😉

Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

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

Still LGTM

Copy link
Collaborator

@anuraaga anuraaga left a comment

Choose a reason for hiding this comment

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

LGTM code-wise but still have concern about the thread local handling for captor.

Copy link
Collaborator

@anuraaga anuraaga left a comment

Choose a reason for hiding this comment

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

Thanks!

@trustin
Copy link
Member Author

trustin commented Dec 31, 2019

Added a few utility methods to ClientRequestContextCaptor - 1de2333

@trustin trustin merged commit 0e5cb96 into line:master Dec 31, 2019
@trustin trustin deleted the capture_client_context branch December 31, 2019 07:11
@trustin
Copy link
Member Author

trustin commented Dec 31, 2019

Thanks for reviewing!

fmguerreiro pushed a commit to fmguerreiro/armeria that referenced this pull request Sep 19, 2020
Motivation:

It'd be nice if a user can capture the `ClientRequestContext` of the
request being sent without using a decorator.

Modifications:

- Add `Clients.newContextCaptor()`
- Add `ClientThreadLocalState` which replaces the functional
  composition of `Consumer<ClientRequestContext>`s.
- Copy the customizer list when a `DefaultClientRequestContext` is
  constructed, so that thread-local customizers are evaluated even if
  an actual request starts from a different thread.
- Add `? super` to some type parameters for user convenience.
- Miscellaneous:
  - Fix a bug in the gRPC client where a wrong path (usually /) is set to the context.
  - Make `ClientRequestContext.ownAttr()` nullable.

Result:

- Closes line#1739
- Fixed a bug where thread-local context customizers were called for derived contexts unintentionally.
- Fixed a bug where `Clients.withHttpHeaders()` and `withContextCustomizer()`
  don't work for gRPC clients.
- Fixed a bug in the gRPC client where a wrong path (usually /) is set to the context.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide a way to get ClientRequestContext without using a decorator.
4 participants