Add connection pooling to the archiving service

Signed-off-by: Graham <gpe@openrs2.org>
Graham 4 years ago
parent c5bd044574
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

@ -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}")

@ -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)

@ -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)
}
}

@ -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)
}
}

@ -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