Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Commit

Permalink
conn.write callback for buffer drain event (#132)
Browse files Browse the repository at this point in the history
* conn.write callback for buffer drain event

* version bump
  • Loading branch information
stopal-r7 authored Sep 9, 2016
1 parent ca69a94 commit 6cdca92
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,18 @@ 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.

```javascript
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();
});
});
});
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
22 changes: 16 additions & 6 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
});
}

/**
Expand All @@ -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.');
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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', () => {
Expand All @@ -385,6 +394,7 @@ class Logger extends Writable {
this.connection = null;
this.emit('timed out');
});
resolve(connection);
});

this.reconnection.on('reconnect', (n, delay) => {
Expand Down

0 comments on commit 6cdca92

Please sign in to comment.