mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-03-22 15:19:59 +07:00
[perf_test]EventBus: implemented ports pool
GitOrigin-RevId: c84e8c2435e7a2f6d9ee14a56c16c84c70a64147
This commit is contained in:
committed by
intellij-monorepo-bot
parent
bc5194066d
commit
5db2bb6d73
@@ -7,6 +7,7 @@ import com.intellij.tools.ide.starter.bus.logger.EventBusLoggerFactory
|
||||
import com.intellij.tools.ide.starter.bus.shared.dto.SharedEventDto
|
||||
import com.intellij.tools.ide.starter.bus.shared.dto.SubscriberDto
|
||||
import com.intellij.tools.ide.starter.bus.shared.server.LocalEventBusServer
|
||||
import java.net.ConnectException
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
import java.rmi.ServerException
|
||||
@@ -53,8 +54,9 @@ class LocalEventBusServerClient(val server: LocalEventBusServer) : EventBusServe
|
||||
it.readText()
|
||||
}
|
||||
}
|
||||
catch (e: Exception) {
|
||||
throw e
|
||||
catch (e: ConnectException) {
|
||||
if (!server.updatePort()) throw e
|
||||
sendRequest(method, endpoint, requestBody)
|
||||
}
|
||||
finally {
|
||||
connection.disconnect()
|
||||
@@ -111,15 +113,6 @@ class LocalEventBusServerClient(val server: LocalEventBusServer) : EventBusServe
|
||||
}
|
||||
|
||||
override fun startServerProcess() {
|
||||
if (!server.startServer()) {
|
||||
try {
|
||||
val onStartEvents = getEvents()
|
||||
LOG.debug("Events on server start: $onStartEvents")
|
||||
}
|
||||
catch (t: Throwable) {
|
||||
LOG.info("Server is running but we cannot get events")
|
||||
throw t
|
||||
}
|
||||
}
|
||||
server.startServer()
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.intellij.tools.ide.starter.bus.shared.server
|
||||
|
||||
interface EventBusServer {
|
||||
val port: Int
|
||||
fun startServer(): Boolean
|
||||
fun startServer()
|
||||
fun endServer()
|
||||
fun updatePort(): Boolean
|
||||
}
|
||||
@@ -15,18 +15,31 @@ import java.util.concurrent.CompletableFuture
|
||||
private val LOG = EventBusLoggerFactory.getLogger(LocalEventBusServer::class.java)
|
||||
|
||||
object LocalEventBusServer : EventBusServer {
|
||||
override val port: Int = 45654
|
||||
private val portsPool: MutableList<Int> = generateSequence(45654) { it + 10 }
|
||||
.takeWhile { it <= 45654 + 100 }
|
||||
.toMutableList()
|
||||
private var currentPortIndex = 0
|
||||
private lateinit var eventsFlowService: EventsFlowService
|
||||
private val objectMapper = jacksonObjectMapper()
|
||||
private lateinit var server: HttpServer
|
||||
|
||||
override val port: Int
|
||||
get() = portsPool[currentPortIndex]
|
||||
|
||||
override fun endServer() {
|
||||
if (this::server.isInitialized) {
|
||||
server.stop(1)
|
||||
currentPortIndex = 0
|
||||
LOG.info("Server stopped")
|
||||
}
|
||||
}
|
||||
|
||||
override fun updatePort(): Boolean {
|
||||
if (currentPortIndex == portsPool.size - 1) return false
|
||||
currentPortIndex++
|
||||
return true
|
||||
}
|
||||
|
||||
private fun handleException(t: Throwable, exchange: HttpExchange) {
|
||||
val response = t.message ?: t.toString()
|
||||
exchange.responseHeaders.add("Content-Type", "text/plain")
|
||||
@@ -34,7 +47,7 @@ object LocalEventBusServer : EventBusServer {
|
||||
exchange.responseBody.bufferedWriter().use { writer -> writer.write(response) }
|
||||
}
|
||||
|
||||
override fun startServer(): Boolean {
|
||||
override fun startServer() {
|
||||
return try {
|
||||
server = HttpServer.create(InetSocketAddress(port), 0)
|
||||
eventsFlowService = EventsFlowService()
|
||||
@@ -122,11 +135,11 @@ object LocalEventBusServer : EventBusServer {
|
||||
|
||||
server.start()
|
||||
LOG.info("Server started on port $port")
|
||||
true
|
||||
}
|
||||
catch (bind: BindException) {
|
||||
LOG.info("Server already running. ${bind.message}")
|
||||
false
|
||||
LOG.info("Port $port is busy. Trying use another")
|
||||
if (!updatePort()) throw BindException("All ports from ports pool are busy")
|
||||
startServer()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user