@@ -260,29 +260,12 @@ async def consume(self) -> None:
260260 # The topic is possibly low traffic, or the producer may be
261261 # slow or having an issue. No need to sleep here to avoid a hot
262262 # CPU loop, the consume call will delay this particular task.
263- self .consumer_logger .info (
264- "no more messages"
265- ) # (TODO: Debugging - Remove)
266263 continue
267264
268- # apply (optional) user derived filtering, which allows dropping messages
269- # based on kafka headers etc. prior to parsing the full payload. These
270- # filters skip messages, retaining those that are 'applicable' for further
271- # processing. Filters are provided on a `per-topic` basis and regex is not
272- # currently supported (yet).
273- # TODO: Abstract into _filter_msgs func
274- applicable_messages = messages if not self .filter_funcs else []
275- if self .filter_funcs :
276- for message in messages :
277- topic = message .topic ()
278- awaitables = self .filter_funcs .get (topic )
279- if not awaitables :
280- # there are registered 'filters' for this topic
281- exclude = await discard_message (topic , message , awaitables )
282- if not exclude :
283- applicable_messages .append (exclude )
284- else :
285- applicable_messages .append (message )
265+ # apply filters to the fetched messages, discarded ones which are not 'in-scope'.
266+ # retain order of messages to not break partition ordering guarantees.
267+ # Note: filters are based on (topic, message) combinations and registered as such.
268+ applicable_messages = await self ._apply_filters (messages )
286269
287270 if not applicable_messages :
288271 # the entire batch was 'filtered' out by the user.
@@ -393,6 +376,33 @@ async def _subscribe(self) -> None:
393376 self ._log_kafka_exception (exc )
394377 raise
395378
379+ async def _apply_filters (self , messages : list [Message ]) -> list [Message ]:
380+ """_apply_filters applies topic-level filtering to all the messages
381+ in the batch, returning a list[Message] objects that have 'passed' the
382+ filtering and should be processed.
383+
384+ Messages skipped by discard_message calls will be stored/committed
385+ as part of the batch processing.
386+
387+ :param messages: The list of messages polled from the broker.
388+
389+ :return: The list of applicable messages (retaining order to guarantee partition
390+ ordering guarantees)
391+ """
392+ applicable_messages = messages if not self .filter_funcs else []
393+ if self .filter_funcs :
394+ for message in messages :
395+ topic = message .topic ()
396+ filter_awaitables = self .filter_funcs .get (topic )
397+ if not filter_awaitables :
398+ # there have been 'filter' functions applied to this particular topic.
399+ exclude = await discard_message (topic , message , filter_awaitables )
400+ if not exclude :
401+ applicable_messages .append (message )
402+ else :
403+ applicable_messages .append (message )
404+ return applicable_messages
405+
396406 def _log_kafka_exception (self , exc : KafkaException ) -> None :
397407 """_log_kafka_error unwraps a KafkaException and logs information about the
398408 underlying KafkaError.
0 commit comments