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

Propagate WebClient attributes into underlying HTTP client request where possible #29958

Closed

Conversation

PhilKes
Copy link
Contributor

@PhilKes PhilKes commented Feb 11, 2023

This PR Closes #26208
As proposed by @tstavinoha (#26208 (comment)) and @rstoyanchev (#26208 (comment)), this adds the option applyAttributes to the WebClient.Builder. If it is set to true, the ClientRequest.attributes() will be copied over to the underlying http-client library HttpRequest. For this there are different implementations for the various ClientHttpConnector implementations:

  • JettyClientHttpRequest: Attributes are set with the org.eclipse.jetty.client.api.Request#attribute(String, Object) method one by one
  • ReactorClientHttpRequest: A single Map<String, Object> containing all request attributes is set to the Attribute attributes in the reactor.netty.channel.ChannelOperations#channel() (as proposed by Passing attributes from WebClient to underlying HTTP library #26208 (comment))
  • ReactorNetty2ClientHttpRequest: Same as in ReactorClientHttpRequest, but with the netty5 classes
  • HttpComponentsClientHttpRequest: Attributes are set with the org.apache.hc.client5.http.protocol.HttpClientContext#setAttribute(String, Object) method one by one
  • JdkClientHttpRequest: I still need some input on this one. As far as I can see there is no possibility to set request attributes to the java.net.http.HttpRequest?

As suggested by #26208 (comment), applyAttributes defaults to true and the user can opt-out when using the Builder to create the Connector.

Allows applying the attributes of the Http request to the underlying http-client request

closes spring-projectsgh-26208
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 11, 2023
@Override
protected void applyAttributes() {
// TODO
throw new RuntimeException(String.format("Using attributes is not available for %s", HttpRequest.class.getName()));
Copy link
Contributor Author

@PhilKes PhilKes Feb 11, 2023

Choose a reason for hiding this comment

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

Can somebody please verify if there is an option to set request attributes for the java.net.http.HttpRequest?
If there is not, what should be the proper behaviour, should an Exception be thrown to the user if applyAttributes is set to true? Or should it simply always default to false when using the JdkClientHttpConnector?

Copy link
Contributor

Choose a reason for hiding this comment

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

If the boolean flag is removed as suggested above, this should become a no-op.

assertThat(nativeReq.getAttributes()).doesNotContainEntry("foo", "bar");
}
} else if (nativeRequest.get() instanceof org.apache.hc.core5.http.HttpRequest nativeReq) {
// TODO get attributes from HttpClientContext
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could not find out how to verify the request attributes were added to the org.apache.hc.client5.http.protocol.HttpClientContext in the Test here, can somebody help out with this?

@rstoyanchev rstoyanchev added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Feb 14, 2023
@rstoyanchev rstoyanchev self-assigned this Feb 14, 2023
@PhilKes PhilKes marked this pull request as ready for review February 15, 2023 19:00
@PhilKes
Copy link
Contributor Author

PhilKes commented Mar 4, 2023

@rstoyanchev would love some feedback if you have the time.

Copy link
Contributor

@rstoyanchev rstoyanchev left a comment

Choose a reason for hiding this comment

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

Thanks for the pull request!

On second thought I don't think we need to make this opt-in and can drop the config option from WebClient.Builder and from ClientHttpConnectors. It does make sense to make attributes go further where we can, and treat them as any other part of the request. For Netty we are also adding the full map as a single attribute, so not likely to be a performance issue.

@@ -45,6 +48,8 @@
*/
class ReactorClientHttpRequest extends AbstractClientHttpRequest implements ZeroCopyHttpOutputMessage {

public static final String ATTRIBUTES_CHANNEL_KEY = "attributes";
Copy link
Contributor

Choose a reason for hiding this comment

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

The attribute name should be a little more qualified. Typically we would use something like:

ReactorClientHttpRequest.class.getName() + ".attributes"

@Override
protected void applyAttributes() {
// TODO
throw new RuntimeException(String.format("Using attributes is not available for %s", HttpRequest.class.getName()));
Copy link
Contributor

Choose a reason for hiding this comment

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

If the boolean flag is removed as suggested above, this should become a no-op.

@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 6, 2024
@rstoyanchev rstoyanchev added this to the 6.2.0-M1 milestone Feb 6, 2024
@rstoyanchev rstoyanchev changed the title Add applyAttributes option to WebClient.Builder to allow copying request attributes to underlying http-client Propagate WebClient attributes into underlying HTTP client request where possible Feb 6, 2024
rstoyanchev pushed a commit that referenced this pull request Mar 12, 2024
rstoyanchev added a commit that referenced this pull request Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Passing attributes from WebClient to underlying HTTP library
3 participants