11import io.netty.channel.unix.Errors.NativeIoException
2+ import io.vertx.core.MultiMap
23import io.vertx.core.buffer.Buffer
34import io.vertx.core.http.HttpHeaders
45import io.vertx.core.http.HttpServer
@@ -18,11 +19,9 @@ import io.vertx.sqlclient.Row
1819import io.vertx.sqlclient.RowSet
1920import io.vertx.sqlclient.Tuple
2021import kotlinx.coroutines.Dispatchers
21- import kotlinx.datetime.TimeZone
2222import kotlinx.datetime.UtcOffset
2323import kotlinx.datetime.format.DateTimeComponents
2424import kotlinx.datetime.format.format
25- import kotlinx.datetime.toLocalDateTime
2625import kotlinx.html.*
2726import kotlinx.html.stream.appendHTML
2827import kotlinx.io.buffered
@@ -34,14 +33,21 @@ import kotlinx.serialization.json.io.encodeToSink
3433import kotlin.time.Clock
3534
3635class MainVerticle (val hasDb : Boolean ) : CoroutineVerticle(), CoroutineRouterSupport {
36+ object HttpHeaderValues {
37+ val vertxWeb = HttpHeaders .createOptimized(" Vert.x-Web" )
38+ val applicationJson = HttpHeaders .createOptimized(" application/json" )
39+ val textHtmlCharsetUtf8 = HttpHeaders .createOptimized(" text/html; charset=utf-8" )
40+ val textPlain = HttpHeaders .createOptimized(" text/plain" )
41+ }
42+
3743 // `PgConnection`s as used in the "vertx" portion offers better performance than `PgPool`s.
3844 lateinit var pgConnection: PgConnection
3945 lateinit var date: String
4046 lateinit var httpServer: HttpServer
4147
4248 lateinit var selectWorldQuery: PreparedQuery <RowSet <Row >>
4349 lateinit var selectFortuneQuery: PreparedQuery <RowSet <Row >>
44- lateinit var updateWordQuery : PreparedQuery <RowSet <Row >>
50+ lateinit var updateWorldQuery : PreparedQuery <RowSet <Row >>
4551
4652 fun setCurrentDate () {
4753 date = DateTimeComponents .Formats .RFC_1123 .format {
@@ -61,18 +67,20 @@ class MainVerticle(val hasDb: Boolean) : CoroutineVerticle(), CoroutineRouterSup
6167 user = " benchmarkdbuser" ,
6268 password = " benchmarkdbpass" ,
6369 cachePreparedStatements = true ,
64- pipeliningLimit = 100000
70+ pipeliningLimit = 256
6571 )
6672 ).coAwait()
6773
6874 selectWorldQuery = pgConnection.preparedQuery(SELECT_WORLD_SQL )
6975 selectFortuneQuery = pgConnection.preparedQuery(SELECT_FORTUNE_SQL )
70- updateWordQuery = pgConnection.preparedQuery(UPDATE_WORLD_SQL )
76+ updateWorldQuery = pgConnection.preparedQuery(UPDATE_WORLD_SQL )
7177 }
7278
7379 setCurrentDate()
7480 vertx.setPeriodic(1000 ) { setCurrentDate() }
75- httpServer = vertx.createHttpServer(httpServerOptionsOf(port = 8080 ))
81+ httpServer = vertx.createHttpServer(
82+ httpServerOptionsOf(port = 8080 , http2ClearTextEnabled = false , strictThreadMode = true )
83+ )
7684 .requestHandler(Router .router(vertx).apply { routes() })
7785 .exceptionHandler {
7886 // wrk resets the connections when benchmarking is finished.
@@ -98,15 +106,17 @@ class MainVerticle(val hasDb: Boolean) : CoroutineVerticle(), CoroutineRouterSup
98106 }
99107
100108 @Suppress(" NOTHING_TO_INLINE" )
101- inline fun HttpServerResponse. putCommonHeaders () {
102- putHeader (HttpHeaders .SERVER , " Vert.x-Web " )
103- putHeader (HttpHeaders .DATE , date)
109+ inline fun MultiMap. addCommonHeaders () {
110+ add (HttpHeaders .SERVER , HttpHeaderValues .vertxWeb )
111+ add (HttpHeaders .DATE , date)
104112 }
105113
106114 @Suppress(" NOTHING_TO_INLINE" )
107- inline fun HttpServerResponse.putJsonResponseHeader () {
108- putCommonHeaders()
109- putHeader(HttpHeaders .CONTENT_TYPE , " application/json" )
115+ inline fun HttpServerResponse.addJsonResponseHeaders () {
116+ headers().run {
117+ addCommonHeaders()
118+ add(HttpHeaders .CONTENT_TYPE , HttpHeaderValues .applicationJson)
119+ }
110120 }
111121
112122
@@ -122,7 +132,7 @@ class MainVerticle(val hasDb: Boolean) : CoroutineVerticle(), CoroutineRouterSup
122132 ) =
123133 coHandlerUnconfined {
124134 it.response().run {
125- putJsonResponseHeader ()
135+ addJsonResponseHeaders ()
126136
127137 /*
128138 // approach 1
@@ -202,20 +212,23 @@ class MainVerticle(val hasDb: Boolean) : CoroutineVerticle(), CoroutineRouterSup
202212 }
203213
204214 it.response().run {
205- putCommonHeaders()
206- putHeader(HttpHeaders .CONTENT_TYPE , " text/html; charset=utf-8" )
215+ headers().run {
216+ addCommonHeaders()
217+ add(HttpHeaders .CONTENT_TYPE , HttpHeaderValues .textHtmlCharsetUtf8)
218+ }
207219 end(htmlString)/* .coAwait()*/
208220 }
209221 }
210222
223+ // Some changes to this part in the `vertx` portion in #9142 are not ported.
211224 get(" /updates" ).jsonResponseCoHandler(Serializers .worlds) {
212225 val queries = it.request().getQueries()
213226 val worlds = selectRandomWorlds(queries)
214227 val updatedWorlds = worlds.map { it.copy(randomNumber = randomIntBetween1And10000()) }
215228
216229 // Approach 1
217230 // The updated worlds need to be sorted first to avoid deadlocks.
218- updateWordQuery
231+ updateWorldQuery
219232 .executeBatch(updatedWorlds.sortedBy { it.id }.map { Tuple .of(it.randomNumber, it.id) }).coAwait()
220233
221234 /*
@@ -230,8 +243,10 @@ class MainVerticle(val hasDb: Boolean) : CoroutineVerticle(), CoroutineRouterSup
230243
231244 get(" /plaintext" ).coHandlerUnconfined {
232245 it.response().run {
233- putCommonHeaders()
234- putHeader(HttpHeaders .CONTENT_TYPE , " text/plain" )
246+ headers().run {
247+ addCommonHeaders()
248+ add(HttpHeaders .CONTENT_TYPE , HttpHeaderValues .textPlain)
249+ }
235250 end(" Hello, World!" )/* .coAwait()*/
236251 }
237252 }
0 commit comments