Skip to content

Expose Message.totalSize, Message.size, and Message.headerSize #1337

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ func (msg Message) message(cw *crc32Writer) message {

const timestampSize = 8

func (msg *Message) size() int32 {
func (msg *Message) Size() int32 {
return 4 + 1 + 1 + sizeofBytes(msg.Key) + sizeofBytes(msg.Value) + timestampSize
}

func (msg *Message) headerSize() int {
func (msg *Message) HeaderSize() int {
return varArrayLen(len(msg.Headers), func(i int) int {
h := &msg.Headers[i]
return varStringLen(h.Key) + varBytesLen(h.Value)
})
}

func (msg *Message) totalSize() int32 {
return int32(msg.headerSize()) + msg.size()
func (msg *Message) TotalSize() int32 {
return int32(msg.HeaderSize()) + msg.Size()
}

type message struct {
Expand Down
2 changes: 1 addition & 1 deletion message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ func TestMessageSize(t *testing.T) {
Time: randate(),
}
expSize := msg.message(nil).size()
gotSize := msg.size()
gotSize := msg.Size()
if expSize != gotSize {
t.Errorf("Expected size %d, but got size %d", expSize, gotSize)
}
Expand Down
4 changes: 2 additions & 2 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ func (w *Writer) WriteMessages(ctx context.Context, msgs ...Message) error {
batchBytes := w.batchBytes()

for i := range msgs {
n := int64(msgs[i].totalSize())
n := int64(msgs[i].TotalSize())
if n > batchBytes {
// This error is left for backward compatibility with historical
// behavior, but it can yield O(N^2) behaviors. The expectations
Expand Down Expand Up @@ -1219,7 +1219,7 @@ func newWriteBatch(now time.Time, timeout time.Duration) *writeBatch {
}

func (b *writeBatch) add(msg Message, maxSize int, maxBytes int64) bool {
bytes := int64(msg.totalSize())
bytes := int64(msg.TotalSize())

if b.size > 0 && (b.bytes+bytes) > maxBytes {
return false
Expand Down