This is an automated email from the ASF dual-hosted git repository.

wenjun pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git


The following commit(s) were added to refs/heads/dev by this push:
     new a158f1403f [Improvement][Alert] Add timeout params for HTTP plugin 
(#15174)
a158f1403f is described below

commit a158f1403f82ee0e5443cc6975287634177392cb
Author: 旺阳 <[email protected]>
AuthorDate: Sat Nov 18 22:28:29 2023 +0800

    [Improvement][Alert] Add timeout params for HTTP plugin (#15174)
---
 .../plugin/alert/http/HttpAlertChannelFactory.java   | 11 ++++++++++-
 .../plugin/alert/http/HttpAlertConstants.java        |  6 ++++++
 .../plugin/alert/http/HttpSender.java                | 20 ++++++++++++++++----
 .../alert/http/HttpAlertChannelFactoryTest.java      |  2 +-
 .../plugin/alert/http/HttpAlertChannelTest.java      |  6 ++++++
 .../plugin/alert/http/HttpSenderTest.java            |  4 ++--
 dolphinscheduler-ui/src/locales/en_US/security.ts    |  1 +
 dolphinscheduler-ui/src/locales/zh_CN/security.ts    |  1 +
 8 files changed, 43 insertions(+), 8 deletions(-)

diff --git 
a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactory.java
 
b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactory.java
index 7fca63ea6d..211cf19b9f 100644
--- 
a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactory.java
+++ 
b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactory.java
@@ -23,6 +23,7 @@ import org.apache.dolphinscheduler.alert.api.AlertInputTips;
 import org.apache.dolphinscheduler.spi.params.base.PluginParams;
 import org.apache.dolphinscheduler.spi.params.base.Validate;
 import org.apache.dolphinscheduler.spi.params.input.InputParam;
+import org.apache.dolphinscheduler.spi.params.input.number.InputNumberParam;
 
 import java.util.Arrays;
 import java.util.List;
@@ -79,7 +80,15 @@ public final class HttpAlertChannelFactory implements 
AlertChannelFactory {
                                 .build())
                         .build();
 
-        return Arrays.asList(url, requestType, headerParams, bodyParams, 
contentField);
+        InputNumberParam timeout =
+                InputNumberParam.newBuilder(HttpAlertConstants.NAME_TIMEOUT, 
HttpAlertConstants.TIMEOUT)
+                        .setValue(HttpAlertConstants.DEFAULT_TIMEOUT)
+                        .addValidate(Validate.newBuilder()
+                                .setRequired(false)
+                                .build())
+                        .build();
+
+        return Arrays.asList(url, requestType, headerParams, bodyParams, 
contentField, timeout);
     }
 
     @Override
diff --git 
a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertConstants.java
 
b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertConstants.java
index 22db292d5f..ab4e6dd1c1 100644
--- 
a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertConstants.java
+++ 
b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertConstants.java
@@ -39,6 +39,12 @@ public final class HttpAlertConstants {
 
     public static final String NAME_REQUEST_TYPE = "requestType";
 
+    public static final String TIMEOUT = "$t('timeout')";
+
+    public static final String NAME_TIMEOUT = "timeout";
+
+    public static final int DEFAULT_TIMEOUT = 120;
+
     private HttpAlertConstants() {
         throw new UnsupportedOperationException("This is a utility class and 
cannot be instantiated");
     }
diff --git 
a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java
 
b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java
index 32448393ce..999a0c9599 100644
--- 
a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java
+++ 
b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/main/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSender.java
@@ -23,6 +23,7 @@ import org.apache.dolphinscheduler.common.utils.JSONUtils;
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.http.HttpEntity;
+import org.apache.http.client.config.RequestConfig;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
@@ -32,7 +33,6 @@ import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.util.EntityUtils;
 
-import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
 import java.net.URI;
@@ -63,6 +63,7 @@ public final class HttpSender {
     private final String bodyParams;
     private final String contentField;
     private final String requestType;
+    private final int timeout;
     private String url;
     private HttpRequestBase httpRequest;
 
@@ -73,6 +74,9 @@ public final class HttpSender {
         bodyParams = paramsMap.get(HttpAlertConstants.NAME_BODY_PARAMS);
         contentField = paramsMap.get(HttpAlertConstants.NAME_CONTENT_FIELD);
         requestType = paramsMap.get(HttpAlertConstants.NAME_REQUEST_TYPE);
+        timeout = 
StringUtils.isNotBlank(paramsMap.get(HttpAlertConstants.NAME_TIMEOUT))
+                ? 
Integer.parseInt(paramsMap.get(HttpAlertConstants.NAME_TIMEOUT))
+                : HttpAlertConstants.DEFAULT_TIMEOUT;
     }
 
     public AlertResult send(String msg) {
@@ -107,9 +111,17 @@ public final class HttpSender {
         return alertResult;
     }
 
-    public String getResponseString(HttpRequestBase httpRequest) throws 
IOException {
-        CloseableHttpClient httpClient =
-                
HttpClients.custom().setRetryHandler(HttpServiceRetryStrategy.retryStrategy).build();
+    public String getResponseString(HttpRequestBase httpRequest) throws 
Exception {
+
+        RequestConfig requestConfig = RequestConfig.custom()
+                .setConnectTimeout(timeout * 1000)
+                .setConnectionRequestTimeout(timeout * 1000)
+                .setSocketTimeout(timeout * 1000)
+                .build();
+        CloseableHttpClient httpClient = HttpClients.custom()
+                .setDefaultRequestConfig(requestConfig)
+                
.setRetryHandler(HttpServiceRetryStrategy.retryStrategy).build();
+
         CloseableHttpResponse response = httpClient.execute(httpRequest);
         HttpEntity entity = response.getEntity();
         return EntityUtils.toString(entity, DEFAULT_CHARSET);
diff --git 
a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactoryTest.java
 
b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactoryTest.java
index 462060867e..4597aac2bb 100644
--- 
a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactoryTest.java
+++ 
b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelFactoryTest.java
@@ -39,7 +39,7 @@ public class HttpAlertChannelFactoryTest {
     public void getParamsTest() {
 
         List<PluginParams> pluginParamsList = httpAlertChannelFactory.params();
-        Assertions.assertEquals(5, pluginParamsList.size());
+        Assertions.assertEquals(6, pluginParamsList.size());
     }
 
     @Test
diff --git 
a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelTest.java
 
b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelTest.java
index f5d338f54d..aebf6f9d50 100644
--- 
a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelTest.java
+++ 
b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpAlertChannelTest.java
@@ -98,11 +98,17 @@ public class HttpAlertChannelTest {
                 .addValidate(Validate.newBuilder().setRequired(true).build())
                 .build();
 
+        InputParam timeout = InputParam.newBuilder("timeout", "timeout")
+                .setValue(120)
+                .addValidate(Validate.newBuilder().setRequired(true).build())
+                .build();
+
         paramsList.add(urlParam);
         paramsList.add(headerParams);
         paramsList.add(bodyParams);
         paramsList.add(content);
         paramsList.add(requestType);
+        paramsList.add(timeout);
 
         return JSONUtils.toJsonString(paramsList);
     }
diff --git 
a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSenderTest.java
 
b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSenderTest.java
index 9f4e017b86..be013457ac 100644
--- 
a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSenderTest.java
+++ 
b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-http/src/test/java/org/apache/dolphinscheduler/plugin/alert/http/HttpSenderTest.java
@@ -23,7 +23,6 @@ import static org.mockito.Mockito.spy;
 
 import org.apache.dolphinscheduler.alert.api.AlertResult;
 
-import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -33,7 +32,7 @@ import org.junit.jupiter.api.Test;
 public class HttpSenderTest {
 
     @Test
-    public void sendTest() throws IOException {
+    public void sendTest() throws Exception {
         Map<String, String> paramsMap = new HashMap<>();
         String url = "https://www.dolphinscheduler-not-exists-web.com:12345";;
         String contentField = "content";
@@ -42,6 +41,7 @@ public class HttpSenderTest {
         paramsMap.put(HttpAlertConstants.NAME_HEADER_PARAMS, 
"{\"Content-Type\":\"application/json\"}");
         paramsMap.put(HttpAlertConstants.NAME_BODY_PARAMS, 
"{\"number\":\"123456\"}");
         paramsMap.put(HttpAlertConstants.NAME_CONTENT_FIELD, contentField);
+        paramsMap.put(HttpAlertConstants.NAME_TIMEOUT, 
String.valueOf(HttpAlertConstants.DEFAULT_TIMEOUT));
 
         HttpSender httpSender = spy(new HttpSender(paramsMap));
         doReturn("success").when(httpSender).getResponseString(any());
diff --git a/dolphinscheduler-ui/src/locales/en_US/security.ts 
b/dolphinscheduler-ui/src/locales/en_US/security.ts
index 91b9941722..578047fd14 100644
--- a/dolphinscheduler-ui/src/locales/en_US/security.ts
+++ b/dolphinscheduler-ui/src/locales/en_US/security.ts
@@ -249,6 +249,7 @@ export default {
     headerParams: 'Headers',
     bodyParams: 'Body',
     contentField: 'Content Field',
+    timeout: 'Timeout(s)',
     Keyword: 'Keyword',
     userParams: 'User Params',
     path: 'Script Path',
diff --git a/dolphinscheduler-ui/src/locales/zh_CN/security.ts 
b/dolphinscheduler-ui/src/locales/zh_CN/security.ts
index 86624eb0fc..30f25e5953 100644
--- a/dolphinscheduler-ui/src/locales/zh_CN/security.ts
+++ b/dolphinscheduler-ui/src/locales/zh_CN/security.ts
@@ -246,6 +246,7 @@ export default {
     headerParams: '请求头',
     bodyParams: '请求体',
     contentField: '内容字段',
+    timeout: '超时时间(秒)',
     Keyword: '关键词',
     userParams: '自定义参数',
     path: '脚本路径',

Reply via email to