Skip to content

Commit c2ff17b

Browse files
committed
Move fallback topic option to the configuration and allow the topic to be configured
1 parent 0eae6f7 commit c2ff17b

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

pubsub/gochannel/pubsub.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import (
1111
"github.com/ThreeDotsLabs/watermill/message"
1212
)
1313

14-
// NoSubscribersFallbackTopic is the fallback topic messages without any subscribers will be sent to.
15-
// This is used if the `EnableFallback` configuration option is enabled.
16-
const NoSubscribersFallbackTopic = "*"
14+
// NoSubscribersFallbackDefaultTopic is the default fallback topic messages without any subscribers
15+
// will be sent to – it is used if the `EnableNoSubscribersFallback` option is enabled and no
16+
// fallback topic is configured via the `NoSubscribersFallbackTopic` option.
17+
const NoSubscribersFallbackDefaultTopic = "*"
1718

1819
// Config holds the GoChannel Pub/Sub's configuration options.
1920
type Config struct {
@@ -32,8 +33,13 @@ type Config struct {
3233
BlockPublishUntilSubscriberAck bool
3334

3435
// When true, messages sent to a topic without any subscribers will be sent to the
35-
// subscribers of the `*` topic.
36-
EnableFallback bool
36+
// subscribers of the fallback topic (configured via `NoSubscribersFallbackTopic` option).
37+
EnableNoSubscribersFallback bool
38+
39+
// NoSubscribersFallbackTopic is the fallback topic messages without any subscribers will be sent to.
40+
// This is used if the `EnableNoSubscribersFallback` configuration option is enabled.
41+
// If it's not set then `*` is used by default.
42+
NoSubscribersFallbackTopic string
3743
}
3844

3945
// GoChannel is the simplest Pub/Sub implementation.
@@ -69,6 +75,10 @@ func NewGoChannel(config Config, logger watermill.LoggerAdapter) *GoChannel {
6975
logger = watermill.NopLogger{}
7076
}
7177

78+
if config.EnableNoSubscribersFallback && config.NoSubscribersFallbackTopic == "" {
79+
config.NoSubscribersFallbackTopic = NoSubscribersFallbackDefaultTopic
80+
}
81+
7282
return &GoChannel{
7383
config: config,
7484

@@ -149,12 +159,12 @@ func (g *GoChannel) sendMessage(topic string, message *message.Message) (<-chan
149159
logFields := watermill.LogFields{"message_uuid": message.UUID, "topic": topic}
150160

151161
if len(subscribers) == 0 {
152-
if !g.config.EnableFallback {
162+
if !g.config.EnableNoSubscribersFallback {
153163
return g.handleNoSubscribers(ackedBySubscribers, logFields)
154164
}
155165

156166
g.logger.Debug("No subscribers to send the message to, trying the fallback subscribers", logFields)
157-
if subscribers = g.topicSubscribers(NoSubscribersFallbackTopic); len(subscribers) == 0 {
167+
if subscribers = g.topicSubscribers(g.config.NoSubscribersFallbackTopic); len(subscribers) == 0 {
158168
return g.handleNoSubscribers(ackedBySubscribers, logFields)
159169
}
160170
}

pubsub/gochannel/pubsub_test.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,41 @@ func TestPublishSubscribe_not_persistent(t *testing.T) {
6363
assert.NoError(t, pubSub.Close())
6464
}
6565

66-
func TestPublishSubscribe_enable_fallback(t *testing.T) {
66+
func TestPublishSubscribe_enable_no_subscribers_fallback(t *testing.T) {
6767
messagesCount := 100
6868
pubSub := gochannel.NewGoChannel(
6969
gochannel.Config{
70-
OutputChannelBuffer: int64(messagesCount),
71-
EnableFallback: true,
70+
OutputChannelBuffer: int64(messagesCount),
71+
EnableNoSubscribersFallback: true,
72+
},
73+
watermill.NewStdLogger(true, true),
74+
)
75+
topicName := "test_topic_" + watermill.NewUUID()
76+
77+
msgs, err := pubSub.Subscribe(context.Background(), gochannel.NoSubscribersFallbackDefaultTopic)
78+
require.NoError(t, err)
79+
80+
sendMessages := tests.PublishSimpleMessages(t, messagesCount, pubSub, topicName)
81+
receivedMsgs, _ := subscriber.BulkRead(msgs, messagesCount, time.Second)
82+
83+
tests.AssertAllMessagesReceived(t, sendMessages, receivedMsgs)
84+
85+
assert.NoError(t, pubSub.Close())
86+
}
87+
88+
func TestPublishSubscribe_enable_no_subscribers_fallback_with_custom_topic(t *testing.T) {
89+
messagesCount := 100
90+
pubSub := gochannel.NewGoChannel(
91+
gochannel.Config{
92+
OutputChannelBuffer: int64(messagesCount),
93+
EnableNoSubscribersFallback: true,
94+
NoSubscribersFallbackTopic: "custom_fallback_topic",
7295
},
7396
watermill.NewStdLogger(true, true),
7497
)
7598
topicName := "test_topic_" + watermill.NewUUID()
7699

77-
msgs, err := pubSub.Subscribe(context.Background(), gochannel.NoSubscribersFallbackTopic)
100+
msgs, err := pubSub.Subscribe(context.Background(), "custom_fallback_topic")
78101
require.NoError(t, err)
79102

80103
sendMessages := tests.PublishSimpleMessages(t, messagesCount, pubSub, topicName)

0 commit comments

Comments
 (0)