Skip to content

Commit

Permalink
Empty the socket before returning on errors in Query and Scan
Browse files Browse the repository at this point in the history
  • Loading branch information
khaf committed Jun 9, 2015
1 parent 9996155 commit 3a4f98b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
4 changes: 3 additions & 1 deletion info.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ func (nfo *info) sendCommand(conn *Connection) error {
}

// Logger.Debug("Header Response: %v %v %v %v", t.Type, t.Version, t.Length(), t.DataLen)
nfo.msg.Resize(nfo.msg.Length())
if err := nfo.msg.Resize(nfo.msg.Length()); err != nil {
return err
}
_, err := conn.Read(nfo.msg.Data, len(nfo.msg.Data))
return err
}
Expand Down
4 changes: 4 additions & 0 deletions query_record_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func (cmd *queryRecordCommand) parseRecordResults(ifc command, receiveSize int)

if resultCode != 0 {
if resultCode == KEY_NOT_FOUND_ERROR {
// consume the rest of the input buffer from the socket
if cmd.dataOffset < receiveSize {
cmd.readBytes(receiveSize - cmd.dataOffset)
}
return false, nil
}
err := NewAerospikeError(resultCode)
Expand Down
4 changes: 4 additions & 0 deletions scan_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func (cmd *scanCommand) parseRecordResults(ifc command, receiveSize int) (bool,

if resultCode != 0 {
if resultCode == KEY_NOT_FOUND_ERROR {
// consume the rest of the input buffer from the socket
if cmd.dataOffset < receiveSize {
cmd.readBytes(receiveSize - cmd.dataOffset)
}
return false, nil
}
err := NewAerospikeError(resultCode)
Expand Down
11 changes: 9 additions & 2 deletions types/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package types
import (
"bytes"
"encoding/binary"
"fmt"

// . "github.com/aerospike/aerospike-client-go/logger"
)
Expand Down Expand Up @@ -59,12 +60,18 @@ func NewMessage(mtype messageType, data []byte) *Message {
}
}

const maxAllowedBufferSize = 1024 * 1024

// Resize changes the internal buffer size for the message.
func (msg *Message) Resize(newSize int64) {
func (msg *Message) Resize(newSize int64) error {
if newSize > maxAllowedBufferSize {
return fmt.Errorf("Requested new buffer size is too big. Requested: %d, max allowed: %d", newSize, maxAllowedBufferSize)
}
if int64(len(msg.Data)) == newSize {
return
return nil
}
msg.Data = make([]byte, newSize)
return nil
}

// Serialize returns a byte slice containing the message.
Expand Down

0 comments on commit 3a4f98b

Please sign in to comment.