From 7f32943dd8f11c1a17d602566437e196817acaa1 Mon Sep 17 00:00:00 2001 From: Adam Eijdenberg Date: Tue, 2 Apr 2024 10:52:40 +1100 Subject: [PATCH] fix: return ErrMessageSizeTooLarge when message is too large For most of this library's existence it has returned ErrMessageSizeTooLarge when the message exceeded the configured size. For a short period in 2019 this error was renamed (#1218) but shortly revered back (#1262). Later in 2023 this error was changed to a ConfigurationError (#2628) to fix #2137, however this has caused issues with clients who rely on the previous error code being distinct from other ConfigurationError conditions (#2655). This commit reverts to previous behaviour, and adds a test to pickup if this changes again in the future. Signed-off-by: Adam Eijdenberg --- async_producer.go | 5 ++--- sync_producer_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/async_producer.go b/async_producer.go index f629a6a2e..8843a29fb 100644 --- a/async_producer.go +++ b/async_producer.go @@ -450,9 +450,8 @@ func (p *asyncProducer) dispatcher() { continue } - size := msg.ByteSize(version) - if size > p.conf.Producer.MaxMessageBytes { - p.returnError(msg, ConfigurationError(fmt.Sprintf("Attempt to produce message larger than configured Producer.MaxMessageBytes: %d > %d", size, p.conf.Producer.MaxMessageBytes))) + if msg.ByteSize(version) > p.conf.Producer.MaxMessageBytes { + p.returnError(msg, ErrMessageSizeTooLarge) continue } diff --git a/sync_producer_test.go b/sync_producer_test.go index 776ae5f69..99c542b4c 100644 --- a/sync_producer_test.go +++ b/sync_producer_test.go @@ -52,6 +52,35 @@ func TestSyncProducer(t *testing.T) { } } + // try to send a message too large + _, _, err = producer.SendMessage(&ProducerMessage{ + Topic: "my_topic", + Value: ByteEncoder(make([]byte, config.Producer.MaxMessageBytes+1)), // will exceed default max size, e.g. configuration side + Metadata: "test", + }) + if err != ErrMessageSizeTooLarge { + t.Error("expected err to be ErrMessageSizeTooLarge - many people rely on this, please do not change with searching for previous discussions") + } + + // try to send small message the server rejects because too large + leader.Returns(&ProduceResponse{ + Blocks: map[string]map[int32]*ProduceResponseBlock{ + "my_topic": { + 0: &ProduceResponseBlock{ + Err: ErrMessageSizeTooLarge, + }, + }, + }, + }) + _, _, err = producer.SendMessage(&ProducerMessage{ + Topic: "my_topic", + Value: StringEncoder(TestMessage), + Metadata: "test", + }) + if err != ErrMessageSizeTooLarge { + t.Error("expected err to be ErrMessageSizeTooLarge - many people rely on this, please do not change with searching for previous discussions") + } + safeClose(t, producer) leader.Close() seedBroker.Close()