Browse Source

Add connection pooling to the archiving service

Signed-off-by: Graham <gpe@openrs2.org>
pull/132/head
Graham 1 year ago
parent
commit
cd874f4b2c
  1. 1
      archive/build.gradle.kts
  2. 5
      archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt
  3. 26
      archive/src/main/kotlin/org/openrs2/archive/DataSourceProvider.kt
  4. 20
      archive/src/main/kotlin/org/openrs2/archive/DatabaseProvider.kt
  5. 1
      buildSrc/src/main/kotlin/Versions.kt

1
archive/build.gradle.kts

@ -22,6 +22,7 @@ dependencies { @@ -22,6 +22,7 @@ dependencies {
implementation(project(":protocol"))
implementation(project(":util"))
implementation("com.google.guava:guava:${Versions.guava}")
implementation("com.zaxxer:HikariCP:${Versions.hikaricp}")
implementation("io.ktor:ktor-jackson:${Versions.ktor}")
implementation("io.ktor:ktor-server-netty:${Versions.ktor}")
implementation("io.ktor:ktor-thymeleaf:${Versions.ktor}")

5
archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt

@ -14,6 +14,7 @@ import org.openrs2.db.Database @@ -14,6 +14,7 @@ import org.openrs2.db.Database
import org.openrs2.http.HttpModule
import org.openrs2.json.JsonModule
import org.openrs2.net.NetworkModule
import javax.sql.DataSource
public object ArchiveModule : AbstractModule() {
override fun configure() {
@ -23,6 +24,10 @@ public object ArchiveModule : AbstractModule() { @@ -23,6 +24,10 @@ public object ArchiveModule : AbstractModule() {
install(JsonModule)
install(NetworkModule)
bind(DataSource::class.java)
.toProvider(DataSourceProvider::class.java)
.`in`(Scopes.SINGLETON)
bind(Database::class.java)
.toProvider(DatabaseProvider::class.java)
.`in`(Scopes.SINGLETON)

26
archive/src/main/kotlin/org/openrs2/archive/DataSourceProvider.kt

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
package org.openrs2.archive
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import org.flywaydb.core.Flyway
import org.postgresql.ds.PGSimpleDataSource
import javax.inject.Provider
import javax.sql.DataSource
public class DataSourceProvider : Provider<DataSource> {
override fun get(): DataSource {
val dataSource = PGSimpleDataSource()
// TODO(gpe): make the URL configurable
dataSource.setUrl("jdbc:postgresql://localhost/runearchive?user=gpe&password=gpe")
Flyway.configure()
.dataSource(dataSource)
.locations("classpath:/org/openrs2/archive")
.load()
.migrate()
val config = HikariConfig()
config.dataSource = dataSource
return HikariDataSource(config)
}
}

20
archive/src/main/kotlin/org/openrs2/archive/DatabaseProvider.kt

@ -1,25 +1,15 @@ @@ -1,25 +1,15 @@
package org.openrs2.archive
import org.flywaydb.core.Flyway
import org.openrs2.db.Database
import org.openrs2.db.PostgresDeadlockDetector
import org.postgresql.ds.PGSimpleDataSource
import javax.inject.Inject
import javax.inject.Provider
import javax.sql.DataSource
public class DatabaseProvider : Provider<Database> {
public class DatabaseProvider @Inject constructor(
private val dataSource: DataSource
) : Provider<Database> {
override fun get(): Database {
val dataSource = PGSimpleDataSource()
// TODO(gpe): make the URL configurable
dataSource.setUrl("jdbc:postgresql://localhost/runearchive?user=gpe&password=gpe")
Flyway.configure()
.dataSource(dataSource)
.locations("classpath:/org/openrs2/archive")
.load()
.migrate()
// TODO(gpe): wrap dataSource with HikariCP? how do we close the pool?
return Database(dataSource, deadlockDetector = PostgresDeadlockDetector)
}
}

1
buildSrc/src/main/kotlin/Versions.kt

@ -13,6 +13,7 @@ object Versions { @@ -13,6 +13,7 @@ object Versions {
const val guava = "30.1-jre"
const val guice = "5.0.0-BETA-1"
const val h2 = "1.4.200"
const val hikaricp = "4.0.2"
const val inlineLogger = "1.0.3"
const val jackson = "2.12.1"
const val javaParser = "3.19.0"

Loading…
Cancel
Save