Prechádzať zdrojové kódy

feat: 配置线程池并发限制

weipengtao 6 mesiacov pred
rodič
commit
28d2ed298a

+ 56 - 0
web/src/main/java/cn/seecoder/web/infrastructure/config/DeployAsyncConfig.java

@@ -0,0 +1,56 @@
+package cn.seecoder.web.infrastructure.config;
+
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+
+import java.util.concurrent.Executor;
+import java.util.concurrent.ThreadPoolExecutor;
+
+/**
+ * 部署流水线异步线程池配置
+ */
+@Slf4j
+@Data
+@Configuration
+@ConfigurationProperties(prefix = "seecoder.deploy.thread-pool")
+public class DeployAsyncConfig {
+
+    public static final String DEPLOY_EXECUTOR = "deployExecutor";
+
+    /** 核心线程数 */
+    private int corePoolSize = 60;
+    /** 最大线程数 */
+    private int maxPoolSize = 100;
+    /** 队列容量 */
+    private int queueCapacity = 1000;
+    /** 线程名前缀 */
+    private String threadNamePrefix = "deploy-pipeline-";
+    /** 空闲线程存活时间(秒) */
+    private int keepAliveSeconds = 60;
+    /** 等待所有任务完成后再关闭线程池 */
+    private boolean waitForTasksToCompleteOnShutdown = true;
+    /** 关闭时最长等待时间(秒) */
+    private int awaitTerminationSeconds = 60;
+
+    @Bean(name = DEPLOY_EXECUTOR)
+    public Executor deployExecutor() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        executor.setCorePoolSize(corePoolSize);
+        executor.setMaxPoolSize(maxPoolSize);
+        executor.setQueueCapacity(queueCapacity);
+        executor.setThreadNamePrefix(threadNamePrefix);
+        executor.setKeepAliveSeconds(keepAliveSeconds);
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+        executor.setWaitForTasksToCompleteOnShutdown(waitForTasksToCompleteOnShutdown);
+        executor.setAwaitTerminationSeconds(awaitTerminationSeconds);
+        executor.initialize();
+        log.info("部署线程池初始化完成, 核心线程数: {}, 最大线程数: {}, 队列容量: {}",
+                corePoolSize, maxPoolSize, queueCapacity);
+        return executor;
+    }
+}
+

+ 2 - 1
web/src/main/java/cn/seecoder/web/service/impl/pipeline/DeploymentServiceImpl.java

@@ -28,6 +28,7 @@ import cn.seecoder.web.core.pipeline.inspector.PipelineInspector;
 import cn.seecoder.web.core.pipeline.inspector.PipelineInspectorFactory;
 import cn.seecoder.web.model.vo.pipeline.DeploymentInfoVO;
 import cn.seecoder.web.service.pipeline.DeploymentService;
+import cn.seecoder.web.infrastructure.config.DeployAsyncConfig;
 
 import java.sql.Timestamp;
 import java.util.ArrayList;
@@ -100,7 +101,7 @@ public class DeploymentServiceImpl implements DeploymentService {
     }
 
     @Override
-    @Async
+    @Async(DeployAsyncConfig.DEPLOY_EXECUTOR)
     public void deploy(Integer projectId, Integer pipelineId, Integer userId) throws ServiceException {
         //projectAuthentication(projectId);
         PipelinePO pipelinePO = pipelineMapper.selectById(pipelineId);

+ 9 - 0
web/src/main/resources/application-wpt.yml

@@ -41,6 +41,15 @@ seecoder:
   sonar:
     host: http://environment-7-10.seec.svc.cluster.local
     loginToken: sqa_e35e4ec80dedf481dc8fe12448bf47f8a7648413
+  deploy:
+    thread-pool:
+      core-pool-size: 60
+      max-pool-size: 100
+      queue-capacity: 1000
+      thread-name-prefix: deploy-pipeline-
+      keep-alive-seconds: 60
+      wait-for-tasks-to-complete-on-shutdown: true
+      await-termination-seconds: 60
 
 logging:
   file: