From cd874f4b2c93f9da43efa5ebcb9245be27df295e Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 20 Feb 2021 13:54:30 +0000 Subject: [PATCH] Add connection pooling to the archiving service Signed-off-by: Graham --- archive/build.gradle.kts | 1 + .../org/openrs2/archive/ArchiveModule.kt | 5 ++++ .../org/openrs2/archive/DataSourceProvider.kt | 26 +++++++++++++++++++ .../org/openrs2/archive/DatabaseProvider.kt | 20 ++++---------- buildSrc/src/main/kotlin/Versions.kt | 1 + 5 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 archive/src/main/kotlin/org/openrs2/archive/DataSourceProvider.kt diff --git a/archive/build.gradle.kts b/archive/build.gradle.kts index f4d5afe51b..56abf5ebea 100644 --- a/archive/build.gradle.kts +++ b/archive/build.gradle.kts @@ -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}") diff --git a/archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt b/archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt index 2e210684ca..c690b61ec4 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt @@ -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() { 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) diff --git a/archive/src/main/kotlin/org/openrs2/archive/DataSourceProvider.kt b/archive/src/main/kotlin/org/openrs2/archive/DataSourceProvider.kt new file mode 100644 index 0000000000..3a385534ec --- /dev/null +++ b/archive/src/main/kotlin/org/openrs2/archive/DataSourceProvider.kt @@ -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 { + 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) + } +} diff --git a/archive/src/main/kotlin/org/openrs2/archive/DatabaseProvider.kt b/archive/src/main/kotlin/org/openrs2/archive/DatabaseProvider.kt index 39dd83e9fb..823bb39075 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/DatabaseProvider.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/DatabaseProvider.kt @@ -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 { +public class DatabaseProvider @Inject constructor( + private val dataSource: DataSource +) : Provider { 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) } } diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index 2fb5da433a..a8195d34e0 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -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"