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

Fix a bug when parsing multiple responses #115

Merged
merged 2 commits into from
Aug 18, 2018
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
60 changes: 30 additions & 30 deletions src/network/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,43 +304,43 @@ module.exports = class Connection {

this.buffer = Buffer.concat([this.buffer, rawData])

// Not enough bytes to read the expected response size, keep buffering
if (Buffer.byteLength(this.buffer) <= Decoder.int32Size()) {
return
}

const data = Buffer.from(this.buffer)
const decoder = new Decoder(data)
const expectedResponseSize = decoder.readInt32()
// Process data if there are enough bytes to read the expected response size,
// otherwise keep buffering
while (Buffer.byteLength(this.buffer) > Decoder.int32Size()) {
const data = Buffer.from(this.buffer)
const decoder = new Decoder(data)
const expectedResponseSize = decoder.readInt32()

if (!decoder.canReadBytes(expectedResponseSize)) {
return
}

if (!decoder.canReadBytes(expectedResponseSize)) {
return
}
const response = new Decoder(decoder.readBytes(expectedResponseSize))
// Reset the buffer as the rest of the bytes
this.buffer = decoder.readAll()

// The full payload is loaded, erase the temporary buffer
this.buffer = Buffer.alloc(0)
if (this.authHandlers) {
return this.authHandlers.onSuccess(data.slice(0, this.offset + byteLength))
Copy link
Owner

Choose a reason for hiding this comment

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

byteLength isn't defined, this is breaking the tests

Copy link
Author

Choose a reason for hiding this comment

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

good catch! copy-paste bug...

}

if (this.authHandlers) {
return this.authHandlers.onSuccess(data)
}
const correlationId = response.readInt32()
const payload = response.readAll()

const correlationId = decoder.readInt32()
const payload = decoder.readAll()
const entry = this.pendingQueue[correlationId]
delete this.pendingQueue[correlationId]
Copy link
Owner

Choose a reason for hiding this comment

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

I wonder if we should remove the previous data from the buffer, I fear to end up leaking memory.

Copy link
Author

@bowendeng bowendeng Aug 17, 2018

Choose a reason for hiding this comment

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

I think we are okey, since the buffer is reset using decoder.readAll() which only copies (not slicing) the rest of the data. So the original buffer could be collected by GC once the response gets consumed.


const entry = this.pendingQueue[correlationId]
delete this.pendingQueue[correlationId]
if (!entry) {
this.logDebug(`Response without match`, { correlationId })
return
}

if (!entry) {
this.logDebug(`Response without match`, { correlationId })
return
entry.resolve({
size: expectedResponseSize,
correlationId,
entry,
payload,
})
}

entry.resolve({
size: expectedResponseSize,
correlationId,
entry,
payload,
})
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/protocol/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ module.exports = class Decoder {
return Buffer.byteLength(this.buffer) - this.offset >= length
}

readBytes() {
const byteLength = this.readInt32()

readBytes(byteLength = this.readInt32()) {
if (byteLength === -1) {
return null
}
Expand Down