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

[Core Components] OEmbedClientImpl leaks network connections #2717

Merged
merged 4 commits into from
Apr 10, 2024

Conversation

vladbailescu
Copy link
Member

  • Refactored code to move HTTP client creation to a dedicated method and wrap the whole method that does the reading in try-with-resources

* Refactored code to move HTTP client creation to a dedicated method and wrap the whole method that does the reading in try-with-resources
}

protected InputStream getData(String url, HttpClient httpClient) throws IOException, IllegalArgumentException {
HttpResponse response = httpClient.execute(new HttpGet(url));
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively you need to close the HttpResponse (but this is more effort, since you need to defer until the inputstream is read). In the best case the HttpClient is being reused across multiple requests (in order to cache HTTP connections), then releasing individual connections/responses/inputstreams become crucial.

Copy link
Member Author

Choose a reason for hiding this comment

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

The JSON/XML un-marshalling should take care of consuming the input stream. Seems a bit ineffective to consume it to a string/buffer (could be large) and then feed it through the un-marshalers

Copy link
Contributor

@kwin kwin Apr 8, 2024

Choose a reason for hiding this comment

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

The idea is rather to call the unmarshaller inside your ResponseHandler, then you don't need to come up with your own exception handling for not-fully consumed input streams.

Copy link
Member Author

Choose a reason for hiding this comment

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

@kwin , the ResponseHandler-based implementation would look something like: #2718

I can't say I particularly like it or find it better, the HttpResponse is not closable and we are not reusing the client any way between calls.

Copy link

sonarqubecloud bot commented Apr 9, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@vladbailescu vladbailescu merged commit 6907e23 into main Apr 10, 2024
16 checks passed
@vladbailescu vladbailescu deleted the issue/SITES-14725-2 branch April 10, 2024 11:02
@vladbailescu vladbailescu added this to the 2.25.0 milestone Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants