@@ -106,8 +106,7 @@ type partitionProducer struct {
106
106
107
107
// Channel where app is posting messages to be published
108
108
dataChan chan * sendRequest
109
- cmdChan chan interface {}
110
- connectClosedCh chan * connectionClosed
109
+ eventChan chan func ()
111
110
publishSemaphore internal.Semaphore
112
111
pendingQueue internal.BlockingQueue
113
112
lastSequenceID int64
@@ -167,8 +166,7 @@ func newPartitionProducer(client *client, topic string, options *ProducerOptions
167
166
options : options ,
168
167
producerID : client .rpcClient .NewProducerID (),
169
168
dataChan : make (chan * sendRequest , maxPendingMessages ),
170
- cmdChan : make (chan interface {}, 10 ),
171
- connectClosedCh : make (chan * connectionClosed , 10 ),
169
+ eventChan : make (chan func ()),
172
170
batchFlushTicker : time .NewTicker (batchingMaxPublishDelay ),
173
171
compressionProvider : internal .GetCompressionProvider (pb .CompressionType (options .CompressionType ),
174
172
compression .Level (options .CompressionLevel )),
@@ -294,6 +292,7 @@ func (p *partitionProducer) grabCnx(assignedBrokerURL string) error {
294
292
295
293
res , err := p .client .rpcClient .Request (lr .LogicalAddr , lr .PhysicalAddr , id , pb .BaseCommand_PRODUCER , cmdProducer )
296
294
if err != nil {
295
+ cnx .UnregisterListener (p .producerID )
297
296
p .log .WithError (err ).Error ("Failed to create producer at send PRODUCER request" )
298
297
if errors .Is (err , internal .ErrRequestTimeOut ) {
299
298
id := p .client .rpcClient .NewRequestID ()
@@ -408,9 +407,10 @@ func (p *partitionProducer) ConnectionClosed(closeProducer *pb.CommandCloseProdu
408
407
assignedBrokerURL = p .client .selectServiceURL (
409
408
closeProducer .GetAssignedBrokerServiceUrl (), closeProducer .GetAssignedBrokerServiceUrlTls ())
410
409
}
411
- p .connectClosedCh <- & connectionClosed {
410
+ p .log .Info ("runEventsLoop will reconnect in producer" )
411
+ p .reconnectToBroker (& connectionClosed {
412
412
assignedBrokerURL : assignedBrokerURL ,
413
- }
413
+ })
414
414
}
415
415
416
416
func (p * partitionProducer ) SetRedirectedClusterURI (redirectedClusterURI string ) {
@@ -545,27 +545,22 @@ func (p *partitionProducer) runEventsLoop() {
545
545
return
546
546
}
547
547
p .internalSend (data )
548
- case cmd , ok := <- p .cmdChan :
549
- // when doClose() is call, p.dataChan will be closed, cmd will be nil
548
+ case event , ok := <- p .eventChan :
549
+ // when doClose() is call, p.eventChan will be closed, cmd will be nil
550
550
if ! ok {
551
551
return
552
552
}
553
- switch v := cmd .(type ) {
554
- case * flushRequest :
555
- p .internalFlush (v )
556
- case * closeProducer :
557
- p .internalClose (v )
558
- return
559
- }
560
- case connectionClosed := <- p .connectClosedCh :
561
- p .log .Info ("runEventsLoop will reconnect in producer" )
562
- p .reconnectToBroker (connectionClosed )
553
+ event ()
563
554
case <- p .batchFlushTicker .C :
564
555
p .internalFlushCurrentBatch ()
565
556
}
566
557
}
567
558
}
568
559
560
+ func (p * partitionProducer ) execute (fn func ()) {
561
+ p .eventChan <- fn
562
+ }
563
+
569
564
func (p * partitionProducer ) Topic () string {
570
565
return p .topic
571
566
}
@@ -1399,7 +1394,7 @@ func (p *partitionProducer) doClose(reason error) {
1399
1394
1400
1395
p .log .Info ("Closing producer" )
1401
1396
defer close (p .dataChan )
1402
- defer close (p .cmdChan )
1397
+ defer close (p .eventChan )
1403
1398
1404
1399
id := p .client .rpcClient .NewRequestID ()
1405
1400
_ , err := p .client .rpcClient .RequestOnCnx (p ._getConn (), id , pb .BaseCommand_CLOSE_PRODUCER , & pb.CommandCloseProducer {
@@ -1481,7 +1476,10 @@ func (p *partitionProducer) FlushWithCtx(ctx context.Context) error {
1481
1476
select {
1482
1477
case <- ctx .Done ():
1483
1478
return ctx .Err ()
1484
- case p .cmdChan <- flushReq :
1479
+ default :
1480
+ p .execute (func () {
1481
+ p .internalFlush (flushReq )
1482
+ })
1485
1483
}
1486
1484
1487
1485
// wait for the flush request to complete
@@ -1514,7 +1512,9 @@ func (p *partitionProducer) Close() {
1514
1512
}
1515
1513
1516
1514
cp := & closeProducer {doneCh : make (chan struct {})}
1517
- p .cmdChan <- cp
1515
+ p .execute (func () {
1516
+ p .internalClose (cp )
1517
+ })
1518
1518
1519
1519
// wait for close producer request to complete
1520
1520
<- cp .doneCh
0 commit comments