-
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 #4 from TeamTroublePainter/feature/DRAW-375
DRAW-375 synchronized -> Redis Lettuce 분산 락 리팩터링
- Loading branch information
Showing
8 changed files
with
80 additions
and
17 deletions.
There are no files selected for viewing
Empty file.
44 changes: 44 additions & 0 deletions
44
adapter/redis/src/main/kotlin/com/xorker/draw/lock/RedisLockAdapter.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,44 @@ | ||
package com.xorker.draw.lock | ||
|
||
import com.xorker.draw.exception.NotFoundLockKeyException | ||
import com.xorker.draw.exception.UnSupportedException | ||
import java.time.Duration | ||
import org.springframework.data.redis.core.RedisTemplate | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
internal class RedisLockAdapter( | ||
private val redisTemplate: RedisTemplate<String, String>, | ||
) : LockRepository { | ||
|
||
override fun <R> lock(key: String, call: () -> R): R { | ||
while (getLock(key).not()) { | ||
try { | ||
Thread.sleep(SLEEP_TIME) | ||
} catch (e: InterruptedException) { | ||
throw UnSupportedException | ||
} | ||
} | ||
try { | ||
return call.invoke() | ||
} finally { | ||
unlock(key) | ||
} | ||
} | ||
|
||
private fun unlock(key: String) { | ||
redisTemplate.delete(key + LOCK) | ||
} | ||
|
||
private fun getLock(key: String): Boolean { | ||
return redisTemplate | ||
.opsForValue() | ||
.setIfAbsent(key + LOCK, LOCK, Duration.ofSeconds(LOCK_TIME)) ?: throw NotFoundLockKeyException | ||
} | ||
|
||
companion object { | ||
private const val LOCK = "lock" | ||
private const val LOCK_TIME = 1L | ||
private const val SLEEP_TIME = 50L | ||
} | ||
} |
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
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
5 changes: 5 additions & 0 deletions
5
domain/src/main/kotlin/com/xorker/draw/lock/LockRepository.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,5 @@ | ||
package com.xorker.draw.lock | ||
|
||
interface LockRepository { | ||
fun <R> lock(key: String, call: () -> R): R | ||
} |