Skip to content

Fix client fails on large responses #25

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 29 additions & 1 deletion manticore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,34 @@ func (cl *Client) connect() error {
return cl.failclose(err)
}

/// read raw answer (with fixed size) to buf
func (cl *Client) readRawAnswer(buf []byte, size int) (int, error) {
const MAX_CHUNK_SIZE = 16 * 1024
nbytes := 0
for {
chunkSize := MAX_CHUNK_SIZE
bytesRemaining := size - nbytes
if bytesRemaining < chunkSize {
chunkSize = bytesRemaining
}
n, e := cl.conn.Read(buf[nbytes:nbytes+chunkSize])
if e != nil {
return n, e
}
nbytes += n
if (nbytes < size) {
continue
}
break
}

if (nbytes > size) {
return nbytes, errors.New("Logical error in Client.read()!")
}

return nbytes, nil
}

/// get and check response packet from searchd server
func (cl *Client) getResponse(client_ver uCommandVersion) (apibuf, error) {
rawrecv := cl.getByteBuf(8)
Expand All @@ -151,7 +179,7 @@ func (cl *Client) getResponse(client_ver uCommandVersion) (apibuf, error) {
iReplySize := rawrecv.getInt()

rawanswer := cl.getByteBuf(iReplySize)
nbytes, err = cl.conn.Read(*rawanswer)
nbytes, err = cl.readRawAnswer(*rawanswer, iReplySize)
if err != nil {
return nil, err
}
Expand Down