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

Fix alarm httpclient connection leak (#6431)

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