-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from Team-Going/feature/156
[fix] 중복 회원가입 동시성 문제 해결
- Loading branch information
Showing
7 changed files
with
184 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
doorip-api/src/main/java/org/doorip/user/facade/UserFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.doorip.user.facade; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.doorip.common.Constants; | ||
import org.doorip.user.dto.request.*; | ||
import org.doorip.user.dto.response.ProfileGetResponse; | ||
import org.doorip.user.dto.response.UserSignInResponse; | ||
import org.doorip.user.dto.response.UserSignUpResponse; | ||
import org.doorip.user.repository.LettuceLockRepository; | ||
import org.doorip.user.service.UserService; | ||
import org.springframework.stereotype.Component; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class UserFacade { | ||
private final UserService userService; | ||
private final LettuceLockRepository lettuceLockRepository; | ||
|
||
public void splash(Long userId) { | ||
userService.splash(userId); | ||
} | ||
|
||
public UserSignInResponse signIn(String token, UserSignInRequest request) { | ||
return userService.signIn(token, request); | ||
} | ||
|
||
public UserSignUpResponse signUp(String token, UserSignUpRequest request) { | ||
while (!lettuceLockRepository.lock(token, Constants.SIGN_UP_LOCK)) { | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
try { | ||
return userService.signUp(token, request); | ||
} finally { | ||
lettuceLockRepository.unlock(token); | ||
} | ||
} | ||
|
||
public void signOut(Long userId) { | ||
userService.signOut(userId); | ||
} | ||
|
||
public void withdraw(Long userId) { | ||
userService.withdraw(userId); | ||
} | ||
|
||
public UserSignUpResponse reissue(String refreshToken, UserReissueRequest request) { | ||
return userService.reissue(refreshToken, request); | ||
} | ||
|
||
public ProfileGetResponse getProfile(Long userId) { | ||
return userService.getProfile(userId); | ||
} | ||
|
||
public void updateResult(Long userId, ResultUpdateRequest request) { | ||
userService.updateResult(userId, request); | ||
} | ||
|
||
public void updateProfile(Long userId, ProfileUpdateRequest request) { | ||
userService.updateProfile(userId, request); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
doorip-api/src/test/java/org/doorip/user/facade/UserFacadeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.doorip.user.facade; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.doorip.exception.ConflictException; | ||
import org.doorip.exception.EntityNotFoundException; | ||
import org.doorip.message.ErrorMessage; | ||
import org.doorip.user.dto.request.UserSignInRequest; | ||
import org.doorip.user.dto.request.UserSignUpRequest; | ||
import org.doorip.user.dto.response.UserSignInResponse; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.stream.IntStream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
@Slf4j | ||
@SpringBootTest | ||
class UserFacadeTest { | ||
@Autowired | ||
UserFacade userFacade; | ||
@Value("${oauth.kakao.test}") | ||
String accessToken; | ||
|
||
@AfterEach | ||
void afterEach() { | ||
UserSignInRequest request = new UserSignInRequest("kakao"); | ||
try { | ||
UserSignInResponse response = userFacade.signIn(accessToken, request); | ||
userFacade.withdraw(response.userId()); | ||
} catch (EntityNotFoundException e) { | ||
log.error("After Each Error: ", e); | ||
throw e; | ||
} | ||
} | ||
|
||
@DisplayName("동일한 회원의 회원가입 요청이 동시에 여러 개 들어오는 경우 정상적으로 회원가입된다.") | ||
@Test | ||
void 동일한_회원의_회원가입_요청이_동시에_여러_개_들어오는_경우_정상적으로_회원가입된다() throws InterruptedException { | ||
// given | ||
int threadCount = 20; | ||
ExecutorService executorService = Executors.newFixedThreadPool(32); | ||
CountDownLatch latch = new CountDownLatch(threadCount); | ||
UserSignUpRequest signUpRequest = new UserSignUpRequest("개발자", "동시성 테스트", "kakao"); | ||
UserSignInRequest signInRequest = new UserSignInRequest("kakao"); | ||
|
||
// when | ||
IntStream.range(0, threadCount) | ||
.forEach(i -> | ||
executorService.submit(() -> { | ||
try { | ||
userFacade.signUp(accessToken, signUpRequest); | ||
} catch (ConflictException e) { | ||
log.error("Sub Task Thread Error: ", e); | ||
throw e; | ||
} finally { | ||
latch.countDown(); | ||
} | ||
})); | ||
latch.await(); | ||
|
||
// then | ||
assertThatThrownBy(() -> userFacade.signUp(accessToken, signUpRequest)) | ||
.isInstanceOf(ConflictException.class) | ||
.hasMessage(ErrorMessage.DUPLICATE_USER.getMessage()); | ||
UserSignInResponse response = userFacade.signIn(accessToken, signInRequest); | ||
assertThat(response.userId()).isNotNull(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
doorip-domain/src/main/java/org/doorip/user/repository/LettuceLockRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.doorip.user.repository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.time.Duration; | ||
|
||
@RequiredArgsConstructor | ||
@Repository | ||
public class LettuceLockRepository { | ||
private final RedisTemplate<String, String> redisTemplate; | ||
|
||
public Boolean lock(String token, String lockType) { | ||
return redisTemplate | ||
.opsForValue() | ||
.setIfAbsent(token, lockType, Duration.ofSeconds(3L)); | ||
} | ||
|
||
public void unlock(String token) { | ||
redisTemplate.delete(token); | ||
} | ||
} |