@@ -69,7 +69,7 @@ func NewKafkaPublisher(cfg *config.KafkaConfig) (*KafkaPublisher, error) {
6969 kgo .TransactionalID (fmt .Sprintf ("insight-producer-%s" , chainID )),
7070 kgo .MaxBufferedBytes (2 * 1024 * 1024 * 1024 ), // 2GB
7171 kgo .MaxBufferedRecords (1_000_000 ),
72- kgo .ProducerBatchMaxBytes (16_000_000 ),
72+ kgo .ProducerBatchMaxBytes (100 * 1024 * 1024 ), // 100MB
7373 kgo .RecordPartitioner (kgo .ManualPartitioner ()),
7474 kgo .ProduceRequestTimeout (30 * time .Second ),
7575 kgo .MetadataMaxAge (60 * time .Second ),
@@ -161,9 +161,23 @@ func (p *KafkaPublisher) publishMessages(ctx context.Context, messages []*kgo.Re
161161 return fmt .Errorf ("failed to begin transaction: %v" , err )
162162 }
163163
164+ // Track if any produce errors occur
165+ var produceErrors []error
166+ var produceErrorsMu sync.Mutex
167+ var wg sync.WaitGroup
168+
164169 // Produce all messages in the transaction
165170 for _ , msg := range messages {
166- p .client .Produce (ctx , msg , nil )
171+ wg .Add (1 )
172+ p .client .Produce (ctx , msg , func (r * kgo.Record , err error ) {
173+ defer wg .Done ()
174+ if err != nil {
175+ log .Error ().Err (err ).Any ("headers" , r .Headers ).Msg ("KAFKA PUBLISHER::publishMessages::err" )
176+ produceErrorsMu .Lock ()
177+ produceErrors = append (produceErrors , err )
178+ produceErrorsMu .Unlock ()
179+ }
180+ })
167181 }
168182
169183 // Flush all messages
@@ -172,6 +186,18 @@ func (p *KafkaPublisher) publishMessages(ctx context.Context, messages []*kgo.Re
172186 return fmt .Errorf ("failed to flush messages: %v" , err )
173187 }
174188
189+ // Wait for all callbacks to complete
190+ wg .Wait ()
191+
192+ // Check if any produce errors occurred
193+ hasErrors := len (produceErrors ) > 0
194+
195+ if hasErrors {
196+ // Abort the transaction if any produce errors occurred
197+ p .client .EndTransaction (ctx , kgo .TryAbort )
198+ return fmt .Errorf ("transaction aborted due to produce errors: %v" , produceErrors )
199+ }
200+
175201 // Commit the transaction
176202 if err := p .client .EndTransaction (ctx , kgo .TryCommit ); err != nil {
177203 return fmt .Errorf ("failed to commit transaction: %v" , err )
0 commit comments