@@ -210,7 +210,14 @@ public final class KafkaConsumer {
210210 source. finish ( )
211211 throw error
212212 }
213- try await Task . sleep ( for: self . config. pollInterval)
213+ // We support no back pressure, we can ignore the yield result
214+ _ = source. yield ( messageResult)
215+ try await Task . sleep ( for: self . pollInterval)
216+ case . pollUntilClosed( let client) :
217+ // Ignore poll result, we are closing down and just polling to commit
218+ // outstanding consumer state
219+ _ = try client. consumerPoll ( )
220+ try await Task . sleep ( for: self . pollInterval)
214221 case . terminatePollLoop:
215222 return
216223 }
@@ -304,7 +311,9 @@ extension KafkaConsumer {
304311 source: Producer . Source
305312 )
306313 /// The ``KafkaConsumer`` has been closed.
307- case finished
314+ ///
315+ /// - Parameter client: Client used for handling the connection to the Kafka cluster.
316+ case finished( client: KafkaClient )
308317 }
309318
310319 /// The current state of the StateMachine.
@@ -335,6 +344,11 @@ extension KafkaConsumer {
335344 client: KafkaClient ,
336345 source: Producer . Source
337346 )
347+ /// The ``KafkaConsumer`` is in the process of closing down, but still needs to poll
348+ /// to commit its state to the broker.
349+ ///
350+ /// - Parameter client: Client used for handling the connection to the Kafka cluster.
351+ case pollUntilClosed( client: KafkaClient )
338352 /// Terminate the poll loop.
339353 case terminatePollLoop
340354 }
@@ -351,8 +365,12 @@ extension KafkaConsumer {
351365 fatalError ( " Subscribe to consumer group / assign to topic partition pair before reading messages " )
352366 case . consuming( let client, let source) :
353367 return . pollForAndYieldMessage( client: client, source: source)
354- case . finished:
355- return . terminatePollLoop
368+ case . finished( let client) :
369+ if client. isConsumerClosed {
370+ return . terminatePollLoop
371+ } else {
372+ return . pollUntilClosed( client: client)
373+ }
356374 }
357375 }
358376
0 commit comments