Instantly share code, notes, and snippets.

Embed
What would you like to do?
Memory leak Node-Nats
var nats = require('../lib/nats').connect();
///////////////////////////////////////
// Publish Performance
//
// Setup:
// NodeJs: 6.9.2, Windows 10
///////////////////////////////////////
var loop = 2000000;
var hash = 2500;
console.log('Publish Performance Test');
nats.on('connect', function() {
var start = new Date();
var invalid2octet = new Buffer('\xc3\x28', 'binary');
for (var i=0; i<loop; i++) {
//nats.publish('test', invalid2octet); //old version
nats.publish('test', invalid2octet, function () {}); //with callback
//nats.publish('test', 'ok');
if (i % hash === 0) {
process.stdout.write('+');
}
}
nats.flush(function() {
var stop = new Date();
var mps = parseInt(loop/((stop-start)/1000));
console.log('\nPublished at ' + mps + ' msgs/sec');
process.exit();
});
});
@derekcollison

This comment has been minimized.

Show comment
Hide comment
@derekcollison

derekcollison Jan 18, 2017

So you are queueing up a stack of functions to be processed when the publish completes. The way this works is that the PUB is sent and followed by a PING. The server eventually sends a PONG which will pull the head from the queue of functions. No messages or PONG responses can be received until the publishing for loop finishes, hence the memory buildup.

You could do a simple flush at the end as is coded which will guarantee that when that callback runs all messages have been received and processed by the server. Otherwise I would recommend the for loop be bound by a small number a controlled with a repeating timer to allow the inbound socket to be processed.

derekcollison commented Jan 18, 2017

So you are queueing up a stack of functions to be processed when the publish completes. The way this works is that the PUB is sent and followed by a PING. The server eventually sends a PONG which will pull the head from the queue of functions. No messages or PONG responses can be received until the publishing for loop finishes, hence the memory buildup.

You could do a simple flush at the end as is coded which will guarantee that when that callback runs all messages have been received and processed by the server. Otherwise I would recommend the for loop be bound by a small number a controlled with a repeating timer to allow the inbound socket to be processed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment