Skip to content

Commit

Permalink
[Issue 1272][connection] Attempt to avoid deadlock during reconnection (
Browse files Browse the repository at this point in the history
#1273)

Fixes #1272 

### Motivation

Producers and consumers register themselves to the connection to be notified when it gets closed. Even though the callback push the events in a channel, it can get stuck and the connection pool is locked which prevents any other caller to get a connection.

### Modifications

This PR makes sure that the callback never blocks.
  • Loading branch information
Gilthoniel authored Sep 11, 2024
1 parent 7ae0059 commit 98dc8d4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 7 additions & 3 deletions pulsar/consumer_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func newPartitionConsumer(parent Consumer, client *client, options *partitionCon
startMessageID: atomicMessageID{msgID: options.startMessageID},
connectedCh: make(chan struct{}),
messageCh: messageCh,
connectClosedCh: make(chan *connectionClosed, 10),
connectClosedCh: make(chan *connectionClosed, 1),
closeCh: make(chan struct{}),
clearQueueCh: make(chan func(id *trackingMessageID)),
compressionProviders: sync.Map{},
Expand Down Expand Up @@ -1381,8 +1381,12 @@ func (pc *partitionConsumer) ConnectionClosed(closeConsumer *pb.CommandCloseCons
assignedBrokerURL = pc.client.selectServiceURL(
closeConsumer.GetAssignedBrokerServiceUrl(), closeConsumer.GetAssignedBrokerServiceUrlTls())
}
pc.connectClosedCh <- &connectionClosed{
assignedBrokerURL: assignedBrokerURL,

select {
case pc.connectClosedCh <- &connectionClosed{assignedBrokerURL: assignedBrokerURL}:
default:
// Reconnect has already been requested so we do not block the
// connection callback.
}
}

Expand Down
10 changes: 7 additions & 3 deletions pulsar/producer_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func newPartitionProducer(client *client, topic string, options *ProducerOptions
producerID: client.rpcClient.NewProducerID(),
dataChan: make(chan *sendRequest, maxPendingMessages),
cmdChan: make(chan interface{}, 10),
connectClosedCh: make(chan *connectionClosed, 10),
connectClosedCh: make(chan *connectionClosed, 1),
batchFlushTicker: time.NewTicker(batchingMaxPublishDelay),
compressionProvider: internal.GetCompressionProvider(pb.CompressionType(options.CompressionType),
compression.Level(options.CompressionLevel)),
Expand Down Expand Up @@ -413,8 +413,12 @@ func (p *partitionProducer) ConnectionClosed(closeProducer *pb.CommandCloseProdu
assignedBrokerURL = p.client.selectServiceURL(
closeProducer.GetAssignedBrokerServiceUrl(), closeProducer.GetAssignedBrokerServiceUrlTls())
}
p.connectClosedCh <- &connectionClosed{
assignedBrokerURL: assignedBrokerURL,

select {
case p.connectClosedCh <- &connectionClosed{assignedBrokerURL: assignedBrokerURL}:
default:
// Reconnect has already been requested so we do not block the
// connection callback.
}
}

Expand Down

0 comments on commit 98dc8d4

Please sign in to comment.