@@ -110,7 +110,7 @@ type partitionProducer struct {
110110 compressionProvider compression.Provider
111111
112112 // Channel where app is posting messages to be published
113- dataChan chan * sendRequest
113+ dataChan chan * pendingItem
114114 cmdChan chan interface {}
115115 connectClosedCh chan * connectionClosed
116116 publishSemaphore internal.Semaphore
@@ -177,16 +177,15 @@ func newPartitionProducer(client *client, topic string, options *ProducerOptions
177177
178178 ctx , cancelFunc := context .WithCancel (context .Background ())
179179 p := & partitionProducer {
180- client : client ,
181- topic : topic ,
182- log : logger ,
183- cnxKeySuffix : client .cnxPool .GenerateRoundRobinIndex (),
184- options : options ,
185- producerID : client .rpcClient .NewProducerID (),
186- dataChan : make (chan * sendRequest , maxPendingMessages ),
187- cmdChan : make (chan interface {}, 10 ),
188- connectClosedCh : make (chan * connectionClosed , 1 ),
189- batchFlushTicker : time .NewTicker (batchingMaxPublishDelay ),
180+ client : client ,
181+ topic : topic ,
182+ log : logger ,
183+ cnxKeySuffix : client .cnxPool .GenerateRoundRobinIndex (),
184+ options : options ,
185+ producerID : client .rpcClient .NewProducerID (),
186+ dataChan : make (chan * pendingItem , maxPendingMessages ),
187+ cmdChan : make (chan interface {}, 10 ),
188+ connectClosedCh : make (chan * connectionClosed , 1 ),
190189 compressionProvider : internal .GetCompressionProvider (pb .CompressionType (options .CompressionType ),
191190 compression .Level (options .CompressionLevel )),
192191 publishSemaphore : internal .NewSemaphore (int32 (maxPendingMessages )),
@@ -200,9 +199,6 @@ func newPartitionProducer(client *client, topic string, options *ProducerOptions
200199 cancelFunc : cancelFunc ,
201200 backOffPolicyFunc : boFunc ,
202201 }
203- if p .options .DisableBatching {
204- p .batchFlushTicker .Stop ()
205- }
206202 p .setProducerState (producerInit )
207203
208204 if options .Schema != nil && options .Schema .GetSchemaInfo () != nil {
@@ -219,7 +215,6 @@ func newPartitionProducer(client *client, topic string, options *ProducerOptions
219215 }
220216 err := p .grabCnx ("" )
221217 if err != nil {
222- p .batchFlushTicker .Stop ()
223218 logger .WithError (err ).Error ("Failed to create producer at newPartitionProducer" )
224219 return nil , err
225220 }
@@ -232,6 +227,11 @@ func newPartitionProducer(client *client, topic string, options *ProducerOptions
232227 p .log .WithField ("cnx" , p ._getConn ().ID ()).Info ("Created producer" )
233228 p .setProducerState (producerReady )
234229
230+ if ! p .options .DisableBatching {
231+ p .batchFlushTicker = time .NewTicker (batchingMaxPublishDelay )
232+ go p .listenBatch ()
233+ }
234+
235235 if p .options .SendTimeout > 0 {
236236 go p .failTimeoutMessages ()
237237 }
@@ -558,6 +558,15 @@ func (p *partitionProducer) reconnectToBroker(connectionClosed *connectionClosed
558558 })
559559}
560560
561+ func (p * partitionProducer ) listenBatch () {
562+ for {
563+ select {
564+ case <- p .batchFlushTicker .C :
565+ p .internalFlushCurrentBatch ()
566+ }
567+ }
568+ }
569+
561570func (p * partitionProducer ) runEventsLoop () {
562571 for {
563572 select {
@@ -566,7 +575,7 @@ func (p *partitionProducer) runEventsLoop() {
566575 if ! ok {
567576 return
568577 }
569- p .internalSend (data )
578+ p .internalWriteData (data )
570579 case cmd , ok := <- p .cmdChan :
571580 // when doClose() is call, p.dataChan will be closed, cmd will be nil
572581 if ! ok {
@@ -582,8 +591,6 @@ func (p *partitionProducer) runEventsLoop() {
582591 case connectionClosed := <- p .connectClosedCh :
583592 p .log .Info ("runEventsLoop will reconnect in producer" )
584593 p .reconnectToBroker (connectionClosed )
585- case <- p .batchFlushTicker .C :
586- p .internalFlushCurrentBatch ()
587594 }
588595 }
589596}
@@ -688,6 +695,10 @@ func (p *partitionProducer) internalSend(sr *sendRequest) {
688695 }
689696}
690697
698+ func (p * partitionProducer ) internalWriteData (item * pendingItem ) {
699+ p ._getConn ().WriteData (item .ctx , item .buffer )
700+ }
701+
691702func addRequestToBatch (smm * pb.SingleMessageMetadata , p * partitionProducer ,
692703 uncompressedPayload []byte ,
693704 request * sendRequest , msg * ProducerMessage , deliverAt time.Time ,
@@ -898,16 +909,17 @@ func (p *partitionProducer) writeData(buffer internal.Buffer, sequenceID uint64,
898909 default :
899910 now := time .Now ()
900911 ctx , cancel := context .WithCancel (context .Background ())
901- p . pendingQueue . Put ( & pendingItem {
912+ item := pendingItem {
902913 ctx : ctx ,
903914 cancel : cancel ,
904915 createdAt : now ,
905916 sentAt : now ,
906917 buffer : buffer ,
907918 sequenceID : sequenceID ,
908919 sendRequests : callbacks ,
909- })
910- p ._getConn ().WriteData (ctx , buffer )
920+ }
921+ p .pendingQueue .Put (& item )
922+ p .dataChan <- & item
911923 }
912924}
913925
@@ -1077,7 +1089,7 @@ func (p *partitionProducer) clearPendingSendRequests() {
10771089 for i := 0 ; i < sizeBeforeFlushing ; i ++ {
10781090 select {
10791091 case pendingData := <- p .dataChan :
1080- p .internalSend (pendingData )
1092+ p .internalWriteData (pendingData )
10811093
10821094 default :
10831095 return
@@ -1352,7 +1364,7 @@ func (p *partitionProducer) internalSendAsync(
13521364 return
13531365 }
13541366
1355- p .dataChan <- sr
1367+ p .internalSend ( sr )
13561368}
13571369
13581370func (p * partitionProducer ) ReceivedSendReceipt (response * pb.CommandSendReceipt ) {
@@ -1466,7 +1478,10 @@ func (p *partitionProducer) doClose(reason error) {
14661478
14671479 p .setProducerState (producerClosed )
14681480 p ._getConn ().UnregisterListener (p .producerID )
1469- p .batchFlushTicker .Stop ()
1481+
1482+ if p .batchFlushTicker != nil {
1483+ p .batchFlushTicker .Stop ()
1484+ }
14701485}
14711486
14721487func (p * partitionProducer ) failPendingMessages (err error ) {
0 commit comments