@@ -635,7 +635,10 @@ struct SinkBase {
635
635
// Write flags used to control the data to be sent in `buf`.
636
636
buf_flags : Option < WriteFlags > ,
637
637
// Used to records whether there is a message in which `buffer_hint` is false.
638
- buf_buffer_hint : bool ,
638
+ last_buffer_hint : bool ,
639
+ // Flag to indicate if enable batch. This behavior will modify the `buffer_hint` to batch messages
640
+ // as much as possible.
641
+ enable_batch : bool ,
639
642
send_metadata : bool ,
640
643
}
641
644
@@ -645,8 +648,9 @@ impl SinkBase {
645
648
batch_f : None ,
646
649
buf : Vec :: new ( ) ,
647
650
buf_flags : None ,
648
- buf_buffer_hint : true ,
651
+ last_buffer_hint : true ,
649
652
send_metadata,
653
+ enable_batch : false ,
650
654
}
651
655
}
652
656
@@ -666,16 +670,23 @@ impl SinkBase {
666
670
self . send_metadata = false ;
667
671
return Ok ( ( ) ) ;
668
672
}
673
+
669
674
// If there is already a buffered message waiting to be sent, set `buffer_hint` to true to indicate
670
675
// that this is not the last message.
671
- if self . buf_flags . is_some ( ) {
672
- // `start_send` is supposed to be called after `poll_ready` returns ready.
673
- assert ! ( self . batch_f. is_none( ) ) ;
676
+ if !self . buf . is_empty ( ) {
674
677
self . start_send_buffer_message ( true , call) ?;
675
678
}
679
+
676
680
ser ( t, & mut self . buf ) ;
677
- self . buf_buffer_hint &= flags. get_buffer_hint ( ) ;
681
+ let hint = flags. get_buffer_hint ( ) ;
682
+ self . last_buffer_hint &= hint;
678
683
self . buf_flags = Some ( flags) ;
684
+
685
+ // If sink disable batch, start sending the message in buffer immediately.
686
+ if !self . enable_batch {
687
+ self . start_send_buffer_message ( hint, call) ?;
688
+ }
689
+
679
690
Ok ( ( ) )
680
691
}
681
692
@@ -700,8 +711,8 @@ impl SinkBase {
700
711
if self . batch_f . is_some ( ) {
701
712
ready ! ( self . poll_ready( cx) ?) ;
702
713
}
703
- if self . buf_flags . is_some ( ) {
704
- self . start_send_buffer_message ( self . buf_buffer_hint , call) ?;
714
+ if ! self . buf . is_empty ( ) {
715
+ self . start_send_buffer_message ( self . last_buffer_hint , call) ?;
705
716
ready ! ( self . poll_ready( cx) ?) ;
706
717
}
707
718
Poll :: Ready ( Ok ( ( ) ) )
@@ -713,6 +724,9 @@ impl SinkBase {
713
724
buffer_hint : bool ,
714
725
call : & mut C ,
715
726
) -> Result < ( ) > {
727
+ // `start_send` is supposed to be called after `poll_ready` returns ready.
728
+ assert ! ( self . batch_f. is_none( ) ) ;
729
+
716
730
let mut flags = self . buf_flags . clone ( ) . unwrap ( ) ;
717
731
flags = flags. buffer_hint ( buffer_hint) ;
718
732
let write_f = call. call ( |c| {
0 commit comments