@@ -13,6 +13,7 @@ import (
1313 "github.com/aws/aws-sdk-go-v2/service/sns"
1414 "github.com/aws/aws-sdk-go-v2/service/sqs"
1515 "github.com/botchris/go-pubsub"
16+ "github.com/botchris/go-pubsub/middleware/codec"
1617 "github.com/hashicorp/go-multierror"
1718 "github.com/kubemq-io/kubemq-go/pkg/uuid"
1819)
@@ -38,10 +39,15 @@ type broker struct {
3839// implementation (`pubsub.topic`). This allows to keep the topic name
3940// agnostic to their underlying implementation.
4041//
42+ // You can instance a write-only broker by passing no SQS related configuration options.
43+ // This broker will only be able to publish messages to SNS topics, but will not be able to
44+ // subscribe handlers to topics nor receive messages from SQS queues. Of course, no goroutine
45+ // to poll the SQS queue will be started in this case.
46+ //
4147// IMPORTANT: this broker must be used in conjunction with a Codec middleware in
4248// order to ensure that the messages are properly encoded and decoded.
4349// Otherwise, only binary messages will be accepted when publishing or
44- // delivering messages.
50+ // delivering messages. Hence, `WithCodec` option is mandatory.
4551func NewBroker (ctx context.Context , option ... Option ) (pubsub.Broker , error ) {
4652 opts := & options {
4753 deliverTimeout : 5 * time .Second ,
@@ -59,12 +65,12 @@ func NewBroker(ctx context.Context, option ...Option) (pubsub.Broker, error) {
5965 return nil , errors .New ("no SNS client was provided" )
6066 }
6167
62- if opts .sqsClient = = nil {
63- return nil , errors . New ( "no SQS client was provided" )
68+ if err := opts .validateSQSSettings (); err ! = nil {
69+ return nil , err
6470 }
6571
66- if opts .sqsQueueURL == "" {
67- return nil , errors .New ("no SQS queue URL was provided" )
72+ if opts .codec == nil {
73+ return nil , errors .New ("no codec was provided" )
6874 }
6975
7076 ctx , cancel := context .WithCancel (ctx )
@@ -84,7 +90,7 @@ func NewBroker(ctx context.Context, option ...Option) (pubsub.Broker, error) {
8490 go b .run ()
8591 }()
8692
87- return b , nil
93+ return codec . NewCodecMiddleware ( b , opts . codec ) , nil
8894}
8995
9096func (b * broker ) Publish (ctx context.Context , topic pubsub.Topic , m interface {}) error {
@@ -111,6 +117,10 @@ func (b *broker) Publish(ctx context.Context, topic pubsub.Topic, m interface{})
111117}
112118
113119func (b * broker ) Subscribe (ctx context.Context , topic pubsub.Topic , handler pubsub.Handler , option ... pubsub.SubscribeOption ) (pubsub.Subscription , error ) {
120+ if b .options .isWriteOnly () {
121+ return nil , errors .New ("no SQS queue ARN provided, broker is configured as write-only" )
122+ }
123+
114124 topicARN , err := b .topics .arnOf (topic )
115125 if err != nil {
116126 return nil , err
@@ -196,6 +206,10 @@ func (b *broker) Shutdown(ctx context.Context) error {
196206}
197207
198208func (b * broker ) run () {
209+ if b .options .isWriteOnly () {
210+ return
211+ }
212+
199213 done := b .runnerCtx .Done ()
200214 topicTicker := time .NewTicker (b .options .topicsReloadInterval )
201215 defer topicTicker .Stop ()
0 commit comments