@@ -32,6 +32,14 @@ import org.jetbrains.ktor.benchmarks.models.World
3232import reactor.core.publisher.Flux
3333import reactor.core.publisher.Mono
3434import kotlin.random.Random
35+ import java.time.Duration
36+
37+ private val json = Json {
38+ prettyPrint = false
39+ isLenient = true
40+ ignoreUnknownKeys = true
41+ coerceInputValues = true
42+ }
3543
3644fun Application.main () {
3745 val config = ApplicationConfig (" application.conf" )
@@ -40,22 +48,23 @@ fun Application.main() {
4048 install(DefaultHeaders )
4149
4250 val helloWorldContent = TextContent (" Hello, World!" , ContentType .Text .Plain )
51+ val helloWorldMsg = Message (" Hello, world!" )
4352
4453 routing {
4554 get(" /plaintext" ) {
4655 call.respond(helloWorldContent)
4756 }
4857
4958 get(" /json" ) {
50- call.respondText(Json .encodeToString(Message ( " Hello, world! " ) ), ContentType .Application .Json )
59+ call.respondText(json .encodeToString(helloWorldMsg ), ContentType .Application .Json )
5160 }
5261
5362 get(" /db" ) {
5463 val random = Random .Default
5564 val request = getWorld(dbConnFactory, random)
5665 val result = request.awaitFirstOrNull()
5766
58- call.respondText(Json .encodeToString(result), ContentType .Application .Json )
67+ call.respondText(json .encodeToString(result), ContentType .Application .Json )
5968 }
6069
6170 fun selectWorlds (queries : Int , random : Random ): Flow <World > = flow {
@@ -74,7 +83,7 @@ fun Application.main() {
7483 }
7584 }
7685
77- call.respondText(Json .encodeToString(result), ContentType .Application .Json )
86+ call.respondText(json .encodeToString(result), ContentType .Application .Json )
7887 }
7988
8089 get(" /fortunes" ) {
@@ -123,19 +132,20 @@ fun Application.main() {
123132 worlds.collect { world ->
124133 world.randomNumber = random.nextInt(DB_ROWS ) + 1
125134 add(world)
126-
127- Mono .usingWhen(dbConnFactory.create(), { connection ->
128- Mono .from(
129- connection.createStatement(UPDATE_QUERY )
130- .bind(0 , world.randomNumber)
131- .bind(1 , world.id)
132- .execute()
133- ).flatMap { Mono .from(it.rowsUpdated) }
134- }, Connection ::close).awaitFirstOrNull()
135135 }
136136 }
137137
138- call.respondText(Json .encodeToString(worldsUpdated), ContentType .Application .Json )
138+ Mono .usingWhen(dbConnFactory.create(), { connection ->
139+ val statement = connection.createStatement(UPDATE_QUERY )
140+ worldsUpdated.forEach { world ->
141+ statement.bind(0 , world.randomNumber)
142+ statement.bind(1 , world.id)
143+ statement.add()
144+ }
145+ Mono .from(statement.execute())
146+ }, Connection ::close).awaitFirstOrNull()
147+
148+ call.respondText(json.encodeToString(worldsUpdated), ContentType .Application .Json )
139149 }
140150 }
141151}
@@ -170,6 +180,9 @@ private fun configurePostgresR2DBC(config: ApplicationConfig): ConnectionFactory
170180 val cp = ConnectionPoolConfiguration .builder(cf)
171181 .initialSize(config.property(" db.initPoolSize" ).getString().toInt())
172182 .maxSize(config.property(" db.maxPoolSize" ).getString().toInt())
183+ .maxIdleTime(Duration .ofSeconds(30 ))
184+ .maxAcquireTime(Duration .ofSeconds(5 ))
185+ .validationQuery(" SELECT 1" )
173186 .build()
174187
175188 return ConnectionPool (cp)
0 commit comments