@@ -207,6 +207,8 @@ use crate::client::producer::aggregator::TryPush;
207
207
use crate :: client:: { error:: Error as ClientError , partition:: PartitionClient } ;
208
208
use crate :: record:: Record ;
209
209
210
+ use super :: partition:: Compression ;
211
+
210
212
pub mod aggregator;
211
213
mod broadcast;
212
214
@@ -230,6 +232,8 @@ pub struct BatchProducerBuilder {
230
232
client : Arc < dyn ProducerClient > ,
231
233
232
234
linger : Duration ,
235
+
236
+ compression : Compression ,
233
237
}
234
238
235
239
impl BatchProducerBuilder {
@@ -243,6 +247,7 @@ impl BatchProducerBuilder {
243
247
Self {
244
248
client,
245
249
linger : Duration :: from_millis ( 5 ) ,
250
+ compression : Compression :: default ( ) ,
246
251
}
247
252
}
248
253
@@ -251,12 +256,21 @@ impl BatchProducerBuilder {
251
256
Self { linger, ..self }
252
257
}
253
258
259
+ /// Sets compression.
260
+ pub fn with_compression ( self , compression : Compression ) -> Self {
261
+ Self {
262
+ compression,
263
+ ..self
264
+ }
265
+ }
266
+
254
267
pub fn build < A > ( self , aggregator : A ) -> BatchProducer < A >
255
268
where
256
269
A : aggregator:: Aggregator ,
257
270
{
258
271
BatchProducer {
259
272
linger : self . linger ,
273
+ compression : self . compression ,
260
274
client : self . client ,
261
275
inner : Mutex :: new ( ProducerInner {
262
276
aggregator,
@@ -268,12 +282,20 @@ impl BatchProducerBuilder {
268
282
269
283
// A trait wrapper to allow mocking
270
284
trait ProducerClient : std:: fmt:: Debug + Send + Sync {
271
- fn produce ( & self , records : Vec < Record > ) -> BoxFuture < ' _ , Result < Vec < i64 > , ClientError > > ;
285
+ fn produce (
286
+ & self ,
287
+ records : Vec < Record > ,
288
+ compression : Compression ,
289
+ ) -> BoxFuture < ' _ , Result < Vec < i64 > , ClientError > > ;
272
290
}
273
291
274
292
impl ProducerClient for PartitionClient {
275
- fn produce ( & self , records : Vec < Record > ) -> BoxFuture < ' _ , Result < Vec < i64 > , ClientError > > {
276
- Box :: pin ( self . produce ( records) )
293
+ fn produce (
294
+ & self ,
295
+ records : Vec < Record > ,
296
+ compression : Compression ,
297
+ ) -> BoxFuture < ' _ , Result < Vec < i64 > , ClientError > > {
298
+ Box :: pin ( self . produce ( records, compression) )
277
299
}
278
300
}
279
301
@@ -293,6 +315,8 @@ where
293
315
{
294
316
linger : Duration ,
295
317
318
+ compression : Compression ,
319
+
296
320
client : Arc < dyn ProducerClient > ,
297
321
298
322
inner : Mutex < ProducerInner < A > > ,
@@ -370,7 +394,7 @@ where
370
394
TryPush :: NoCapacity ( data) => {
371
395
debug ! ( "Insufficient capacity in aggregator - flushing" ) ;
372
396
373
- Self :: flush_impl ( & mut inner, self . client . as_ref ( ) ) . await ;
397
+ Self :: flush_impl ( & mut inner, self . client . as_ref ( ) , self . compression ) . await ;
374
398
match inner
375
399
. aggregator
376
400
. try_push ( data)
@@ -413,7 +437,7 @@ where
413
437
debug ! ( "Linger expired - flushing" ) ;
414
438
415
439
// Flush data
416
- Self :: flush_impl ( & mut inner, self . client . as_ref ( ) ) . await ;
440
+ Self :: flush_impl ( & mut inner, self . client . as_ref ( ) , self . compression ) . await ;
417
441
418
442
extract ( & result_slot. now_or_never ( ) . expect ( "just flushed" ) , tag)
419
443
}
@@ -422,12 +446,16 @@ where
422
446
pub async fn flush ( & self ) {
423
447
let mut inner = self . inner . lock ( ) . await ;
424
448
debug ! ( "Manual flush" ) ;
425
- Self :: flush_impl ( & mut inner, self . client . as_ref ( ) ) . await ;
449
+ Self :: flush_impl ( & mut inner, self . client . as_ref ( ) , self . compression ) . await ;
426
450
}
427
451
428
452
/// Flushes out the data from the aggregator, publishes the result to the result slot,
429
453
/// and creates a fresh result slot for future writes to use
430
- async fn flush_impl ( inner : & mut ProducerInner < A > , client : & dyn ProducerClient ) {
454
+ async fn flush_impl (
455
+ inner : & mut ProducerInner < A > ,
456
+ client : & dyn ProducerClient ,
457
+ compression : Compression ,
458
+ ) {
431
459
trace ! ( "Flushing batch producer" ) ;
432
460
433
461
let ( output, status_deagg) = match inner. aggregator . flush ( ) {
@@ -444,7 +472,7 @@ where
444
472
return ;
445
473
}
446
474
447
- let r = client. produce ( output) . await ;
475
+ let r = client. produce ( output, compression ) . await ;
448
476
449
477
// Reset result slot
450
478
let slot = std:: mem:: take ( & mut inner. result_slot ) ;
@@ -483,7 +511,11 @@ mod tests {
483
511
}
484
512
485
513
impl ProducerClient for MockClient {
486
- fn produce ( & self , records : Vec < Record > ) -> BoxFuture < ' _ , Result < Vec < i64 > , ClientError > > {
514
+ fn produce (
515
+ & self ,
516
+ records : Vec < Record > ,
517
+ _compression : Compression ,
518
+ ) -> BoxFuture < ' _ , Result < Vec < i64 > , ClientError > > {
487
519
Box :: pin ( async move {
488
520
tokio:: time:: sleep ( self . delay ) . await ;
489
521
0 commit comments