Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return ErrMessageSizeTooLarge when message is too large #2851

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions async_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
29 changes: 29 additions & 0 deletions sync_producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 { //nolint:errorlint // linter complains that we use errors.Is(), but we know code bases out there don't, so this test is specifically to test that we don't wrap this
t.Error("expected err to be ErrMessageSizeTooLarge - many people rely on this, please do not change without 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 { //nolint:errorlint // linter complains that we use errors.Is(), but we know code bases out there don't, so this test is specifically to test that we don't wrap this
t.Error("expected err to be ErrMessageSizeTooLarge - many people rely on this, please do not change without searching for previous discussions")
}

safeClose(t, producer)
leader.Close()
seedBroker.Close()
Expand Down
Loading