Skip to content

Commit

Permalink
♻️ Refactor/296-async io blocking 최대한 줄이기
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanvp committed Feb 5, 2024
1 parent e653f9a commit 1ae8910
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions src/main/java/zipdabang/server/converter/RecipeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import zipdabang.server.apiPayload.code.CommonStatus;
Expand All @@ -26,6 +28,8 @@
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -704,6 +708,8 @@ public static ReportedComment toCommentReport(Report report, Comment comment, Me
// @Value("${cloud.aws.s3.user-default-image}")
// String userDefaultImage;

private static final ExecutorService ioExecutor = Executors.newFixedThreadPool(Math.min(Runtime.getRuntime().availableProcessors(), 8));

public static RecipeResponseDto.RecipeStatusDto toTestRecipeStatusDto(TestRecipe recipe) {
return RecipeResponseDto.RecipeStatusDto.builder()
.recipeId(recipe.getId())
Expand All @@ -719,32 +725,36 @@ public static String uploadTestThumbnail(MultipartFile thumbnail) throws IOExcep
return fileUrl;
}

public static String uploadTestStep(MultipartFile thumbnail) throws IOException {
public static String uploadTestStep(MultipartFile stepImage) throws IOException {
Uuid uuid = staticAmazonS3Manager.createUUID();
String keyName = staticAmazonS3Manager.generateTestStepKeyName(uuid);
String fileUrl = staticAmazonS3Manager.uploadFile(keyName, thumbnail);
String fileUrl = staticAmazonS3Manager.uploadFile(keyName, stepImage);
log.info("S3에 업로드 한 test step 파일의 url : {}", fileUrl);
return fileUrl;
}

public static TestRecipe toTestRecipe(RecipeRequestDto.CreateRecipeDto request, MultipartFile thumbnail) throws IOException {

TestRecipe recipe = TestRecipe.builder()
CompletableFuture<TestRecipe> buildRecipe = new CompletableFuture<>();
CompletableFuture<String> setThumbnail = new CompletableFuture<>();

ioExecutor.submit(() -> buildRecipe.complete(TestRecipe.builder()
.isBarista(false)
.name(request.getName())
.intro(request.getIntro())
.recipeTip(request.getRecipeTip())
.time(request.getTime())
.build();
.build()));

String imageUrl = null;
if(thumbnail != null)
imageUrl = uploadTestThumbnail(thumbnail);
ioExecutor.submit(()-> setThumbnail.complete(uploadTestThumbnail(thumbnail)));
else
throw new RecipeException(CommonStatus.NULL_RECIPE_ERROR);
recipe.setThumbnail(imageUrl);

return recipe;
return buildRecipe.thenCombine(setThumbnail, (recipe, imageUrl) -> {
recipe.setThumbnail(imageUrl);
return recipe;
}).join();
}


Expand All @@ -762,7 +772,7 @@ private static TestRecipeCategoryMapping toTestRecipeCategoryMappingDto(Long cat
}

public static CompletableFuture<List<TestStep>> toTestStep(RecipeRequestDto.CreateRecipeDto request, TestRecipe recipe, List<MultipartFile> stepImages) {
return CompletableFuture.completedFuture(request.getSteps().stream().parallel()
return CompletableFuture.supplyAsync(() -> request.getSteps().stream().parallel()
.map(step-> {
if (step.getDescription() == null)
throw new RecipeException(CommonStatus.NULL_RECIPE_ERROR);
Expand All @@ -772,36 +782,37 @@ public static CompletableFuture<List<TestStep>> toTestStep(RecipeRequestDto.Crea
throw new RuntimeException(e);
}
})
.collect(Collectors.toList())
.collect(Collectors.toList()), ioExecutor
);
}

private static TestStep toTestStepDto(RecipeRequestDto.StepDto step, TestRecipe recipe, List<MultipartFile> stepImages) throws IOException {

TestStep createdStep = TestStep.builder()
CompletableFuture<TestStep> buildStep = new CompletableFuture<>();
CompletableFuture<String> setStep = new CompletableFuture<>();

ioExecutor.submit(() -> buildStep.complete(TestStep.builder()
.stepNum(step.getStepNum())
.description(step.getDescription())
.recipe(recipe)
.build();
.build()));

MultipartFile stepImage = null;

for (int i = 0; i < stepImages.size(); i++) {
for (int i = 0; i <= stepImages.size(); i++) {
Integer imageNum = Integer.parseInt(stepImages.get(i).getOriginalFilename().substring(0,1)) + 1;
if (imageNum == step.getStepNum()){
stepImage = stepImages.get(i);
MultipartFile stepImage = stepImages.get(i);
ioExecutor.submit(()-> setStep.complete(uploadTestStep(stepImage)));
break;
}
else if(i == stepImages.size())
throw new RecipeException(CommonStatus.NULL_RECIPE_ERROR);
}

String imageUrl = null;
if(stepImages != null)
imageUrl = uploadTestStep(stepImage);
else
throw new RecipeException(CommonStatus.NULL_RECIPE_ERROR);
createdStep.setImage(imageUrl);
return buildStep.thenCombine(setStep, (createStep, imageUrl) -> {
createStep.setImage(imageUrl);
return createStep;
}).join();

return createdStep;
}

public static CompletableFuture<List<TestIngredient>> toTestIngredient(RecipeRequestDto.CreateRecipeDto request, TestRecipe recipe) {
Expand Down

0 comments on commit 1ae8910

Please sign in to comment.