Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ internal class ConnectionPool(
else -> 0
}

private val readerChannel = Channel<SQLiteConnection>(capacity = maxReaderConnectionsCount)
private val readerConnections = List(maxReaderConnectionsCount) {
lazy {
createConnection(name).withConfiguration()
private val readerChannel = Channel<Lazy<SQLiteConnection>>(capacity = maxReaderConnectionsCount)

init {
repeat(maxReaderConnectionsCount) {
readerChannel.trySend(
lazy {
createConnection(name).withConfiguration()
},
)
}
}

Expand All @@ -53,8 +58,7 @@ internal class ConnectionPool(
fun acquireReaderConnection() = when(maxReaderConnectionsCount) {
0 -> acquireWriterConnection()
else -> runBlocking {
val firstUninitialized = readerConnections.firstOrNull { !it.isInitialized() }
firstUninitialized?.value ?: readerChannel.receive()
readerChannel.receive().value
}
}

Expand All @@ -65,7 +69,7 @@ internal class ConnectionPool(
fun releaseReaderConnection(connection: SQLiteConnection) = when(maxReaderConnectionsCount) {
0 -> releaseWriterConnection()
else -> runBlocking {
readerChannel.send(connection)
readerChannel.send(lazy { connection })
}
}

Expand Down Expand Up @@ -95,15 +99,12 @@ internal class ConnectionPool(

if(maxReaderConnectionsCount > 0) {
runBlocking {
val createdReadersCount = readerConnections.count { it.isInitialized() }
val readers = List(createdReadersCount) {
readerChannel.receive()
}
readers.forEach { reader ->
repeat(maxReaderConnectionsCount) {
val reader = readerChannel.receive()
try {
reader.writePragma(sql)
reader.value.writePragma(sql)
} finally {
releaseReaderConnection(reader)
releaseReaderConnection(reader.value)
}
}
}
Expand All @@ -125,7 +126,8 @@ internal class ConnectionPool(
writerMutex.withLock {
writerConnection.close()
}
readerConnections.forEach { reader ->
repeat(maxReaderConnectionsCount) {
val reader = readerChannel.receive()
if(reader.isInitialized()) reader.value.close()
}
readerChannel.close()
Expand Down
Loading