-
Notifications
You must be signed in to change notification settings - Fork 754
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
Conversation
vladbailescu
commented
Apr 8, 2024
- 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)); |
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.
I really recommend implementing https://www.javadoc.io/doc/org.apache.httpcomponents/httpclient/4.3.2/org/apache/http/client/ResponseHandler.html instead and pass that to execute. Otherwise you need to manually make sure to consume the inputstream fully (e.g. with https://hc.apache.org/httpcomponents-core-4.4.x/current/httpcore/apidocs/org/apache/http/util/EntityUtils.html#consumeQuietly(org.apache.http.HttpEntity)).
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.
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.
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 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
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 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.
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.
Quality Gate passedIssues Measures |