Skip to content

Commit

Permalink
feat: add threadpool config
Browse files Browse the repository at this point in the history
  • Loading branch information
novohit committed Nov 13, 2023
1 parent 0b93722 commit a58e33a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
5 changes: 5 additions & 0 deletions server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.cloudshare.server.common.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* @author novo
* @since 2023/11/13
*/
@Slf4j
@Configuration
public class ThreadPoolConfig {

/**
* 核心线程数
*/
public static final Integer CORE_POOL_SIZE = calculateCoreNum();

/**
* 最大线程数
*/
public static final Integer MAX_POOL_SIZE = CORE_POOL_SIZE + (CORE_POOL_SIZE >> 1);


@Bean(name = "baseExecutor", destroyMethod = "shutdown")
public ExecutorService taskExecutor() {
ThreadPoolExecutor baseExecutor = new ThreadPoolExecutor(CORE_POOL_SIZE,
MAX_POOL_SIZE,
30000L,
TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(4096),
new BasicThreadFactory.Builder().namingPattern("base-thread-%d").build(),
((r, executor) -> log.error("The async executor pool is full!"))
);
return baseExecutor;
}

private static Integer calculateCoreNum() {
int cpuCoreNum = Runtime.getRuntime().availableProcessors();
return new BigDecimal(cpuCoreNum).divide(BigDecimal.valueOf(0.2)).intValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@ public class FileDocument extends BaseModel implements Serializable {
@Column(length = 256)
@Comment("MD5")
private String md5;

@Column(columnDefinition = "text")
@Comment("文档内容")
private String content;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.cloudshare.web.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -53,6 +54,8 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
Expand Down Expand Up @@ -80,15 +83,19 @@ public class FileServiceImpl implements FileService {

private final UserService userService;

private final ExecutorService baseExecutor;

public FileServiceImpl(FileRepository fileRepository, FileConverter fileConverter,
StorageEngine storageEngine, FileChunkRepository fileChunkRepository,
TransactionTemplate transactionTemplate, UserService userService) {
TransactionTemplate transactionTemplate, UserService userService,
@Qualifier("baseExecutor") ExecutorService baseExecutor) {
this.fileRepository = fileRepository;
this.fileConverter = fileConverter;
this.storageEngine = storageEngine;
this.fileChunkRepository = fileChunkRepository;
this.transactionTemplate = transactionTemplate;
this.userService = userService;
this.baseExecutor = baseExecutor;
}

@Override
Expand All @@ -106,7 +113,8 @@ public void createDir(DirCreateReqDTO reqDTO) {
null,
0L,
FileType.DIR,
null
null,
""
);
saveFile2DB(dir, true);
}
Expand Down Expand Up @@ -185,6 +193,9 @@ public void singleUpload(FileSingleUploadReqDTO reqDTO) {
MultipartFile multipartFile = reqDTO.file();
long size = multipartFile.getSize();
checkQuota(userContext, size);
CompletableFuture<String> task = CompletableFuture.supplyAsync(() -> {
return "file content";
}, baseExecutor);
// 保存实体
// 上传文件
try {
Expand All @@ -207,7 +218,8 @@ public void singleUpload(FileSingleUploadReqDTO reqDTO) {
context.getRealPath(),
context.getTotalSize(),
FileType.suffix2Type(suffix),
suffix
suffix,
task.join()
);
saveFile2DB(fileDocument, true);
userService.incrementQuota(fileDocument.getSize(), userId);
Expand Down Expand Up @@ -249,7 +261,8 @@ public Boolean secUpload(FileSecUploadReqDTO reqDTO) {
same.getRealPath(),
same.getSize(),
FileType.suffix2Type(suffix),
suffix
suffix,
""
);
saveFile2DB(fileDocument, true);
userService.incrementQuota(fileDocument.getSize(), userId);
Expand Down Expand Up @@ -356,7 +369,8 @@ public void chunkMerge(FileChunkMergeReqDTO reqDTO) {
context.getRealPath(),
totalSize.get(),
FileType.suffix2Type(suffix),
suffix
suffix,
""
);
saveFile2DB(fileDocument, true);
userService.incrementQuota(fileDocument.getSize(), userId);
Expand Down Expand Up @@ -577,7 +591,8 @@ public void copy(FileMoveOrCopyReqDTO reqDTO, Long sourceId, UserContext targetU
file.getRealPath(),
file.getSize(),
file.getType(),
file.getSuffix()
file.getSuffix(),
""
);

String originalPath = file.getPath();
Expand All @@ -603,7 +618,8 @@ public void copy(FileMoveOrCopyReqDTO reqDTO, Long sourceId, UserContext targetU
sub.getRealPath(),
sub.getSize(),
sub.getType(),
sub.getSuffix()
sub.getSuffix(),
""
);
String curDirectory = sub.getCurDirectory().replaceFirst(originalCurDirectory, target);
String path = sub.getPath().replaceFirst(originalCurDirectory, target);
Expand Down Expand Up @@ -670,7 +686,8 @@ private FileDocument assembleFileDocument(
String realPath,
Long size,
FileType fileType,
String suffix
String suffix,
String content
) {
FileDocument fileDocument = new FileDocument();
fileDocument.setFileId(SnowflakeUtil.nextId());
Expand All @@ -684,6 +701,7 @@ private FileDocument assembleFileDocument(
fileDocument.setSize(size);
fileDocument.setType(fileType);
fileDocument.setSuffix(suffix);
fileDocument.setContent(content);
return fileDocument;
}

Expand Down

0 comments on commit a58e33a

Please sign in to comment.