1
1
//! High-level consumers with a [`Stream`](futures::Stream) interface.
2
2
3
+ use std:: ffi:: CString ;
3
4
use std:: marker:: PhantomData ;
4
5
use std:: pin:: Pin ;
6
+ use std:: ptr;
5
7
use std:: sync:: { Arc , Mutex } ;
6
8
use std:: task:: { Context , Poll , Waker } ;
7
9
use std:: time:: Duration ;
@@ -16,9 +18,9 @@ use slab::Slab;
16
18
use rdkafka_sys as rdsys;
17
19
use rdkafka_sys:: types:: * ;
18
20
19
- use crate :: client:: { Client , ClientContext , NativeClient } ;
21
+ use crate :: client:: { Client , ClientContext , NativeClient , NativeQueue } ;
20
22
use crate :: config:: { ClientConfig , FromClientConfig , FromClientConfigAndContext , RDKafkaLogLevel } ;
21
- use crate :: consumer:: base_consumer:: { BaseConsumer , PartitionQueue } ;
23
+ use crate :: consumer:: base_consumer:: { enable_nonempty_callback , BaseConsumer , PartitionQueue } ;
22
24
use crate :: consumer:: {
23
25
CommitMode , Consumer , ConsumerContext , ConsumerGroupMetadata , DefaultConsumerContext , Rebalance ,
24
26
} ;
@@ -126,7 +128,7 @@ where
126
128
C : ConsumerContext + ' static ,
127
129
{
128
130
consumer : & ' a StreamConsumer < C , R > ,
129
- partition : Option < & ' a PartitionQueue < StreamConsumerContext < C > > > ,
131
+ partition : Option < & ' a PartitionQueue < StreamConsumerContext < C > , StreamConsumer < C , R > > > ,
130
132
slot : usize ,
131
133
}
132
134
@@ -149,7 +151,7 @@ where
149
151
150
152
fn new_partition (
151
153
consumer : & ' a StreamConsumer < C , R > ,
152
- partition : & ' a PartitionQueue < StreamConsumerContext < C > > ,
154
+ partition : & ' a PartitionQueue < StreamConsumerContext < C > , StreamConsumer < C , R > > ,
153
155
) -> MessageStream < ' a , C , R > {
154
156
let slot = {
155
157
let context = consumer. base . context ( ) ;
@@ -250,7 +252,7 @@ pub struct StreamConsumer<C = DefaultConsumerContext, R = DefaultRuntime>
250
252
where
251
253
C : ConsumerContext ,
252
254
{
253
- base : Arc < BaseConsumer < StreamConsumerContext < C > > > ,
255
+ base : BaseConsumer < StreamConsumerContext < C > > ,
254
256
_shutdown_trigger : oneshot:: Sender < ( ) > ,
255
257
_runtime : PhantomData < R > ,
256
258
}
@@ -313,7 +315,7 @@ where
313
315
} ) ;
314
316
315
317
Ok ( StreamConsumer {
316
- base : Arc :: new ( base ) ,
318
+ base,
317
319
_shutdown_trigger : shutdown_trigger,
318
320
_runtime : PhantomData ,
319
321
} )
@@ -385,8 +387,25 @@ where
385
387
topic : & str ,
386
388
partition : i32 ,
387
389
) -> Option < PartitionStream < C , R > > {
388
- self . base
389
- . split_partition_queue ( topic, partition)
390
+ let topic = match CString :: new ( topic) {
391
+ Ok ( topic) => topic,
392
+ Err ( _) => return None ,
393
+ } ;
394
+ let queue = unsafe {
395
+ NativeQueue :: from_ptr ( rdsys:: rd_kafka_queue_get_partition (
396
+ self . base . client ( ) . native_ptr ( ) ,
397
+ topic. as_ptr ( ) ,
398
+ partition,
399
+ ) )
400
+ } ;
401
+ queue
402
+ . map ( |queue| {
403
+ unsafe {
404
+ enable_nonempty_callback ( & queue, self . base . client ( ) . context ( ) ) ;
405
+ rdsys:: rd_kafka_queue_forward ( queue. ptr ( ) , ptr:: null_mut ( ) ) ;
406
+ }
407
+ PartitionQueue :: new ( self . clone ( ) , queue)
408
+ } )
390
409
. map ( |partition| PartitionStream :: new ( self , partition) )
391
410
}
392
411
}
@@ -548,7 +567,7 @@ where
548
567
C : ConsumerContext ,
549
568
{
550
569
consumer : Arc < StreamConsumer < C , R > > ,
551
- queue : PartitionQueue < StreamConsumerContext < C > > ,
570
+ queue : PartitionQueue < StreamConsumerContext < C > , StreamConsumer < C , R > > ,
552
571
}
553
572
554
573
impl < C , R > PartitionStream < C , R >
@@ -557,7 +576,7 @@ where
557
576
{
558
577
fn new (
559
578
consumer : & Arc < StreamConsumer < C , R > > ,
560
- queue : PartitionQueue < StreamConsumerContext < C > > ,
579
+ queue : PartitionQueue < StreamConsumerContext < C > , StreamConsumer < C , R > > ,
561
580
) -> Self {
562
581
PartitionStream {
563
582
consumer : consumer. clone ( ) ,
0 commit comments