Colton Idle
07/26/2024, 3:30 AMval (dbResponse: Config?, duration: Duration) = measureTimedValue {
database.playerQueries.selectConfig(call.parameters["api"].orEmpty()).executeAsOneOrNull()
}
which is defined as
selectConfig:
SELECT *
FROM config
WHERE api = ?;
My two inclinations were 1) I'm using the wrong db url (my server has internal/external urls) but the hosting service support team told me I'm indeed using the correct url for the db
2) maybe I'm not sharing my db connection? i checked that though. and it is shared. i can also make back to back network calls and they take the same long amount of time.
It's a stretch but not sure what to do from here. my entire server code is essentially just 10 lines at the moment. empty postgres table. server support team says everything looks good but indeed the db query looks slow. Is there any chance there's some SQLDelight/postgres gotcha that queries are just real slow?eygraber
07/26/2024, 3:25 PMColton Idle
07/26/2024, 3:59 PMget("/config/{api}") {
val (dbResponse: Config?, dur: Duration) = measureTimedValue {
database.playerQueries.selectConfig(call.parameters["api"].orEmpty()).executeAsOneOrNull()
}
println("DURATION: $dur") // This is like 600ms+ every time
if (dbResponse != null) {
call.respondText(
"""
{"apiKey":"found!"}
""".trimIndent()
)
} //else default json
}
Colton Idle
07/26/2024, 4:00 PMeygraber
07/26/2024, 4:06 PMColton Idle
07/26/2024, 4:39 PMeygraber
07/26/2024, 4:47 PMColton Idle
07/26/2024, 5:21 PMeygraber
07/26/2024, 5:28 PMeygraber
07/26/2024, 5:34 PMfun main() {
embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = Application::module)
.start(wait = true)
}
fun Application.module() {
configureRouting()
}
private fun Application.configureRouting() {
routing {
get("/") {
call.respondText("Hello World!")
}
}
}
Colton Idle
07/26/2024, 5:57 PMeygraber
07/26/2024, 6:02 PMColton Idle
07/26/2024, 6:17 PMeygraber
07/26/2024, 7:29 PMimport app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.asJdbcDriver
import com.eygraber.db.EsigDatabase
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import io.ktor.server.application.Application
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import io.ktor.server.response.respondText
import io.ktor.server.routing.get
import io.ktor.server.routing.routing
private const val DATABASE_NAME = "esig"
fun main() {
embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = Application::module)
.start(wait = true)
}
fun Application.module() {
configureRouting()
}
private fun Application.configureRouting() {
routing {
get("/") {
db.testQueries.select("").executeAsOneOrNull()
call.respondText("Hello World!")
}
}
}
private val db: EsigDatabase by lazy {
val driver = hikari().asJdbcDriver().apply {
init()
}
EsigDatabase(driver)
}
private fun hikari(): HikariDataSource {
val config = HikariConfig().apply {
jdbcUrl = "jdbc:<postgresql://localhost:5432/$DATABASE_NAME>"
username = "postgres"
password = ""
maximumPoolSize = 3
isAutoCommit = true
addDataSourceProperty("cachePrepStmts", "true")
addDataSourceProperty("prepStmtCacheSize", "250")
addDataSourceProperty("prepStmtCacheSqlLimit", "2048")
validate()
}
return HikariDataSource(config)
}
private fun SqlDriver.init() {
fun SqlDriver.getVersion() = executeQuery(
identifier = null,
sql = """
SELECT description
FROM pg_shdescription
JOIN pg_database ON objoid = pg_database.oid
WHERE datname = '$DATABASE_NAME';
""".trimIndent(),
mapper = { cursor ->
QueryResult.Value(
if(cursor.next().value) {
cursor.getString(0)?.removePrefix("version:")?.toLongOrNull() ?: 0L
}
else {
0L
}
)
},
parameters = 0,
binders = null
).value
fun SqlDriver.setVersion(version: Long) {
execute(null, "COMMENT ON DATABASE $DATABASE_NAME IS 'version:$version';", 0, null)
}
val schema = EsigDatabase.Schema
try {
val databaseFileVersion = getVersion()
if(databaseFileVersion == 0L) {
schema.create(this)
setVersion(1)
println("Created database")
}
else {
if(schema.version > databaseFileVersion) {
schema.migrate(this, databaseFileVersion, schema.version)
setVersion(schema.version)
println("Migrated database ($databaseFileVersion -> ${schema.version})")
}
else {
println("Opened database (v${schema.version})")
}
}
}
catch(t: Throwable) {
throw t
}
}
then create a file called docker-compose.yml
with this content
services:
db:
image: postgres
restart: always
environment:
POSTGRES_DB: esig
POSTGRES_USER: postgres
POSTGRES_HOST_AUTH_METHOD: trust
volumes:
- pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
pgdata:
and run sudo docker compose up
in the same directory as that fileeygraber
07/26/2024, 7:29 PMsqldelight {
databases {
create("EsigDatabase") {
packageName = "com.eygraber.db"
dialect(libs.sqldelight.postgresDialect)
}
}
}
eygraber
07/26/2024, 7:30 PMColton Idle
07/26/2024, 7:37 PMColton Idle
07/27/2024, 1:29 AMColton Idle
07/27/2024, 2:34 PM