Skip to content

Commit

Permalink
feat : 포인트 충전과 결제 같은 분산락 key 사용하도록 변경 #38
Browse files Browse the repository at this point in the history
- lockType 추가
- 포인트 충전과 결제를 같은 key를 사용하여 동시성 문제가 발생 안하도록 구현
  • Loading branch information
rlatmd0829 committed May 6, 2024
1 parent 63938a1 commit f19f118
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kpop.ticketing.domain.common.annotation;

import com.kpop.ticketing.domain.common.aspect.LockType;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -9,6 +10,8 @@
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DistributedLock {
LockType lockType() default LockType.DEFAULT;

TimeUnit timeUnit() default TimeUnit.SECONDS;

long waitTime() default 10L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ public Object lock(final ProceedingJoinPoint joinPoint) throws Throwable {

MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
String methodName = method.getName();
RLock rLock = redissonClient.getLock(String.format("lock:%s:%s", methodName, lockKey));
DistributedLock lock = method.getAnnotation(DistributedLock.class);
RLock rLock = redissonClient.getLock(String.format("lock:%s:%s", lock.lockType(), lockKey));

try {
if (!rLock.tryLock(lock.waitTime(), lock.leaseTime(), lock.timeUnit())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.kpop.ticketing.domain.common.aspect;

public enum LockType {
POINT, RESERVATION, DEFAULT
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kpop.ticketing.presentation.payment.usecase;

import com.kpop.ticketing.domain.common.annotation.DistributedLock;
import com.kpop.ticketing.domain.common.aspect.LockType;
import org.springframework.stereotype.Component;

import com.kpop.ticketing.domain.payment.components.PaymentStore;
Expand All @@ -21,7 +22,7 @@ public class ProcessPaymentUseCase {
private final PaymentStore paymentStore;
private final ReservationReader reservationReader;

@DistributedLock
@DistributedLock(lockType = LockType.POINT)
public void execute(Long reservationId) {
Reservation reservation = reservationReader.getReservation(reservationId);
User user = userReader.getUser(reservation.getUser().getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kpop.ticketing.presentation.reservation.usecase;

import com.kpop.ticketing.domain.common.annotation.DistributedLock;
import com.kpop.ticketing.domain.common.aspect.LockType;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -29,7 +30,7 @@ public class ReserveSeatUseCase {
private final ReservationStore reservationStore;
private final WaitTokenReader waitTokenReader;

@DistributedLock
@DistributedLock(lockType = LockType.RESERVATION)
public void execute(String token, Long seatId) {
WaitToken waitToken = waitTokenReader.getWaitTokenByToken(token);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kpop.ticketing.presentation.user.usecase;

import com.kpop.ticketing.domain.common.annotation.DistributedLock;
import com.kpop.ticketing.domain.common.aspect.LockType;
import org.springframework.stereotype.Component;

import com.kpop.ticketing.domain.user.components.UserReader;
Expand All @@ -18,7 +19,7 @@ public class ChargeUserBalanceUseCase {
private final UserReader userReader;
private final UserStore userStore;

@DistributedLock
@DistributedLock(lockType = LockType.POINT)
public void execute(Long userId, UserBalanceRequest userBalanceRequest) {
User user = userReader.getUser(userId);
user.chargeBalance(userBalanceRequest.getChargeAmount());
Expand Down

0 comments on commit f19f118

Please sign in to comment.