-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lock implementation
- Loading branch information
Showing
6 changed files
with
158 additions
and
12 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
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/com/vauthenticator/server/job/LockService.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,42 @@ | ||
package com.vauthenticator.server.job | ||
|
||
import org.slf4j.LoggerFactory | ||
import org.springframework.data.redis.core.RedisTemplate | ||
import java.util.concurrent.TimeUnit | ||
|
||
|
||
interface LockService { | ||
|
||
fun lock(timeout: Long) | ||
|
||
fun unlock() | ||
|
||
} | ||
|
||
class RedisLockService( | ||
private val redisTemplate: RedisTemplate<String, String> | ||
) : LockService { | ||
|
||
private val logger = LoggerFactory.getLogger(RedisLockService::class.java) | ||
|
||
override fun lock(timeout: Long) { | ||
if (!acquireLockWith(timeout)) { | ||
logger.info("lock already acquired") | ||
Thread.sleep(timeout) | ||
} else { | ||
logger.info("lock acquired") | ||
} | ||
} | ||
|
||
private fun acquireLockWith(timeout: Long): Boolean { | ||
logger.info("try to acquire Lock") | ||
return redisTemplate.opsForValue() | ||
.setIfAbsent("lockKey", "locked", timeout, TimeUnit.MILLISECONDS)!! | ||
} | ||
|
||
override fun unlock() { | ||
logger.info("lock released") | ||
redisTemplate.opsForValue().getAndDelete("lockKey") | ||
} | ||
|
||
} |
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
31 changes: 31 additions & 0 deletions
31
src/test/kotlin/com/vauthenticator/server/job/RedisLockServiceTest.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,31 @@ | ||
package com.vauthenticator.server.job | ||
|
||
import io.mockk.every | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.junit5.MockKExtension | ||
import io.mockk.mockk | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.springframework.data.redis.core.RedisTemplate | ||
import org.springframework.data.redis.core.ValueOperations | ||
import java.util.concurrent.TimeUnit | ||
|
||
@ExtendWith(MockKExtension::class) | ||
class RedisLockServiceTest { | ||
|
||
@MockK | ||
lateinit var redisTemplate: RedisTemplate<String, String> | ||
|
||
@Test | ||
fun `when lock and unlock`() { | ||
val uut = RedisLockService(redisTemplate) | ||
|
||
every { redisTemplate.opsForValue() } returns mockk<ValueOperations<String, String>> { | ||
every { setIfAbsent("lockKey", "locked", 100, TimeUnit.MILLISECONDS) } returns true | ||
every { getAndDelete("lockKey") } returns "lockKey" | ||
} | ||
|
||
uut.lock(100) | ||
uut.unlock() | ||
} | ||
} |