未验证 提交 5b49f8f9 编写于 作者: H haoyann 提交者: GitHub

Fix alarm httpclient connection leak (#6431)

上级 08bceb90
......@@ -29,6 +29,7 @@ Release Notes.
* Fix kubernetes.client.opeanapi.ApiException.
* Remove filename suffix in the meter active file config.
* Introduce log analysis language (LAL).
* Fix alarm httpclient connection leak.
#### UI
* Update selector scroller to show in all pages.
......
......@@ -74,10 +74,11 @@ public class WebhookCallback implements AlarmCallback {
post.setHeader(HttpHeaders.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON.toString());
StringEntity entity;
CloseableHttpResponse httpResponse = null;
try {
entity = new StringEntity(gson.toJson(alarmMessage), StandardCharsets.UTF_8);
post.setEntity(entity);
CloseableHttpResponse httpResponse = httpClient.execute(post);
httpResponse = httpClient.execute(post);
StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine != null && statusLine.getStatusCode() != HttpStatus.SC_OK) {
log.error("send alarm to " + url + " failure. Response code: " + statusLine.getStatusCode());
......@@ -86,6 +87,15 @@ public class WebhookCallback implements AlarmCallback {
log.error("Alarm to JSON error, " + e.getMessage(), e);
} catch (IOException e) {
log.error("send alarm to " + url + " failure.", e);
} finally {
if (httpResponse != null) {
try {
httpResponse.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
});
} finally {
......
......@@ -134,6 +134,7 @@ public class DingtalkHookCallback implements AlarmCallback {
* Send alarm message to remote endpoint
*/
private void sendAlarmMessage(CloseableHttpClient httpClient, String url, String requestBody) {
CloseableHttpResponse httpResponse = null;
try {
HttpPost post = new HttpPost(url);
post.setConfig(requestConfig);
......@@ -141,7 +142,7 @@ public class DingtalkHookCallback implements AlarmCallback {
post.setHeader(HttpHeaders.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON.toString());
StringEntity entity = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
post.setEntity(entity);
CloseableHttpResponse httpResponse = httpClient.execute(post);
httpResponse = httpClient.execute(post);
StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine != null && statusLine.getStatusCode() != HttpStatus.SC_OK) {
log.error("send dingtalk alarm to {} failure. Response code: {}, Response content: {}", url, statusLine.getStatusCode(),
......@@ -149,6 +150,15 @@ public class DingtalkHookCallback implements AlarmCallback {
}
} catch (Throwable e) {
log.error("send dingtalk alarm to {} failure.", url, e);
} finally {
if (httpResponse != null) {
try {
httpResponse.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
}
}
......@@ -153,6 +153,7 @@ public class FeishuHookCallback implements AlarmCallback {
* Send alarm message to remote endpoint
*/
private void sendAlarmMessage(CloseableHttpClient httpClient, String url, String requestBody) {
CloseableHttpResponse httpResponse = null;
try {
HttpPost post = new HttpPost(url);
post.setConfig(requestConfig);
......@@ -160,7 +161,7 @@ public class FeishuHookCallback implements AlarmCallback {
post.setHeader(HttpHeaders.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON.toString());
StringEntity entity = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
post.setEntity(entity);
CloseableHttpResponse httpResponse = httpClient.execute(post);
httpResponse = httpClient.execute(post);
StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine != null && statusLine.getStatusCode() != HttpStatus.SC_OK) {
log.error("send feishu alarm to {} failure. Response code: {}, Response content: {}", url, statusLine.getStatusCode(),
......@@ -168,6 +169,15 @@ public class FeishuHookCallback implements AlarmCallback {
}
} catch (Throwable e) {
log.error("send feishu alarm to {} failure.", url, e);
} finally {
if (httpResponse != null) {
try {
httpResponse.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
}
}
......@@ -76,6 +76,7 @@ public class SlackhookCallback implements AlarmCallback {
post.setHeader(HttpHeaders.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON.toString());
StringEntity entity;
CloseableHttpResponse httpResponse = null;
try {
JsonObject jsonObject = new JsonObject();
JsonArray jsonElements = new JsonArray();
......@@ -88,7 +89,7 @@ public class SlackhookCallback implements AlarmCallback {
jsonObject.add("blocks", jsonElements);
entity = new StringEntity(GSON.toJson(jsonObject), ContentType.APPLICATION_JSON);
post.setEntity(entity);
CloseableHttpResponse httpResponse = httpClient.execute(post);
httpResponse = httpClient.execute(post);
StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine != null && statusLine.getStatusCode() != HttpStatus.SC_OK) {
log.error("Send slack alarm to {} failure. Response code: {}", url , statusLine.getStatusCode());
......@@ -97,6 +98,15 @@ public class SlackhookCallback implements AlarmCallback {
log.error("Alarm to JSON error, {} ", e.getMessage(), e);
} catch (IOException e) {
log.error("Send slack alarm to {} failure.", url , e);
} finally {
if (httpResponse != null) {
try {
httpResponse.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
});
} finally {
......
......@@ -82,6 +82,7 @@ public class WechatHookCallback implements AlarmCallback {
}
private void sendAlarmMessage(CloseableHttpClient httpClient, String url, String requestBody) {
CloseableHttpResponse httpResponse = null;
try {
HttpPost post = new HttpPost(url);
post.setConfig(requestConfig);
......@@ -89,13 +90,22 @@ public class WechatHookCallback implements AlarmCallback {
post.setHeader(HttpHeaders.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON.toString());
StringEntity entity = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
post.setEntity(entity);
CloseableHttpResponse httpResponse = httpClient.execute(post);
httpResponse = httpClient.execute(post);
StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine != null && statusLine.getStatusCode() != HttpStatus.SC_OK) {
log.error("send wechat alarm to {} failure. Response code: {} ", url, statusLine.getStatusCode());
}
} catch (Throwable e) {
log.error("send wechat alarm to {} failure.", url, e);
} finally {
if (httpResponse != null) {
try {
httpResponse.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册