Hey guys, I'm writing a low latency TCP proxy usin...
# coroutines
b
Hey guys, I'm writing a low latency TCP proxy using
java.nio.AsynchronousSocketServerChannel
and I'm using coroutine channels to push my data to a
writerChannel
which is responsible for forwarding the data between a client and server. I'm noticing some start delays between when I push data to my channel and when its consumer actually starts. my code looks like something of the following
Copy code
//inside the ReadHandler future, push the data read from the socket each time the future completes, also record the start time
val startTime = System.nanoTime()
writerChannel.trySendBlocking(Pair(data, startTime))

//somewhere else in the codebase, consume the data from the channel
scope.launch(Dispatchers.Unconfined) {
    writerChannel.consumeEach { (data, startTime) ->
        val startElapsed = (System.nanoTime() - startTime) / 1_000_000
        println("Took: ${startElapsed}ms to start!")

        //do some other stuff with the data
    }
}
In one of my tests, I open up 200 simultaneous connections, and every now and then it looks like a coroutine has a cold start of about (2-4)ms with some outliers of upwards of 8ms. Is there any way how I can reduce the amount of time it takes a coroutine to start like that?