Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue 1276] Fix multiple consumers using zeroQueueConsumer #1278

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pulsar/consumer_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,10 @@ func (pc *partitionConsumer) dispatcher() {
messages[0] = nil
messages = messages[1:]

pc.availablePermits.inc()
// for the zeroQueueConsumer, the permits controlled by itself
if pc.options.receiverQueueSize > 0 {
pc.availablePermits.inc()
}

if pc.options.autoReceiverQueueSize {
pc.incomingMessages.Dec()
Expand Down
83 changes: 83 additions & 0 deletions pulsar/consumer_zero_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,89 @@ func TestNormalZeroQueueConsumer(t *testing.T) {
err = consumer.Unsubscribe()
assert.Nil(t, err)
}

func TestMultipleConsumer(t *testing.T) {
client, err := NewClient(ClientOptions{
URL: lookupURL,
})
if err != nil {
log.Fatal(err)
}
defer client.Close()

topic := newTopicName()
ctx := context.Background()

// create consumer1
consumer1, err := client.Subscribe(ConsumerOptions{
Topic: topic,
SubscriptionName: "my-sub",
Type: Shared,
EnableZeroQueueConsumer: true,
})
assert.Nil(t, err)
_, ok := consumer1.(*zeroQueueConsumer)
assert.True(t, ok)
defer consumer1.Close()

// create consumer2
consumer2, err := client.Subscribe(ConsumerOptions{
Topic: topic,
SubscriptionName: "my-sub",
Type: Shared,
EnableZeroQueueConsumer: true,
})
assert.Nil(t, err)
_, ok = consumer2.(*zeroQueueConsumer)
assert.True(t, ok)
defer consumer2.Close()

// create producer
producer, err := client.CreateProducer(ProducerOptions{
Topic: topic,
DisableBatching: false,
})
assert.Nil(t, err)
defer producer.Close()

sendNum := 10
// send 10 messages
for i := 0; i < sendNum; i++ {
msg, err := producer.Send(ctx, &ProducerMessage{
Payload: []byte(fmt.Sprintf("hello-%d", i)),
Key: "pulsar",
Properties: map[string]string{
"key-1": "pulsar-1",
},
})
assert.Nil(t, err)
log.Printf("send message: %s", msg.String())
}

// receive messages
for i := 0; i < sendNum/2; i++ {
msg, err := consumer1.Receive(context.Background())
if err != nil {
log.Fatal(err)
}
log.Printf("consumer1 receive message: %s %s", msg.ID().String(), msg.Payload())
// ack message
consumer1.Ack(msg)
}

// receive messages
for i := 0; i < sendNum/2; i++ {
msg, err := consumer2.Receive(context.Background())
if err != nil {
log.Fatal(err)
}
log.Printf("consumer2 receive message: %s %s", msg.ID().String(), msg.Payload())
// ack message
consumer2.Ack(msg)
}

}

func TestPartitionZeroQueueConsumer(t *testing.T) {
client, err := NewClient(ClientOptions{
URL: lookupURL,
Expand Down
Loading