Hi, with the lifecycle changes I might have found...
# kotlinx-rpc
a
Hi, with the lifecycle changes I might have found a bug. Should each collection of a flow create a web-socket connection? I've created a minimal reproducer. https://github.com/afTrolle/kotlinx-rpc-websocket-connections/blob/main/kotlinx-rpc-websocket-connections/src/main/kotlin/Main.kt
This is the code,
Copy code
@Rpc
interface Service {
    fun exampleFlow(): Flow<Int>
}

class ServiceImpl : Service {
    override fun exampleFlow(): Flow<Int> = flow {
        var counter = 0
        while (true) {
            emit(counter++)
            delay(100)
        }
    }
}

fun main() {
    embeddedServer(Netty, 8080) {
        server()
        client()
    }.start(wait = true)
}

private fun Application.server() {
    install(Krpc) { serialization { json() } }
    routing {
        rpc {
            log.debug("on route")
            registerService<Service> {
                log.debug("on service")
                ServiceImpl()
            }
        }
    }
}

private fun Application.client() = launch {
    val ktorClient = HttpClient { installKrpc { serialization { json() } } }
    val rpcClient = ktorClient.rpc("<ws://127.0.0.1:8080>")
    val service = rpcClient.withService<Service>()
    while (true) {
        withTimeoutOrNull(2.seconds) {
            service.exampleFlow().collect()
        }
        log.debug("restart collection")
    }
}
Running it through a reverse proxy (ngrok) shows 100 open connections..
Copy code
Connections    ttl   opn   rt1   rt5   p50   p90                           
               178   100   0.05  0.17  52.06  185.08                         
                                                               
HTTP Requests                                                         
-------------                                                         
                                                               
23:15:23.178 CEST GET / 101 Switching Protocols                           
23:15:21.162 CEST GET / 101 Switching Protocols                           
23:15:19.167 CEST GET / 101 Switching Protocols
...
Console-log:
Copy code
DEBUG io.ktor.server.Application -- on route
DEBUG io.ktor.server.Application -- on service
DEBUG io.ktor.server.Application -- restart collection
DEBUG io.ktor.server.Application -- on route
DEBUG io.ktor.server.Application -- restart collection
DEBUG io.ktor.server.Application -- on route
DEBUG io.ktor.server.Application -- restart collection
DEBUG io.ktor.server.Application -- on route
DEBUG io.ktor.server.Application -- restart collection
DEBUG io.ktor.server.Application -- on route
DEBUG io.ktor.server.Application -- restart collection
....
It looks like the service is being retained but creating new websockets for each flow collection
Note: The flow completes on the server side, but the web-socket connection remains open.
@Alexander Sysoev Should I create a ticket?
a
Hi @Alexander af Trolle ! Thanks for noticing! Yes, please create a ticket and I will take a look
a