kafka-go icon indicating copy to clipboard operation
kafka-go copied to clipboard

`int` Type Message Fields as Pointers

Open colechiumento-coreweave opened this issue 10 months ago • 1 comments

Describe the solution you would like

Currently, the implementation of Message is:

type Message struct {
	// Topic indicates which topic this message was consumed from via Reader.
	//
	// When being used with Writer, this can be used to configure the topic if
	// not already specified on the writer itself.
	Topic string

	// Partition is read-only and MUST NOT be set when writing messages
	Partition     int
	Offset        int64
	HighWaterMark int64
	Key           []byte
	Value         []byte
	Headers       []Header

	// This field is used to hold arbitrary data you wish to include, so it
	// will be available when handle it on the Writer's `Completion` method,
	// this support the application can do any post operation on each message.
	WriterData interface{}

	// If not set at the creation, Time will be automatically set when
	// writing the message.
	Time time.Time
}

This means that, upon creation of a new Message, the following int fields are set to 0: Partition, Offset, and HighWaterMark. Given that Partition and Offset can be 0 in Kafka, there is no reliable way of determining whether a Message has actually populated Partition, Offset, or HighWaterMark when introspecting these fields.

I would propose converting these integer fields to pointers, e.g.

type Message struct {
	// Topic indicates which topic this message was consumed from via Reader.
	//
	// When being used with Writer, this can be used to configure the topic if
	// not already specified on the writer itself.
	Topic string

	// Partition is read-only and MUST NOT be set when writing messages
	Partition     *int
	Offset        *int64
	HighWaterMark *int64
	Key           []byte
	Value         []byte
	Headers       []Header

	// This field is used to hold arbitrary data you wish to include, so it
	// will be available when handle it on the Writer's `Completion` method,
	// this support the application can do any post operation on each message.
	WriterData interface{}

	// If not set at the creation, Time will be automatically set when
	// writing the message.
	Time time.Time
}

which would allow functions that access these fields the ability to test for 0 values in a non-confusing way with little change to the underlying codebase.

colechiumento-coreweave avatar Jan 30 '25 23:01 colechiumento-coreweave

Keeping primary fields as pointers can be a problem in terms of GC (because of heap/stack allocation decision) btw.

Abdulsametileri avatar Feb 04 '25 11:02 Abdulsametileri