@@ -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,12 @@ 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 {
412
- assignedBrokerURL : assignedBrokerURL ,
413
- }
410
+ p .execute (func () {
411
+ p .log .Info ("runEventsLoop will reconnect in producer" )
412
+ p .reconnectToBroker (& connectionClosed {
413
+ assignedBrokerURL : assignedBrokerURL ,
414
+ })
415
+ })
414
416
}
415
417
416
418
func (p * partitionProducer ) SetRedirectedClusterURI (redirectedClusterURI string ) {
@@ -545,27 +547,22 @@ func (p *partitionProducer) runEventsLoop() {
545
547
return
546
548
}
547
549
p .internalSend (data )
548
- case cmd , ok := <- p .cmdChan :
549
- // when doClose() is call, p.dataChan will be closed, cmd will be nil
550
+ case event , ok := <- p .eventChan :
551
+ // when doClose() is call, p.eventChan will be closed, cmd will be nil
550
552
if ! ok {
551
553
return
552
554
}
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 )
555
+ event ()
563
556
case <- p .batchFlushTicker .C :
564
557
p .internalFlushCurrentBatch ()
565
558
}
566
559
}
567
560
}
568
561
562
+ func (p * partitionProducer ) execute (fn func ()) {
563
+ p .eventChan <- fn
564
+ }
565
+
569
566
func (p * partitionProducer ) Topic () string {
570
567
return p .topic
571
568
}
@@ -1399,7 +1396,7 @@ func (p *partitionProducer) doClose(reason error) {
1399
1396
1400
1397
p .log .Info ("Closing producer" )
1401
1398
defer close (p .dataChan )
1402
- defer close (p .cmdChan )
1399
+ defer close (p .eventChan )
1403
1400
1404
1401
id := p .client .rpcClient .NewRequestID ()
1405
1402
_ , err := p .client .rpcClient .RequestOnCnx (p ._getConn (), id , pb .BaseCommand_CLOSE_PRODUCER , & pb.CommandCloseProducer {
@@ -1481,7 +1478,10 @@ func (p *partitionProducer) FlushWithCtx(ctx context.Context) error {
1481
1478
select {
1482
1479
case <- ctx .Done ():
1483
1480
return ctx .Err ()
1484
- case p .cmdChan <- flushReq :
1481
+ default :
1482
+ p .execute (func () {
1483
+ p .internalFlush (flushReq )
1484
+ })
1485
1485
}
1486
1486
1487
1487
// wait for the flush request to complete
@@ -1514,7 +1514,9 @@ func (p *partitionProducer) Close() {
1514
1514
}
1515
1515
1516
1516
cp := & closeProducer {doneCh : make (chan struct {})}
1517
- p .cmdChan <- cp
1517
+ p .execute (func () {
1518
+ p .internalClose (cp )
1519
+ })
1518
1520
1519
1521
// wait for close producer request to complete
1520
1522
<- cp .doneCh
0 commit comments