-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: AOP 관련 의존성 추가 * feat: 락 획득/해지 쿼리 구현 * feat: LockAspect 구현 * feat: SubscribeWorkbookUseCase Lock 적용 * feat: AspectConfig 추가
- Loading branch information
1 parent
e24b096
commit 9fdce8d
Showing
7 changed files
with
122 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.few.api.config | ||
|
||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.EnableAspectJAutoProxy | ||
|
||
@Configuration | ||
@EnableAspectJAutoProxy | ||
class AspectConfig |
77 changes: 77 additions & 0 deletions
77
api/src/main/kotlin/com/few/api/domain/common/lock/LockAspect.kt
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 com.few.api.domain.common.lock | ||
|
||
import com.few.api.domain.subscription.usecase.dto.SubscribeWorkbookUseCaseIn | ||
import com.few.api.repo.dao.subscription.SubscriptionDao | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.aspectj.lang.annotation.AfterReturning | ||
import org.aspectj.lang.annotation.AfterThrowing | ||
import org.aspectj.lang.JoinPoint | ||
import org.aspectj.lang.annotation.Aspect | ||
import org.aspectj.lang.annotation.Before | ||
import org.aspectj.lang.annotation.Pointcut | ||
import org.aspectj.lang.reflect.MethodSignature | ||
import org.springframework.stereotype.Component | ||
|
||
@Aspect | ||
@Component | ||
class LockAspect( | ||
private val subscriptionDao: SubscriptionDao, | ||
) { | ||
private val log = KotlinLogging.logger {} | ||
|
||
@Pointcut("@annotation(com.few.api.domain.common.lock.LockFor)") | ||
fun lockPointcut() {} | ||
|
||
@Before("lockPointcut()") | ||
fun before(joinPoint: JoinPoint) { | ||
getLockFor(joinPoint).run { | ||
when (this.identifier) { | ||
LockIdentifier.SUBSCRIPTION_MEMBER_ID_WORKBOOK_ID -> { | ||
val useCaseIn = joinPoint.args[0] as SubscribeWorkbookUseCaseIn | ||
getSubscriptionMemberIdAndWorkBookIdLock(useCaseIn) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun getSubscriptionMemberIdAndWorkBookIdLock(useCaseIn: SubscribeWorkbookUseCaseIn) { | ||
subscriptionDao.getLock(useCaseIn.memberId, useCaseIn.workbookId).run { | ||
if (!this) { | ||
throw IllegalStateException("Already in progress for ${useCaseIn.memberId}'s subscription to ${useCaseIn.workbookId}") | ||
} | ||
log.debug { "Lock acquired for ${useCaseIn.memberId}'s subscription to ${useCaseIn.workbookId}" } | ||
} | ||
} | ||
|
||
@AfterReturning("lockPointcut()") | ||
fun afterReturning(joinPoint: JoinPoint) { | ||
getLockFor(joinPoint).run { | ||
when (this.identifier) { | ||
LockIdentifier.SUBSCRIPTION_MEMBER_ID_WORKBOOK_ID -> { | ||
val useCaseIn = joinPoint.args[0] as SubscribeWorkbookUseCaseIn | ||
releaseSubscriptionMemberIdAndWorkBookIdLock(useCaseIn) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@AfterThrowing("lockPointcut()") | ||
fun afterThrowing(joinPoint: JoinPoint) { | ||
getLockFor(joinPoint).run { | ||
when (this.identifier) { | ||
LockIdentifier.SUBSCRIPTION_MEMBER_ID_WORKBOOK_ID -> { | ||
val useCaseIn = joinPoint.args[0] as SubscribeWorkbookUseCaseIn | ||
releaseSubscriptionMemberIdAndWorkBookIdLock(useCaseIn) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun getLockFor(joinPoint: JoinPoint) = | ||
(joinPoint.signature as MethodSignature).method.getAnnotation(LockFor::class.java) | ||
|
||
private fun releaseSubscriptionMemberIdAndWorkBookIdLock(useCaseIn: SubscribeWorkbookUseCaseIn) { | ||
subscriptionDao.releaseLock(useCaseIn.memberId, useCaseIn.workbookId) | ||
log.debug { "Lock released for ${useCaseIn.memberId}'s subscription to ${useCaseIn.workbookId}" } | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
api/src/main/kotlin/com/few/api/domain/common/lock/LockFor.kt
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,7 @@ | ||
package com.few.api.domain.common.lock | ||
|
||
@Target(AnnotationTarget.FUNCTION) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class LockFor( | ||
val identifier: LockIdentifier, | ||
) |
8 changes: 8 additions & 0 deletions
8
api/src/main/kotlin/com/few/api/domain/common/lock/LockIdentifier.kt
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,8 @@ | ||
package com.few.api.domain.common.lock | ||
|
||
enum class LockIdentifier { | ||
/** | ||
* 구독 테이블에 멤버와 워크북을 기준으로 락을 건다. | ||
*/ | ||
SUBSCRIPTION_MEMBER_ID_WORKBOOK_ID, | ||
} |
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