Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ private class DefaultKafkaReceiver<K, V>(private val settings: ReceiverSettings<
@OptIn(ExperimentalCoroutinesApi::class)
override fun receive(topicNames: Collection<String>): Flow<ReceiverRecord<K, V>> =
scopedConsumer(settings.groupId) { scope, dispatcher, consumer ->
val loop = PollLoop(topicNames, settings, consumer, scope)
loop.receive().flowOn(dispatcher)
val loop = PollLoop(topicNames, settings, consumer, scope, currentCoroutineContext())
loop.receive()
.flatMapConcat { records ->
records.map { record ->
ReceiverRecord(record, loop.toCommittableOffset(record))
Expand All @@ -54,8 +54,8 @@ private class DefaultKafkaReceiver<K, V>(private val settings: ReceiverSettings<

override fun receiveAutoAck(topicNames: Collection<String>): Flow<Flow<ConsumerRecord<K, V>>> =
scopedConsumer(settings.groupId) { scope, dispatcher, consumer ->
val loop = PollLoop(topicNames, settings, consumer, scope)
loop.receive().flowOn(dispatcher).map { records ->
val loop = PollLoop(topicNames, settings, consumer, scope, currentCoroutineContext())
loop.receive().map { records ->
records.asFlow()
.onCompletion { records.forEach { loop.toCommittableOffset(it).acknowledge() } }
}
Expand Down Expand Up @@ -84,14 +84,15 @@ private class DefaultKafkaReceiver<K, V>(private val settings: ReceiverSettings<
@OptIn(ExperimentalCoroutinesApi::class)
fun <A> scopedConsumer(
groupId: String,
block: (CoroutineScope, ExecutorCoroutineDispatcher, KafkaConsumer<K, V>) -> Flow<A>
block: suspend (CoroutineScope, ExecutorCoroutineDispatcher, KafkaConsumer<K, V>) -> Flow<A>
): Flow<A> = flow {
kafkaConsumerDispatcher(groupId).use { dispatcher: ExecutorCoroutineDispatcher ->
val job = Job()
val scope = CoroutineScope(job + dispatcher + defaultCoroutineExceptionHandler)
try {
KafkaConsumer(settings.toProperties(), settings.keyDeserializer, settings.valueDeserializer)
.use { emit(block(scope, dispatcher, it)) }
withContext(dispatcher) {
KafkaConsumer(settings.toProperties(), settings.keyDeserializer, settings.valueDeserializer)
}.use { emit(block(scope, dispatcher, it)) }
} finally {
job.cancelAndJoin()
}
Expand Down
Loading