-
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.
- Loading branch information
Showing
8 changed files
with
169 additions
and
80 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
api/src/main/kotlin/com/oksusu/susu/api/common/lock/LockManager.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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package com.oksusu.susu.api.common.lock | ||
|
||
interface LockManager { | ||
suspend fun <T> lock(type: LockType, key: String, block: suspend () -> T): T | ||
suspend fun <T> lock(key: String, block: suspend () -> T): T | ||
} |
6 changes: 0 additions & 6 deletions
6
api/src/main/kotlin/com/oksusu/susu/api/common/lock/LockType.kt
This file was deleted.
Oops, something went wrong.
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
29 changes: 0 additions & 29 deletions
29
api/src/test/kotlin/com/oksusu/susu/api/ConcurrencyExtension.kt
This file was deleted.
Oops, something went wrong.
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
53 changes: 53 additions & 0 deletions
53
api/src/test/kotlin/com/oksusu/susu/api/testExtension/ConcurrencyExtension.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,53 @@ | ||
package com.oksusu.susu.api.testExtension | ||
|
||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import kotlinx.coroutines.* | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.atomic.AtomicLong | ||
|
||
private val logger = KotlinLogging.logger { } | ||
|
||
const val CONCURRENT_COUNT = 10 | ||
|
||
suspend fun <T> coExecuteConcurrency(successCount: AtomicLong, block: suspend () -> T?) { | ||
coroutineScope { | ||
val deferreds = mutableListOf<Deferred<Unit>>() | ||
for (i in 1..CONCURRENT_COUNT) { | ||
val deferred = async(Dispatchers.IO) { | ||
try { | ||
block() | ||
successCount.getAndIncrement() | ||
} catch (e: Exception) { | ||
logger.error { e } | ||
} | ||
|
||
return@async | ||
} | ||
|
||
deferreds.add(deferred) | ||
} | ||
|
||
awaitAll(*deferreds.toTypedArray()) | ||
} | ||
} | ||
|
||
suspend fun <T> executeConcurrency(successCount: AtomicLong, block: suspend () -> T?) { | ||
val executorService = Executors.newFixedThreadPool(CONCURRENT_COUNT) | ||
val latch = CountDownLatch(CONCURRENT_COUNT) | ||
for (i in 1..CONCURRENT_COUNT) { | ||
executorService.submit { | ||
try { | ||
runBlocking { | ||
block() | ||
} | ||
successCount.getAndIncrement() | ||
} catch (e: Throwable) { | ||
logger.info { e.toString() } | ||
} finally { | ||
latch.countDown() | ||
} | ||
} | ||
} | ||
latch.await() | ||
} |