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

feat(NODE-4139): streaming protocol message changes #3256

Merged
merged 7 commits into from
May 23, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/cmap/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
this[kHello] = response;
}

// Set the whether the message stream is for a monitoring connection using the
// streaming protocol.
set isStreamingProtocol(value: boolean) {
this[kMessageStream].isStreamingProtocol = value;
}

get isStreamingProtocol(): boolean {
return this[kMessageStream].isStreamingProtocol;
}

get serviceId(): ObjectId | undefined {
return this.hello?.serviceId;
}
Expand Down
35 changes: 28 additions & 7 deletions src/cmap/message_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,19 @@ export class MessageStream extends Duplex {
maxBsonMessageSize: number;
/** @internal */
[kBuffer]: BufferPool;
/** @internal */
isStreamingProtocol = false;

constructor(options: MessageStreamOptions = {}) {
super(options);
this.maxBsonMessageSize = options.maxBsonMessageSize || kDefaultMaxBsonMessageSize;
this[kBuffer] = new BufferPool();
}

get buffer(): BufferPool {
return this[kBuffer];
}

override _write(chunk: Buffer, _: unknown, callback: Callback<Buffer>): void {
this[kBuffer].append(chunk);
processIncomingData(this, callback);
Expand Down Expand Up @@ -165,12 +171,20 @@ function processIncomingData(stream: MessageStream, callback: Callback<Buffer>)
let ResponseType = messageHeader.opCode === OP_MSG ? BinMsg : Response;
if (messageHeader.opCode !== OP_COMPRESSED) {
const messageBody = message.slice(MESSAGE_HEADER_SIZE);
stream.emit('message', new ResponseType(message, messageHeader, messageBody));

if (buffer.length >= 4) {
// If we are a monitoring message stream using the streaming protocol and
// there is more in the buffer that can be read, skip processing since we
// want the last hello command response that is in the buffer.
if (stream.isStreamingProtocol && buffer.length >= 4) {
jyemin marked this conversation as resolved.
Show resolved Hide resolved
processIncomingData(stream, callback);
} else {
callback();
stream.emit('message', new ResponseType(message, messageHeader, messageBody));

if (buffer.length >= 4) {
processIncomingData(stream, callback);
dariakp marked this conversation as resolved.
Show resolved Hide resolved
} else {
callback();
}
}

return;
Expand Down Expand Up @@ -198,12 +212,19 @@ function processIncomingData(stream: MessageStream, callback: Callback<Buffer>)
return;
}

stream.emit('message', new ResponseType(message, messageHeader, messageBody));

if (buffer.length >= 4) {
// If we are a monitoring message stream using the streaming protocol and
// there is more in the buffer that can be read, skip processing since we
// want the last hello command response that is in the buffer.
if (stream.isStreamingProtocol && buffer.length >= 4) {
jyemin marked this conversation as resolved.
Show resolved Hide resolved
processIncomingData(stream, callback);
} else {
callback();
stream.emit('message', new ResponseType(message, messageHeader, messageBody));

if (buffer.length >= 4) {
processIncomingData(stream, callback);
} else {
callback();
}
}
});
}
4 changes: 4 additions & 0 deletions src/sdam/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
// if we are using the streaming protocol then we immediately issue another `started`
// event, otherwise the "check" is complete and return to the main monitor loop
if (isAwaitable && hello.topologyVersion) {
jyemin marked this conversation as resolved.
Show resolved Hide resolved
// Tell the connection that we are using the streaming protocol so that the
// connection's message stream will only read the last hello on the buffer.
connection.isStreamingProtocol = true;

monitor.emit(
Server.SERVER_HEARTBEAT_STARTED,
new ServerHeartbeatStartedEvent(monitor.address)
Expand Down
28 changes: 28 additions & 0 deletions test/unit/cmap/message_stream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,34 @@ function bufferToStream(buffer) {
}

describe('Message Stream', function () {
context('when the stream uses the streaming protocol', function () {
dariakp marked this conversation as resolved.
Show resolved Hide resolved
const data = Buffer.from(
'370000000100000001000000010000000000000000000000000000000000000001000000130000001069736d6173746572000100000000' +
'370000000100000001000000010000000000000000000000000000000000000001000000130000001069736d6173746572000100000000' +
'370000000100000001000000010000000000000000000000000000000000000001000000130000001069736d6173746572000100000000' +
'370000000100000001000000010000000000000000000000000000000000000001000000130000001069736d6173746572000100000000',
'hex'
);

it('only reads the last message in the buffer', function (done) {
const inputStream = bufferToStream(data);
const messageStream = new MessageStream();
messageStream.isStreamingProtocol = true;

messageStream.once('message', msg => {
dariakp marked this conversation as resolved.
Show resolved Hide resolved
msg.parse();
expect(msg)
.to.have.property('documents')
.that.deep.equals([{ [LEGACY_HELLO_COMMAND]: 1 }]);
dariakp marked this conversation as resolved.
Show resolved Hide resolved
// Make sure there is nothing left in the buffer.
expect(messageStream.buffer.length).to.equal(0);
done();
});

inputStream.pipe(messageStream);
});
});

describe('reading', function () {
[
{
Expand Down