@@ -127,8 +127,8 @@ type Writer struct {
127127 BatchBytes int64
128128
129129 // Setting this flag to true causes the WriteMessages starts to derive 'BatchBytes'
130- // from topic 'max.message.size ' setting. If writer is used to write to multiple
131- // topics each topic 'max.message.size ' will be handled appropriately.
130+ // from topic 'max.message.bytes ' setting. If writer is used to write to multiple
131+ // topics each topic 'max.message.bytes ' will be handled appropriately.
132132 // This option simplifies maintaining of architecture - creates the one source of
133133 // truth - topic settings on broker side
134134 //
@@ -138,7 +138,7 @@ type Writer struct {
138138 // Setting this flag to true causes the WriteMessages starts to apply 'BatchBytes'
139139 // as limiting factor after compression stage.
140140 // When this flag is false - it's possible to get case, when Value can exceed
141- // 'max.message.size ' setting, but after compression it's less.
141+ // 'max.message.bytes ' setting, but after compression it's less.
142142 // And WriteMessages returns an error, when indeed there are no error.
143143 //
144144 // Nevertheless, 'BatchBytes' also has second function - to form batches, and
@@ -647,15 +647,14 @@ func (w *Writer) WriteMessages(ctx context.Context, msgs ...Message) error {
647647 return nil
648648 }
649649
650+ balancer := w .balancer ()
650651 if w .AutoDeriveBatchBytes {
651652 err := w .deriveBatchBytes (msgs )
652653 if err != nil {
653654 return err
654655 }
655656 }
656657
657- balancer := w .balancer ()
658-
659658 if ! w .ApplyBatchBytesAfterCompression {
660659 for i := range msgs {
661660 n := int64 (msgs [i ].totalSize ())
@@ -766,7 +765,9 @@ func (w *Writer) produce(key topicPartition, batch *writeBatch) (*ProduceRespons
766765 defer cancel ()
767766
768767 client := w .client (timeout )
769- client .MaxMessageSize = w .batchBytes (key .topic )
768+ if w .ApplyBatchBytesAfterCompression {
769+ client .MaxMessageBytes = w .batchBytes (key .topic )
770+ }
770771
771772 return client .Produce (ctx , & ProduceRequest {
772773 Partition : int (key .partition ),
@@ -860,7 +861,10 @@ func (w *Writer) deriveBatchBytes(msgs []Message) error {
860861 return err
861862 }
862863
863- if _ , ok := w .maxMessageBytesPerTopic .Load (topic ); ok {
864+ if res , ok := w .maxMessageBytesPerTopic .Load (topic ); ok {
865+ // DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
866+ fmt .Println ("Writer::deriveBatchBytes::LOAD" , "topic" , topic , "res" , res )
867+ // DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
864868 continue
865869 }
866870
@@ -887,6 +891,9 @@ func (w *Writer) deriveBatchBytes(msgs []Message) error {
887891 if err != nil {
888892 return err
889893 }
894+ // DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
895+ fmt .Println ("Writer::deriveBatchBytes::STORE" , "topic" , topic , "res" , maxMessageBytesStr )
896+ // DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
890897 w .maxMessageBytesPerTopic .Store (topic , int64 (maxMessageBytes ))
891898 }
892899 return nil
@@ -897,16 +904,15 @@ func (w *Writer) batchBytes(topic string) int64 {
897904 if result , ok := w .maxMessageBytesPerTopic .Load (topic ); ok {
898905 return result .(int64 )
899906 }
900-
901907 // batchBytes expects it's called after 'deriveBatchBytes(msgs)'
902908 // It means, there are no unknown topics
903909 panic (fmt .Sprintf ("unknown topic: %s" , topic ))
904- } else {
905- if w .BatchBytes > 0 {
906- return w .BatchBytes
907- }
908- return 1048576
909910 }
911+
912+ if w .BatchBytes > 0 {
913+ return w .BatchBytes
914+ }
915+ return 1048576
910916}
911917
912918func (w * Writer ) batchTimeout () time.Duration {
0 commit comments