Skip to content

Commit

Permalink
fix: return ErrMessageSizeTooLarge when message is too large
Browse files Browse the repository at this point in the history
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 <adam.eijdenberg@defence.gov.au>
  • Loading branch information
ae-govau committed Apr 3, 2024
1 parent 4ad3504 commit 7f32943
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
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 {

Check failure on line 61 in sync_producer_test.go

View workflow job for this annotation

GitHub Actions / Linting with Go 1.22.x

comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error (errorlint)
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 {

Check failure on line 80 in sync_producer_test.go

View workflow job for this annotation

GitHub Actions / Linting with Go 1.22.x

comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error (errorlint)
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()
Expand Down

0 comments on commit 7f32943

Please sign in to comment.