Skip to content

Commit 067dca1

Browse files
authored
[Improve] Set dlq producerName (#1137)
### Motivation To keep consistent with the Java client. Releted PR: apache/pulsar#21589 *Explain here the context, and why you're making that change. What is the problem you're trying to solve.* ### Modifications Set DLQ producerName `%s-%s-DLQ`
1 parent d457442 commit 067dca1

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

pulsar/consumer_impl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func newConsumer(client *client, options ConsumerOptions) (Consumer, error) {
167167
}
168168
}
169169

170-
dlq, err := newDlqRouter(client, options.DLQ, client.log)
170+
dlq, err := newDlqRouter(client, options.DLQ, options.Topic, options.SubscriptionName, client.log)
171171
if err != nil {
172172
return nil, err
173173
}

pulsar/consumer_regex_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func runRegexConsumerDiscoverPatternAll(t *testing.T, c Client, namespace string
154154
AutoDiscoveryPeriod: 5 * time.Minute,
155155
}
156156

157-
dlq, _ := newDlqRouter(c.(*client), nil, log.DefaultNopLogger())
157+
dlq, _ := newDlqRouter(c.(*client), nil, tn.Topic, "regex-sub", log.DefaultNopLogger())
158158
rlq, _ := newRetryRouter(c.(*client), nil, false, log.DefaultNopLogger())
159159
consumer, err := newRegexConsumer(c.(*client), opts, tn, pattern, make(chan ConsumerMessage, 1), dlq, rlq)
160160
if err != nil {
@@ -192,7 +192,7 @@ func runRegexConsumerDiscoverPatternFoo(t *testing.T, c Client, namespace string
192192
AutoDiscoveryPeriod: 5 * time.Minute,
193193
}
194194

195-
dlq, _ := newDlqRouter(c.(*client), nil, log.DefaultNopLogger())
195+
dlq, _ := newDlqRouter(c.(*client), nil, tn.Topic, "regex-sub", log.DefaultNopLogger())
196196
rlq, _ := newRetryRouter(c.(*client), nil, false, log.DefaultNopLogger())
197197
consumer, err := newRegexConsumer(c.(*client), opts, tn, pattern, make(chan ConsumerMessage, 1), dlq, rlq)
198198
if err != nil {

pulsar/consumer_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1449,9 +1449,10 @@ func DLQWithProducerOptions(t *testing.T, prodOpt *ProducerOptions) {
14491449
if prodOpt != nil {
14501450
dlqPolicy.ProducerOptions = *prodOpt
14511451
}
1452+
sub := "my-sub"
14521453
consumer, err := client.Subscribe(ConsumerOptions{
14531454
Topic: topic,
1454-
SubscriptionName: "my-sub",
1455+
SubscriptionName: sub,
14551456
NackRedeliveryDelay: 1 * time.Second,
14561457
Type: Shared,
14571458
DLQ: &dlqPolicy,
@@ -1506,6 +1507,9 @@ func DLQWithProducerOptions(t *testing.T, prodOpt *ProducerOptions) {
15061507
expectMsg := fmt.Sprintf("hello-%d", expectedMsgIdx)
15071508
assert.Equal(t, []byte(expectMsg), msg.Payload())
15081509

1510+
// check dql produceName
1511+
assert.Equal(t, msg.ProducerName(), fmt.Sprintf("%s-%s-DLQ", topic, sub))
1512+
15091513
// check original messageId
15101514
assert.NotEmpty(t, msg.Properties()[PropertyOriginMessageID])
15111515

pulsar/dlq_router.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,32 @@ package pulsar
1919

2020
import (
2121
"context"
22+
"fmt"
2223
"time"
2324

2425
"github.com/apache/pulsar-client-go/pulsar/internal"
2526
"github.com/apache/pulsar-client-go/pulsar/log"
2627
)
2728

2829
type dlqRouter struct {
29-
client Client
30-
producer Producer
31-
policy *DLQPolicy
32-
messageCh chan ConsumerMessage
33-
closeCh chan interface{}
34-
log log.Logger
30+
client Client
31+
producer Producer
32+
policy *DLQPolicy
33+
messageCh chan ConsumerMessage
34+
closeCh chan interface{}
35+
topicName string
36+
subscriptionName string
37+
log log.Logger
3538
}
3639

37-
func newDlqRouter(client Client, policy *DLQPolicy, logger log.Logger) (*dlqRouter, error) {
40+
func newDlqRouter(client Client, policy *DLQPolicy, topicName, subscriptionName string,
41+
logger log.Logger) (*dlqRouter, error) {
3842
r := &dlqRouter{
39-
client: client,
40-
policy: policy,
41-
log: logger,
43+
client: client,
44+
policy: policy,
45+
topicName: topicName,
46+
subscriptionName: subscriptionName,
47+
log: logger,
4248
}
4349

4450
if policy != nil {
@@ -152,6 +158,9 @@ func (r *dlqRouter) getProducer(schema Schema) Producer {
152158
opt := r.policy.ProducerOptions
153159
opt.Topic = r.policy.DeadLetterTopic
154160
opt.Schema = schema
161+
if opt.Name == "" {
162+
opt.Name = fmt.Sprintf("%s-%s-DLQ", r.topicName, r.subscriptionName)
163+
}
155164

156165
// the origin code sets to LZ4 compression with no options
157166
// so the new design allows compression type to be overwritten but still set lz4 by default

pulsar/reader_impl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func newReader(client *client, options ReaderOptions) (Reader, error) {
127127
}
128128

129129
// Provide dummy dlq router with not dlq policy
130-
dlq, err := newDlqRouter(client, nil, client.log)
130+
dlq, err := newDlqRouter(client, nil, options.Topic, options.SubscriptionName, client.log)
131131
if err != nil {
132132
return nil, err
133133
}

0 commit comments

Comments
 (0)