Skip to content

Commit

Permalink
feat(exposed): configurable tx parallelism
Browse files Browse the repository at this point in the history
  • Loading branch information
LizAinslie committed Nov 6, 2024
1 parent 7116110 commit e32447c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package sh.illumi.kraft.x.exposed

import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.transactions.transaction
Expand All @@ -13,16 +17,21 @@ sealed interface DbManager {
val isPooled: Boolean
val layer: RootLayer

var transactionContext: CoroutineDispatcher

class Hikari(
override val layer: RootLayer,
configure: ExposedConfigurationDsl.Hikari.() -> Unit
) : DbManager {
override var transactionContext: CoroutineDispatcher
private val dataSource: HikariDataSource

init {
val dsl = ExposedConfigurationDsl.Hikari().apply(configure)
dsl.check()

transactionContext = Dispatchers.IO.limitedParallelism(dsl.transactionParallelism)

dataSource = HikariDataSource(HikariConfig().apply {
jdbcUrl = dsl.jdbcUrl
driverClassName = dsl.driverClassName
Expand All @@ -44,6 +53,7 @@ sealed interface DbManager {
override val layer: RootLayer,
configure: ExposedConfigurationDsl.() -> Unit
) : DbManager {
override var transactionContext: CoroutineDispatcher
private val jdbcUrl: String
private val driverClassName: String
private val username: String
Expand All @@ -53,6 +63,8 @@ sealed interface DbManager {
val dsl = ExposedConfigurationDsl().apply(configure)
dsl.check()

transactionContext = Dispatchers.IO.limitedParallelism(dsl.transactionParallelism)

jdbcUrl = dsl.jdbcUrl
driverClassName = dsl.driverClassName
username = dsl.username
Expand All @@ -65,5 +77,7 @@ sealed interface DbManager {
}
}

fun <T> transaction(block: Transaction.() -> T) = transaction(db, block)
fun <T> transaction(block: Transaction.() -> T) = layer.coroutineScope.async {
withContext(transactionContext) { transaction(db, block) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ open class ExposedConfigurationDsl {
var driverClassName: String = ""
var username: String = ""
var password: String = ""
var transactionParallelism = 10

open fun check() {
require(jdbcUrl.isNotBlank()) { "jdbcUrl must be set" }
require(driverClassName.isNotBlank()) { "driverClassName must be set" }
require(username.isNotBlank()) { "username must be set" }
require(password.isNotBlank()) { "password must be set" }
require(transactionParallelism > 0) { "transactionParallelism must be greater than 0" }
}

class Hikari : ExposedConfigurationDsl() {
Expand Down

0 comments on commit e32447c

Please sign in to comment.