···3232 panic(err)
3333 }
34343535- for msg := range consumer.Msgs {
3535+ for msg := range consumer.Messages() {
3636 slog.Info("received message", "message", string(msg.Data))
3737 }
3838
+8-4
pubsub/subscriber.go
···132132// Consumer allows the consumption of messages. It is thread safe to range over the Msgs channel to consume. If during the consumer
133133// receiving messages from the server an error occurs, it will be stored in Err
134134type Consumer struct {
135135- Msgs chan messagebroker.Message
135135+ msgs chan messagebroker.Message
136136 // TODO: better error handling? Maybe a channel of errors?
137137 Err error
138138+}
139139+140140+func (c *Consumer) Messages() <-chan messagebroker.Message {
141141+ return c.msgs
138142}
139143140144// Consume will create a consumer and start it running in a go routine. You can then use the Msgs channel of the consumer
141145// to read the messages
142146func (s *Subscriber) Consume(ctx context.Context) *Consumer {
143147 consumer := &Consumer{
144144- Msgs: make(chan messagebroker.Message),
148148+ msgs: make(chan messagebroker.Message),
145149 }
146150147151 go s.consume(ctx, consumer)
···150154}
151155152156func (s *Subscriber) consume(ctx context.Context, consumer *Consumer) {
153153- defer close(consumer.Msgs)
157157+ defer close(consumer.msgs)
154158 for {
155159 if ctx.Err() != nil {
156160 return
···163167 }
164168165169 if msg != nil {
166166- consumer.Msgs <- *msg
170170+ consumer.msgs <- *msg
167171 }
168172 }
169173}
+2-2
pubsub/subscriber_test.go
···107107 var receivedMessages []messagebroker.Message
108108 consumerFinCh := make(chan struct{})
109109 go func() {
110110- for msg := range consumer.Msgs {
110110+ for msg := range consumer.Messages() {
111111 receivedMessages = append(receivedMessages, msg)
112112 }
113113···180180181181 consumerFinCh := make(chan struct{})
182182 go func() {
183183- for msg := range consumer.Msgs {
183183+ for msg := range consumer.Messages() {
184184 receivedMessages = append(receivedMessages, msg)
185185 }
186186