未验证 提交 f9cb281d 编写于 作者: J Jason Song 提交者: GitHub

Merge pull request #863 from xiaoerlyl/keepalive

clear http response inputstream and errorstream to reuse connection.
......@@ -70,6 +70,7 @@ public class HttpUtil {
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();
......@@ -92,9 +93,18 @@ public class HttpUtil {
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));
}
......@@ -102,17 +112,26 @@ public class HttpUtil {
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,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册