Unverified Commit f9cb281d by Jason Song Committed by GitHub

Merge pull request #863 from xiaoerlyl/keepalive

clear http response inputstream and errorstream to reuse connection.
parents d0bbf0da b2f24cb1
...@@ -70,6 +70,7 @@ public class HttpUtil { ...@@ -70,6 +70,7 @@ public class HttpUtil {
private <T> HttpResponse<T> doGetWithSerializeFunction(HttpRequest httpRequest, private <T> HttpResponse<T> doGetWithSerializeFunction(HttpRequest httpRequest,
Function<String, T> serializeFunction) { Function<String, T> serializeFunction) {
InputStreamReader isr = null; InputStreamReader isr = null;
InputStreamReader esr = null;
int statusCode; int statusCode;
try { try {
HttpURLConnection conn = (HttpURLConnection) new URL(httpRequest.getUrl()).openConnection(); HttpURLConnection conn = (HttpURLConnection) new URL(httpRequest.getUrl()).openConnection();
...@@ -92,9 +93,18 @@ public class HttpUtil { ...@@ -92,9 +93,18 @@ public class HttpUtil {
conn.connect(); conn.connect();
statusCode = conn.getResponseCode(); 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) { if (statusCode == 200) {
isr = new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8);
String content = CharStreams.toString(isr); String content = CharStreams.toString(isr);
return new HttpResponse<>(statusCode, serializeFunction.apply(content)); return new HttpResponse<>(statusCode, serializeFunction.apply(content));
} }
...@@ -102,17 +112,26 @@ public class HttpUtil { ...@@ -102,17 +112,26 @@ public class HttpUtil {
if (statusCode == 304) { if (statusCode == 304) {
return new HttpResponse<>(statusCode, null); return new HttpResponse<>(statusCode, null);
} }
} catch (Throwable ex) { } catch (Throwable ex) {
throw new ApolloConfigException("Could not complete get operation", ex); throw new ApolloConfigException("Could not complete get operation", ex);
} finally { } finally {
if (isr != null) { if (isr != null) {
try { try {
CharStreams.toString(isr);
isr.close(); isr.close();
} catch (IOException e) { } catch (IOException e) {
// ignore // ignore
} }
} }
if (esr != null) {
try {
CharStreams.toString(esr);
esr.close();
} catch (Exception e) {
// ignore
}
}
} }
throw new ApolloConfigStatusCodeException(statusCode, throw new ApolloConfigStatusCodeException(statusCode,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment