Skip to content

Commit

Permalink
Add mutex back for default sqlite backend
Browse files Browse the repository at this point in the history
Modify ActionQueueService to skip delay if queue is overfilled
  • Loading branch information
PotatoPresident committed Jul 16, 2023
1 parent 396716f commit fcae853
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ object ActionQueueService {

private suspend fun prepareNextBatch() {
job = Ledger.launch {
delay(Ledger.config[DatabaseSpec.batchDelay].ticks)
if (queue.size < Ledger.config[DatabaseSpec.batchSize]) {
delay(Ledger.config[DatabaseSpec.batchDelay].ticks)
}
drainBatch()
prepareNextBatch()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.github.quiltservertools.ledger.utility.Negatable
import com.github.quiltservertools.ledger.utility.PlayerResult
import com.mojang.authlib.GameProfile
import kotlinx.coroutines.delay
import kotlinx.coroutines.sync.Mutex
import net.minecraft.nbt.StringNbtReader
import net.minecraft.util.Identifier
import net.minecraft.util.WorldSavePath
Expand Down Expand Up @@ -64,6 +65,8 @@ object DatabaseManager {
get() = database.dialect.name

private val cache = DatabaseCacheService
private val dbMutex = Mutex()
private var enforceMutex = false

fun setup(dataSource: DataSource?) {
val source = dataSource ?: getDefaultDatasource()
Expand All @@ -72,6 +75,7 @@ object DatabaseManager {

private fun getDefaultDatasource(): DataSource {
val dbFilepath = Ledger.server.getSavePath(WorldSavePath.ROOT).resolve("ledger.sqlite").pathString
enforceMutex = true
return SQLiteDataSource().apply {
url = "jdbc:sqlite:$dbFilepath"
}
Expand Down Expand Up @@ -344,6 +348,7 @@ object DatabaseManager {
}

private suspend fun <T : Any?> execute(body: suspend Transaction.() -> T): T {
if (enforceMutex) dbMutex.lock()
while (Ledger.server.overworld?.savingDisabled != false) {
delay(timeMillis = 1000)
}
Expand All @@ -357,7 +362,7 @@ object DatabaseManager {
})
}
body(this)
}
}.also { if (enforceMutex) dbMutex.unlock() }
}

suspend fun purgeActions(params: ActionSearchParams) {
Expand Down

0 comments on commit fcae853

Please sign in to comment.