未验证 提交 dfa26d15 编写于 作者: 张旭 提交者: GitHub

Merge pull request #636 from czah/remoting-cmd-decoding-optimization

[ISSUE #635] Improving decoding performance for remoting commands
...@@ -215,7 +215,7 @@ func encode(command *RemotingCommand) ([]byte, error) { ...@@ -215,7 +215,7 @@ func encode(command *RemotingCommand) ([]byte, error) {
} }
func decode(data []byte) (*RemotingCommand, error) { func decode(data []byte) (*RemotingCommand, error) {
buf := bytes.NewBuffer(data) buf := bytes.NewReader(data)
length := int32(len(data)) length := int32(len(data))
var oriHeaderLen int32 var oriHeaderLen int32
err := binary.Read(buf, binary.BigEndian, &oriHeaderLen) err := binary.Read(buf, binary.BigEndian, &oriHeaderLen)
...@@ -225,8 +225,7 @@ func decode(data []byte) (*RemotingCommand, error) { ...@@ -225,8 +225,7 @@ func decode(data []byte) (*RemotingCommand, error) {
headerLength := oriHeaderLen & 0xFFFFFF headerLength := oriHeaderLen & 0xFFFFFF
headerData := make([]byte, headerLength) headerData := make([]byte, headerLength)
err = binary.Read(buf, binary.BigEndian, &headerData) if _, err = io.ReadFull(buf, headerData); err != nil {
if err != nil {
return nil, err return nil, err
} }
...@@ -246,8 +245,7 @@ func decode(data []byte) (*RemotingCommand, error) { ...@@ -246,8 +245,7 @@ func decode(data []byte) (*RemotingCommand, error) {
bodyLength := length - 4 - headerLength bodyLength := length - 4 - headerLength
if bodyLength > 0 { if bodyLength > 0 {
bodyData := make([]byte, bodyLength) bodyData := make([]byte, bodyLength)
err = binary.Read(buf, binary.BigEndian, &bodyData) if _, err = io.ReadFull(buf, bodyData); err != nil {
if err != nil {
return nil, err return nil, err
} }
command.Body = bodyData command.Body = bodyData
...@@ -463,8 +461,7 @@ func (c *rmqCodec) decodeHeader(data []byte) (*RemotingCommand, error) { ...@@ -463,8 +461,7 @@ func (c *rmqCodec) decodeHeader(data []byte) (*RemotingCommand, error) {
if remarkLen > 0 { if remarkLen > 0 {
var remarkData = make([]byte, remarkLen) var remarkData = make([]byte, remarkLen)
err = binary.Read(buf, binary.BigEndian, &remarkData) if _, err = io.ReadFull(buf, remarkData); err != nil {
if err != nil {
return nil, err return nil, err
} }
command.Remark = string(remarkData) command.Remark = string(remarkData)
...@@ -477,8 +474,7 @@ func (c *rmqCodec) decodeHeader(data []byte) (*RemotingCommand, error) { ...@@ -477,8 +474,7 @@ func (c *rmqCodec) decodeHeader(data []byte) (*RemotingCommand, error) {
if extFieldsLen > 0 { if extFieldsLen > 0 {
extFieldsData := make([]byte, extFieldsLen) extFieldsData := make([]byte, extFieldsLen)
err = binary.Read(buf, binary.BigEndian, &extFieldsData) if _, err := io.ReadFull(buf, extFieldsData); err != nil {
if err != nil {
return nil, err return nil, err
} }
...@@ -515,10 +511,9 @@ func (c *rmqCodec) decodeHeader(data []byte) (*RemotingCommand, error) { ...@@ -515,10 +511,9 @@ func (c *rmqCodec) decodeHeader(data []byte) (*RemotingCommand, error) {
return command, nil 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) var data = make([]byte, length)
err := binary.Read(buff, binary.BigEndian, &data) if _, err := io.ReadFull(buff, data); err != nil {
if err != nil {
return "", err return "", err
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册