Skip to content

Commit

Permalink
clear http response inputstream and errorstream to reuse connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunlong Liang committed Dec 4, 2017
1 parent c97d5bb commit 8d31e51
Showing 1 changed file with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public T apply(String input) {
private <T> HttpResponse<T> doGetWithSerializeFunction(HttpRequest httpRequest,
Function<String, T> serializeFunction) {
InputStreamReader isr = null;
InputStreamReader esr = null;
int statusCode;
try {
HttpURLConnection conn = (HttpURLConnection) new URL(httpRequest.getUrl()).openConnection();
Expand All @@ -92,27 +93,45 @@ private <T> HttpResponse<T> doGetWithSerializeFunction(HttpRequest httpRequest,
conn.connect();

statusCode = conn.getResponseCode();
try {
isr = new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8);
} catch (Exception e) {
// ignore
}
try {
esr = new InputStreamReader(conn.getErrorStream(), StandardCharsets.UTF_8);
} catch (Exception e) {
// ignore
}

if (statusCode == 200) {
isr = new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8);
String content = CharStreams.toString(isr);
return new HttpResponse<>(statusCode, serializeFunction.apply(content));
}

if (statusCode == 304) {
return new HttpResponse<>(statusCode, null);
}

} catch (Throwable ex) {
throw new ApolloConfigException("Could not complete get operation", ex);
} finally {
if (isr != null) {
try {
CharStreams.toString(isr);
isr.close();
} catch (IOException e) {
// ignore
}
}

if (esr != null) {
try {
CharStreams.toString(esr);
esr.close();
} catch (Exception e) {
// ignore
}
}
}

throw new ApolloConfigStatusCodeException(statusCode,
Expand Down

0 comments on commit 8d31e51

Please sign in to comment.