From 6cdca9275068a5193e896274c86c688250150f4c Mon Sep 17 00:00:00 2001 From: Safa Topal Date: Fri, 9 Sep 2016 14:16:53 +0100 Subject: [PATCH] conn.write callback for buffer drain event (#132) * conn.write callback for buffer drain event * version bump --- README.md | 11 +++++------ package.json | 2 +- src/logger.js | 22 ++++++++++++++++------ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 2cb5ff6..88276c6 100644 --- a/README.md +++ b/README.md @@ -187,7 +187,7 @@ These are events inherited from `Writable`. DEPRECATED. Use `buffer drain` event instead. #### `'buffer drain'` -This event is emitted when the underlying ring buffer is fully consumed and becomes empty. +This event is emitted when the underlying ring buffer is fully consumed and Socket.write callback called. This can be useful when it’s time for the application to terminate but you want to be sure any pending logs have finished writing. @@ -195,11 +195,10 @@ to be sure any pending logs have finished writing. process.on('SIGINT', () => { logger.notice({ type: 'server', event: 'shutdown' }); logger.once('buffer drain', () => { - logger.end(() => { - setTimeout(() => { - process.exit(0); - }, 2000); // this delay gives sometime for the underlying connection to flush its queue - }); + logger.closeConnection(); + logger.on('disconnected', () => { + process.exit(); + }); }); }); ``` diff --git a/package.json b/package.json index 5596cef..e804814 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "le_node", - "version": "1.6.4", + "version": "1.6.5", "description": "Logentries client for node; use with or without Winston or Bunyan.", "keywords": [ "logentries", diff --git a/src/logger.js b/src/logger.js index 3f3d8de..3bbd638 100644 --- a/src/logger.js +++ b/src/logger.js @@ -166,6 +166,10 @@ class Logger extends Writable { this.ringBuffer.on('buffer shift', () => { this.debugLogger.log('Buffer is full, will be shifting records until buffer is drained.'); }); + + this.on('buffer drain', () => { + this.debugLogger.log('RingBuffer drained.'); + }); } /** @@ -177,14 +181,19 @@ class Logger extends Writable { this.connection.then(conn => { const record = this.ringBuffer.read(); if (record) { - conn.write(record); // we are checking the buffer state here just after conn.write() // to make sure the last event is sent to socket. if (this.ringBuffer.isEmpty()) { - this.emit('buffer drain'); - // this event is DEPRECATED - will be removed in next major release. - // new users should use 'buffer drain' event instead. - this.emit('connection drain'); + conn.write(record, () => { + process.nextTick(() => { + this.emit('buffer drain'); + // this event is DEPRECATED - will be removed in next major release. + // new users should use 'buffer drain' event instead. + this.emit('connection drain'); + }); + }); + } else { + conn.write(record); } } else { this.debugLogger.log('This should not happen. Read from ringBuffer returned null.'); @@ -309,6 +318,7 @@ class Logger extends Writable { closeConnection() { this.debugLogger.log('Closing retry mechanism along with its connection.'); if (!this.reconnection) { + this.debugLogger.log('No reconnection instance found. Returning.'); return; } // this makes sure retry mechanism and connection will be closed. @@ -373,7 +383,6 @@ class Logger extends Writable { this.reconnection.on('connect', (connection) => { this.debugLogger.log('Connected'); this.emit('connected'); - resolve(connection); // connection listeners connection.on('timeout', () => { @@ -385,6 +394,7 @@ class Logger extends Writable { this.connection = null; this.emit('timed out'); }); + resolve(connection); }); this.reconnection.on('reconnect', (n, delay) => {