@@ -11,7 +11,7 @@ use log::{error, warn};
1111use rdkafka_sys as rdsys;
1212use rdkafka_sys:: types:: * ;
1313
14- use crate :: client:: { Client , NativeClient , NativeQueue } ;
14+ use crate :: client:: { Client , EventPollResult , NativeClient , NativeQueue } ;
1515use crate :: config:: {
1616 ClientConfig , FromClientConfig , FromClientConfigAndContext , NativeClientConfig ,
1717} ;
@@ -117,59 +117,69 @@ where
117117 ///
118118 /// The returned message lives in the memory of the consumer and cannot outlive it.
119119 pub fn poll < T : Into < Timeout > > ( & self , timeout : T ) -> Option < KafkaResult < BorrowedMessage < ' _ > > > {
120- self . poll_queue ( self . get_queue ( ) , timeout)
120+ self . poll_queue ( self . get_queue ( ) , timeout) . into ( )
121121 }
122122
123123 pub ( crate ) fn poll_queue < T : Into < Timeout > > (
124124 & self ,
125125 queue : & NativeQueue ,
126126 timeout : T ,
127- ) -> Option < KafkaResult < BorrowedMessage < ' _ > > > {
127+ ) -> EventPollResult < KafkaResult < BorrowedMessage < ' _ > > > {
128128 let now = Instant :: now ( ) ;
129- let mut timeout = timeout. into ( ) ;
129+ let initial_timeout = timeout. into ( ) ;
130+ let mut timeout = initial_timeout;
130131 let min_poll_interval = self . context ( ) . main_queue_min_poll_interval ( ) ;
131132 loop {
132133 let op_timeout = std:: cmp:: min ( timeout, min_poll_interval) ;
133134 let maybe_event = self . client ( ) . poll_event ( queue, op_timeout) ;
134- if let Some ( event) = maybe_event {
135- let evtype = unsafe { rdsys:: rd_kafka_event_type ( event. ptr ( ) ) } ;
136- match evtype {
137- rdsys:: RD_KAFKA_EVENT_FETCH => {
138- if let Some ( result) = self . handle_fetch_event ( event) {
139- return Some ( result) ;
135+ match maybe_event {
136+ EventPollResult :: Event ( event) => {
137+ let evtype = unsafe { rdsys:: rd_kafka_event_type ( event. ptr ( ) ) } ;
138+ match evtype {
139+ rdsys:: RD_KAFKA_EVENT_FETCH => {
140+ if let Some ( result) = self . handle_fetch_event ( event) {
141+ return EventPollResult :: Event ( result) ;
142+ }
140143 }
141- }
142- rdsys :: RD_KAFKA_EVENT_ERROR => {
143- if let Some ( err ) = self . handle_error_event ( event ) {
144- return Some ( Err ( err ) ) ;
144+ rdsys :: RD_KAFKA_EVENT_ERROR => {
145+ if let Some ( err ) = self . handle_error_event ( event ) {
146+ return EventPollResult :: Event ( Err ( err ) ) ;
147+ }
145148 }
146- }
147- rdsys :: RD_KAFKA_EVENT_REBALANCE => {
148- self . handle_rebalance_event ( event ) ;
149- if timeout != Timeout :: Never {
150- return None ;
149+ rdsys :: RD_KAFKA_EVENT_REBALANCE => {
150+ self . handle_rebalance_event ( event ) ;
151+ if timeout != Timeout :: Never {
152+ return EventPollResult :: EventConsumed ;
153+ }
151154 }
152- }
153- rdsys:: RD_KAFKA_EVENT_OFFSET_COMMIT => {
154- self . handle_offset_commit_event ( event) ;
155- if timeout != Timeout :: Never {
156- return None ;
155+ rdsys:: RD_KAFKA_EVENT_OFFSET_COMMIT => {
156+ self . handle_offset_commit_event ( event) ;
157+ if timeout != Timeout :: Never {
158+ return EventPollResult :: EventConsumed ;
159+ }
160+ }
161+ _ => {
162+ let evname = unsafe {
163+ let evname = rdsys:: rd_kafka_event_name ( event. ptr ( ) ) ;
164+ CStr :: from_ptr ( evname) . to_string_lossy ( )
165+ } ;
166+ warn ! ( "Ignored event '{evname}' on consumer poll" ) ;
157167 }
158168 }
159- _ => {
160- let evname = unsafe {
161- let evname = rdsys:: rd_kafka_event_name ( event. ptr ( ) ) ;
162- CStr :: from_ptr ( evname) . to_string_lossy ( )
163- } ;
164- warn ! ( "Ignored event '{evname}' on consumer poll" ) ;
169+ }
170+ EventPollResult :: None => {
171+ timeout = initial_timeout. saturating_sub ( now. elapsed ( ) ) ;
172+ if timeout. is_zero ( ) {
173+ return EventPollResult :: None ;
165174 }
166175 }
167- }
168-
169- timeout = timeout. saturating_sub ( now. elapsed ( ) ) ;
170- if timeout. is_zero ( ) {
171- return None ;
172- }
176+ EventPollResult :: EventConsumed => {
177+ timeout = initial_timeout. saturating_sub ( now. elapsed ( ) ) ;
178+ if timeout. is_zero ( ) {
179+ return EventPollResult :: EventConsumed ;
180+ }
181+ }
182+ } ;
173183 }
174184 }
175185
@@ -836,7 +846,7 @@ where
836846 /// associated consumer regularly, even if no messages are expected, to
837847 /// serve events.
838848 pub fn poll < T : Into < Timeout > > ( & self , timeout : T ) -> Option < KafkaResult < BorrowedMessage < ' _ > > > {
839- self . consumer . poll_queue ( & self . queue , timeout)
849+ self . consumer . poll_queue ( & self . queue , timeout) . into ( )
840850 }
841851
842852 /// Sets a callback that will be invoked whenever the queue becomes
0 commit comments