From e6daa877138d055cec3ea92f42439653054244e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sm=C3=B3=C5=82ka?= Date: Sat, 2 Nov 2024 16:48:18 +0100 Subject: [PATCH] Router: publish messages in bulk (#513) Currently, the router will publish produced messaged by calling Publish individually, even though the bulk API exists. This change works the same, although it can be handy for some custom implementations when you want to treat the produced messages as a group. For some implementations, it could also slightly improve publish performance. --- message/router.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/message/router.go b/message/router.go index 1b93e85a6..0ecf48c6d 100644 --- a/message/router.go +++ b/message/router.go @@ -825,15 +825,12 @@ func (h *handler) publishProducedMessages(producedMessages Messages, msgFields w "publish_topic": h.publishTopic, })) - for _, msg := range producedMessages { - if err := h.publisher.Publish(h.publishTopic, msg); err != nil { - // todo - how to deal with it better/transactional/retry? - h.logger.Error("Cannot publish message", err, msgFields.Add(watermill.LogFields{ - "not_sent_message": fmt.Sprintf("%#v", producedMessages), - })) - - return err - } + if err := h.publisher.Publish(h.publishTopic, producedMessages...); err != nil { + h.logger.Error("Cannot publish messages", err, msgFields.Add(watermill.LogFields{ + "not_sent_message": fmt.Sprintf("%#v", producedMessages), + })) + + return err } return nil