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

pre-allocate decoding errors #690

Merged
merged 2 commits into from
Jul 5, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 5 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ var ErrShuttingDown = errors.New("kafka: message received by producer in process
// ErrMessageTooLarge is returned when the next message to consume is larger than the configured Consumer.Fetch.Max
var ErrMessageTooLarge = errors.New("kafka: message is larger than Consumer.Fetch.Max")

var ErrInvalidArrayLength = PacketDecodingError{"invalid array length"}
var ErrInvalidByteSliceLength = PacketDecodingError{"invalid byteslice length"}
var ErrInvalidStringLength = PacketDecodingError{"invalid string length"}
var ErrInvalidSubsetSize = PacketDecodingError{"invalid subset size"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might make more sense to make these unexported and move them to the top of real_decoder.go, I can't think of a case where the caller would need to be able to distinguish between them.


// PacketEncodingError is returned from a failure while encoding a Kafka packet. This can happen, for example,
// if you try to encode a string over 2^15 characters in length, since Kafka's encoding rules do not permit that.
type PacketEncodingError struct {
Expand Down
14 changes: 7 additions & 7 deletions real_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (rd *realDecoder) getArrayLength() (int, error) {
rd.off = len(rd.raw)
return -1, ErrInsufficientData
} else if tmp > 2*math.MaxUint16 {
return -1, PacketDecodingError{"invalid array length"}
return -1, ErrInvalidArrayLength
}
return tmp, nil
}
Expand All @@ -82,7 +82,7 @@ func (rd *realDecoder) getBytes() ([]byte, error) {

switch {
case n < -1:
return nil, PacketDecodingError{"invalid byteslice length"}
return nil, ErrInvalidByteSliceLength
case n == -1:
return nil, nil
case n == 0:
Expand All @@ -108,7 +108,7 @@ func (rd *realDecoder) getString() (string, error) {

switch {
case n < -1:
return "", PacketDecodingError{"invalid string length"}
return "", ErrInvalidStringLength
case n == -1:
return "", nil
case n == 0:
Expand Down Expand Up @@ -141,7 +141,7 @@ func (rd *realDecoder) getInt32Array() ([]int32, error) {
}

if n < 0 {
return nil, PacketDecodingError{"invalid array length"}
return nil, ErrInvalidArrayLength
}

ret := make([]int32, n)
Expand Down Expand Up @@ -170,7 +170,7 @@ func (rd *realDecoder) getInt64Array() ([]int64, error) {
}

if n < 0 {
return nil, PacketDecodingError{"invalid array length"}
return nil, ErrInvalidArrayLength
}

ret := make([]int64, n)
Expand All @@ -194,7 +194,7 @@ func (rd *realDecoder) getStringArray() ([]string, error) {
}

if n < 0 {
return nil, PacketDecodingError{"invalid array length"}
return nil, ErrInvalidArrayLength
}

ret := make([]string, n)
Expand All @@ -216,7 +216,7 @@ func (rd *realDecoder) remaining() int {

func (rd *realDecoder) getSubset(length int) (packetDecoder, error) {
if length < 0 {
return nil, PacketDecodingError{"invalid subset size"}
return nil, ErrInvalidSubsetSize
} else if length > rd.remaining() {
rd.off = len(rd.raw)
return nil, ErrInsufficientData
Expand Down