[ijent] Manage connections in a separate thread pool to avoid thread starvation

(cherry picked from commit 1440dc39f1e7a077ae2f7d9c0f9471b6da4a3cce)

IJ-CR-149253

GitOrigin-RevId: d19c837cb6d65d7cd9d2ff681882eb8729f7a54b
This commit is contained in:
Konstantin Nisht
2024-11-12 18:34:58 +01:00
committed by intellij-monorepo-bot
parent 9f980af17a
commit a75c93f695

View File

@@ -9,6 +9,7 @@ import com.intellij.platform.eel.component1
import com.intellij.platform.eel.component2
import com.intellij.platform.eel.withConnectionToRemotePort
import com.intellij.platform.ijent.coroutineNameAppended
import com.intellij.platform.ijent.spi.IjentThreadPool
import com.intellij.util.io.toByteArray
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.ClosedSendChannelException
@@ -34,6 +35,7 @@ private val LOG: Logger = Logger.getInstance(EelTunnelsApi::class.java)
*
* This function returns when the server starts accepting connections.
*/
@OptIn(DelicateCoroutinesApi::class)
fun CoroutineScope.forwardLocalPort(tunnels: EelTunnelsApi, localPort: Int, address: EelTunnelsApi.HostAddress) {
val serverSocket = ServerSocket()
serverSocket.bind(InetSocketAddress("localhost", localPort))
@@ -43,7 +45,8 @@ fun CoroutineScope.forwardLocalPort(tunnels: EelTunnelsApi, localPort: Int, addr
}
LOG.info("Accepting a connection within IDE client on port $localPort")
var connectionCounter = 0
launch(Dispatchers.IO + coroutineNameAppended("Local port forwarding server")) {
// DO NOT use Dispatchers.IO here. Sockets live long enough to cause starvation of the IO dispatcher.
launch(IjentThreadPool.asCoroutineDispatcher() + coroutineNameAppended("Local port forwarding server")) {
while (true) {
try {
val socket = runInterruptible {