diff --git a/README.md b/README.md index b8fd332..3dad96c 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ var logstash = new Logstash({ port: 13333 }); logstash.send(message [, callback]); -``` +``` ## API @@ -37,6 +37,7 @@ Takes the following parameters: * **host**: remote hostname * **port**: remote port * **format** (optional): formatter function (by default the message gets JSON.stringified) +* **maxQueueSize** (optional): Restricts the amount of messages queued when there is no connection. If not specified the queue is not limited in size. Example: @@ -49,7 +50,8 @@ new Client({ message.formattedAt = new Date(); message.password = '!FILTERED!'; return JSON.stringify(message, null, 2); - } + }, + maxQueueSize: 1000 }); ``` @@ -58,7 +60,7 @@ new Client({ Takes the following parameters: * **message**: an object what you are trying to send to your logstash instance -* **callback** (optional): a function called when the message has been sent +* **callback** (optional): a function called when the message has been sent Example: diff --git a/lib/transports/transport.js b/lib/transports/transport.js index 0721be8..521a9f6 100644 --- a/lib/transports/transport.js +++ b/lib/transports/transport.js @@ -25,6 +25,10 @@ Transport.prototype.send = function send(message, cb) { message: message, cb: cb }); + + if(this.queue.length > this.options.maxQueueSize) { + this.queue.shift(); + } }; Transport.prototype._send = function _send() { @@ -39,4 +43,4 @@ Transport.prototype.dequeue = function dequeue() { } }; -module.exports = Transport; \ No newline at end of file +module.exports = Transport; diff --git a/test/transports.js b/test/transports.js index b2f99c9..7beb13f 100644 --- a/test/transports.js +++ b/test/transports.js @@ -9,6 +9,25 @@ test('Offline support for transport', function(t) { t.end(); }); +test('Maximum queue size when offline', function(t) { + var transport = new (Transports.Transport)({maxQueueSize: 1}); + transport.send('foo'); + transport.send('foo1'); + transport.send('foo2'); + t.equal(transport.queue.length, 1, 'should not exceed maxQueueSize'); + t.equal(transport.queue[0].message, 'foo2', 'should store message'); + t.end(); +}); + +test('Unlimited queue size when offline', function(t) { + var transport = new (Transports.Transport)(); + for(var i = 0; i < 10000; i++) { + transport.send('foo'); + } + t.equal(transport.queue.length, 10000, 'should include all messages'); + t.end(); +}); + test('Send messages if transport connected', function(t) { var transport = new (Transports.Transport)(); transport.connected = true; @@ -18,4 +37,4 @@ test('Send messages if transport connected', function(t) { }; transport.send('foo-bar'); -}); \ No newline at end of file +});