Skip to content

Commit

Permalink
fix: 일정 할당 동시성 제어
Browse files Browse the repository at this point in the history
  • Loading branch information
f1v3-dev committed Oct 11, 2024
1 parent f272a2d commit 97e524d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.dnd.jjakkak.domain.schedule.dto.request.ScheduleUpdateRequestDto;
import com.dnd.jjakkak.domain.schedule.dto.response.ScheduleAssignResponseDto;
import com.dnd.jjakkak.domain.schedule.dto.response.ScheduleResponseDto;
import com.dnd.jjakkak.domain.schedule.facade.ScheduleFacade;
import com.dnd.jjakkak.domain.schedule.service.ScheduleService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand All @@ -23,6 +24,7 @@
public class ScheduleController {

private final ScheduleService scheduleService;
private final ScheduleFacade scheduleFacade;

/**
* 회원의 일정을 모임에 할당하는 메서드입니다.
Expand All @@ -37,7 +39,7 @@ public ResponseEntity<Void> assignScheduleToMember(@PathVariable("meetingUuid")
@AuthenticationPrincipal Long memberId,
@Valid @RequestBody ScheduleAssignRequestDto requestDto) {

scheduleService.assignScheduleToMember(memberId, meetingUuid, requestDto);
scheduleFacade.assignScheduleToMember(memberId, meetingUuid, requestDto);
return ResponseEntity.ok().build();
}

Expand All @@ -52,7 +54,7 @@ public ResponseEntity<Void> assignScheduleToMember(@PathVariable("meetingUuid")
public ResponseEntity<ScheduleAssignResponseDto> assignScheduleToGuest(@PathVariable("meetingUuid") String meetingUuid,
@Valid @RequestBody ScheduleAssignRequestDto requestDto) {

ScheduleAssignResponseDto responseDto = scheduleService.assignScheduleToGuest(meetingUuid, requestDto);
ScheduleAssignResponseDto responseDto = scheduleFacade.assignScheduleToGuest(meetingUuid, requestDto);
return ResponseEntity.ok(responseDto);
}

Expand Down Expand Up @@ -95,7 +97,7 @@ public ResponseEntity<ScheduleResponseDto> getGuestSchedule(@PathVariable("meeti
*/
@GetMapping("/check")
public ResponseEntity<Boolean> getMemberScheduleWrite(@PathVariable("meetingUuid") String meetingUuid,
@AuthenticationPrincipal Long memberId){
@AuthenticationPrincipal Long memberId) {
return ResponseEntity.ok(scheduleService.getMemberScheduleWrite(meetingUuid, memberId));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.dnd.jjakkak.domain.schedule.facade;

import com.dnd.jjakkak.domain.redis.RedisRepository;
import com.dnd.jjakkak.domain.schedule.dto.request.ScheduleAssignRequestDto;
import com.dnd.jjakkak.domain.schedule.dto.response.ScheduleAssignResponseDto;
import com.dnd.jjakkak.domain.schedule.service.ScheduleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
* 일정 Facade 클래스입니다.
*
* @author 정승조
* @version 2024. 10. 11.
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class ScheduleFacade {

private final RedisRepository redisRepository;
private final ScheduleService scheduleService;

public void assignScheduleToMember(Long memberId, String meetingUuid, ScheduleAssignRequestDto requestDto) {
String key = "meeting-" + meetingUuid;
while (!redisRepository.lock(key)) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

try {
scheduleService.assignScheduleToMember(memberId, meetingUuid, requestDto);
} finally {
redisRepository.delete(key);
}
}

public ScheduleAssignResponseDto assignScheduleToGuest(String meetingUuid, ScheduleAssignRequestDto requestDto) {
String key = "meeting-" + meetingUuid;
while (!redisRepository.lock(key)) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

try {
return scheduleService.assignScheduleToGuest(meetingUuid, requestDto);
} finally {
redisRepository.delete(key);
}
}
}

0 comments on commit 97e524d

Please sign in to comment.