@@ -5,8 +5,8 @@ import org.gitanimals.core.UpdateUserOrchestrator
55import org.gitanimals.core.auth.UserEntryPoint
66import org.gitanimals.core.filter.MDCFilter.Companion.TRACE_ID
77import org.gitanimals.core.lock.DistributedLock
8+ import org.gitanimals.core.lock.LOCK_KEY_PREFIX
89import org.gitanimals.core.lock.LOCK_KEY_PREFIX.CREATE_NEW_USER
9- import org.gitanimals.core.lock.LOCK_KEY_PREFIX.SET_AUTH
1010import org.gitanimals.render.domain.EntryPoint
1111import org.gitanimals.render.domain.User
1212import org.gitanimals.render.domain.UserService
@@ -34,7 +34,9 @@ class AnimationFacade(
3434 fun getFarmAnimation (username : String ): String {
3535 return when (userService.existsByName(username)) {
3636 true -> {
37- setUserAuthInfoIfNotSet(username)
37+ DistributedLock .withLock(key = " ${LOCK_KEY_PREFIX .SET_AUTH } :$username " ) {
38+ setUserAuthInfoIfNotSet(username)
39+ }
3840
3941 val svgAnimation = userService.getFarmAnimationByUsername(username)
4042 eventPublisher.publishEvent(Visited (username, MDC .get(TRACE_ID )))
@@ -51,7 +53,9 @@ class AnimationFacade(
5153 fun getLineAnimation (username : String , personaId : Long , mode : Mode ): String {
5254 return when (userService.existsByName(username)) {
5355 true -> {
54- setUserAuthInfoIfNotSet(username)
56+ DistributedLock .withLock(key = " ${LOCK_KEY_PREFIX .SET_AUTH } :$username " ) {
57+ setUserAuthInfoIfNotSet(username)
58+ }
5559
5660 val svgAnimation = userService.getLineAnimationByUsername(username, personaId, mode)
5761 eventPublisher.publishEvent(Visited (username, MDC .get(TRACE_ID )))
@@ -89,7 +93,10 @@ class AnimationFacade(
8993 }
9094
9195 private fun createNewUser (username : String ): User {
92- return DistributedLock .withLock(key = " $CREATE_NEW_USER :$username " ) {
96+ return DistributedLock .withLock(
97+ key = " $CREATE_NEW_USER :$username " ,
98+ whenAcquireFailed = { userService.getUserByName(username) }
99+ ) {
93100 runCatching {
94101 val contributionYears = contributionApi.getAllContributionYears(username)
95102 val contributionCountPerYear =
@@ -110,21 +117,19 @@ class AnimationFacade(
110117 }
111118
112119 private fun setUserAuthInfoIfNotSet (username : String ) {
113- DistributedLock .withLock(key = " $SET_AUTH :$username " ) {
114- runCatching {
115- val user = userService.getUserByName(username)
116-
117- if (user.isAuthInfoSet().not ()) {
118- val githubUserAuthInfo = githubRestApi.getGithubUser(user.getName())
119- userService.setAuthInfo(
120- name = user.getName(),
121- entryPoint = EntryPoint .GITHUB ,
122- authenticationId = githubUserAuthInfo.id,
123- )
124- }
125- }.onFailure {
126- logger.info(" Fail to update userAuthInfo cause: ${it.message} " , it)
120+ runCatching {
121+ val user = userService.getUserByName(username)
122+
123+ if (user.isAuthInfoSet().not ()) {
124+ val githubUserAuthInfo = githubRestApi.getGithubUser(user.getName())
125+ userService.setAuthInfo(
126+ name = user.getName(),
127+ entryPoint = EntryPoint .GITHUB ,
128+ authenticationId = githubUserAuthInfo.id,
129+ )
127130 }
131+ }.onFailure {
132+ logger.info(" Fail to update userAuthInfo cause: ${it.message} " , it)
128133 }
129134 }
130135}
0 commit comments