which is better the first block or the second? ```...
# coroutines
l
which is better the first block or the second?
Copy code
scope.launch {
            while (isActive) {
                launch {
                    val record = aKafkaConsumer.poll(Duration.ofMillis(200))
                    log.debug().log("[received records] - count={}", record.count())
                    record.forEach {
                        callback(KafkaMessage(it.key(), it.value()))
                    }
                }
            }
        }
        
 
        scope.launch {
            while (isActive) {
                val record = aKafkaConsumer.poll(Duration.ofMillis(200))
                log.debug().log("[received records] - count={}", record.count())
                record.map {
                    launch {  callback(KafkaMessage(it.key(), it.value()))    }
                }.joinAll()
            }
        }
z
The first one is almost certainly quite bad – you’re going to spin up new coroutines as fast as you can, and idk what
aKafkaConsumer.poll(Duration.ofMillis(200))
does, but if it blocks, you’re going to be blocking a lot of threads in a hurry.
I think you can rewrite the second to make your intent a little more obvious and the code potentially a little more refactor-proof:
Copy code
scope.launch {
    while (isActive) {
        val record = aKafkaConsumer.poll(Duration.ofMillis(200))
        log.debug().log("[received records] - count={}", record.count())

        // Create a sub-scope to wait until all callbacks are processed.
        coroutineScope {
            record.forEach {
                launch {  callback(KafkaMessage(it.key(), it.value()))    }
            }
        }
    }
}
l
thank you, yes, don't know what I was thinking, also just realized calling aKafkaConsumer.poll isn't thread safe
was trying to avoid calling lauch for each message and some what group them
z
group under a common parent?
coroutineScope{}
will effectively do that