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

[ISSUE #635] Improving decoding performance for remoting commands #636

Merged
merged 1 commit into from
Apr 22, 2021
Merged
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
19 changes: 7 additions & 12 deletions internal/remote/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func encode(command *RemotingCommand) ([]byte, error) {
}

func decode(data []byte) (*RemotingCommand, error) {
buf := bytes.NewBuffer(data)
buf := bytes.NewReader(data)
length := int32(len(data))
var oriHeaderLen int32
err := binary.Read(buf, binary.BigEndian, &oriHeaderLen)
Expand All @@ -225,8 +225,7 @@ func decode(data []byte) (*RemotingCommand, error) {

headerLength := oriHeaderLen & 0xFFFFFF
headerData := make([]byte, headerLength)
err = binary.Read(buf, binary.BigEndian, &headerData)
if err != nil {
if _, err = io.ReadFull(buf, headerData); err != nil {
return nil, err
}

Expand All @@ -246,8 +245,7 @@ func decode(data []byte) (*RemotingCommand, error) {
bodyLength := length - 4 - headerLength
if bodyLength > 0 {
bodyData := make([]byte, bodyLength)
err = binary.Read(buf, binary.BigEndian, &bodyData)
if err != nil {
if _, err = io.ReadFull(buf, bodyData); err != nil {
return nil, err
}
command.Body = bodyData
Expand Down Expand Up @@ -463,8 +461,7 @@ func (c *rmqCodec) decodeHeader(data []byte) (*RemotingCommand, error) {

if remarkLen > 0 {
var remarkData = make([]byte, remarkLen)
err = binary.Read(buf, binary.BigEndian, &remarkData)
if err != nil {
if _, err = io.ReadFull(buf, remarkData); err != nil {
return nil, err
}
command.Remark = string(remarkData)
Expand All @@ -477,8 +474,7 @@ func (c *rmqCodec) decodeHeader(data []byte) (*RemotingCommand, error) {

if extFieldsLen > 0 {
extFieldsData := make([]byte, extFieldsLen)
err = binary.Read(buf, binary.BigEndian, &extFieldsData)
if err != nil {
if _, err := io.ReadFull(buf, extFieldsData); err != nil {
return nil, err
}

Expand Down Expand Up @@ -515,10 +511,9 @@ func (c *rmqCodec) decodeHeader(data []byte) (*RemotingCommand, error) {
return command, nil
}

func getExtFieldsData(buff *bytes.Buffer, length int32) (string, error) {
func getExtFieldsData(buff io.Reader, length int32) (string, error) {
var data = make([]byte, length)
err := binary.Read(buff, binary.BigEndian, &data)
if err != nil {
if _, err := io.ReadFull(buff, data); err != nil {
return "", err
}

Expand Down