Skip to content

Immediately release http client resources - #9296

Merged
joschi merged 1 commit into
dropwizard:release/4.0.xfrom
zUniQueX:feature/immediately-close-client-resources
Sep 30, 2024
Merged

joschi merged 1 commit into
dropwizard:release/4.0.xfrom
zUniQueX:feature/immediately-close-client-resources

Conversation

@zUniQueX

Copy link
Copy Markdown
Member

Fixes #9246

When retrieving responses from Jersey clients in the dropwizard-testing module to Response objects instead of reading them directly, a user has to ensure the Response gets closed correctly to release resources of the underlying http client.

In earlier versions of dropwizard this wasn't a problem since there an other http client connector was used.

This PR adds a response interceptor reading the whole entity into a byte array to allow releasing http client resources immediately.

@zUniQueX
zUniQueX requested a review from a team as a code owner September 14, 2024 16:33
@github-actions github-actions Bot added this to the 4.0.9 milestone Sep 14, 2024
@zUniQueX

Copy link
Copy Markdown
Member Author

@dropwizard/committers I'm not very happy with this implementation as it may cause performance overheads. Should we allow this behavior only when explicitly requested by a user through a property?

@zUniQueX
zUniQueX force-pushed the feature/immediately-close-client-resources branch from 52fe98c to b49c8f1 Compare September 14, 2024 16:52
@zUniQueX
zUniQueX force-pushed the feature/immediately-close-client-resources branch from b49c8f1 to 25cf2dd Compare September 14, 2024 16:53
@joschi

joschi commented Sep 30, 2024

Copy link
Copy Markdown
Member

I'm not very happy with this implementation as it may cause performance overheads.

Since it's only for HTTP-based tests I'd swallow the pill right now and revisit the issue when there are complaints.

@joschi
joschi merged commit acfe0cb into dropwizard:release/4.0.x Sep 30, 2024
@zUniQueX
zUniQueX deleted the feature/immediately-close-client-resources branch September 30, 2024 08:45
@JoergSiebahn

Copy link
Copy Markdown

Hi there,

I just stumbled over an edge case, likely introduced with this change, that causes a failure when testing for responses with chunked encoding.
We have a service that streams huge amount of data directly from the database as Json to the output stream while still reading from the database. That requires chunked encoding because the content length is unknown when writing the header. That also implies that there is no Content-length header or Content-length being -1.

We verify also the headers to make sure, we are streaming from this endpoint directly. I assume, rewriting to the ByteArrayEntity in this PR causes that the response has a length and (to my surprise) not only a change for getContentLength() of the entity but also for getHeaderString("Content-length") of the Response. We now have a Content-length header matching the actual response size.

If someone stumbles over the same or similar issues when testing responses in detail, you can create your own client with the minimum you need for your test. In our case we only need Json deserialization and changed DW_APP_EXTENSION.client().… to new JerseyClientBuilder().register(newJacksonFeature(DW.getEnvironment().getObjectMapper())).build().….

I hope this helps someone who faces a similar issue after upgrading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test hangs indefinitely

3 participants