Skip to content

Commit

Permalink
Greatly simplify drain mechanism based on PR feedback and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Ancill committed May 21, 2018
1 parent 0e4d149 commit 7ff756b
Showing 1 changed file with 7 additions and 28 deletions.
35 changes: 7 additions & 28 deletions lib/winston/transports/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ const debug = require('diagnostics')('winston:file');
const os = require('os');
const tailFile = require('../tail-file');

/**
* Simple no-op function.
* @returns {undefined}
*/
function noop() {}

/**
* Transport for outputting to a local log file.
* @type {File}
Expand Down Expand Up @@ -88,8 +82,6 @@ module.exports = class File extends TransportStream {
this._size = 0;
this._pendingSize = 0;
this._created = 0;
this._drain = false;
this._next = noop;
this._opening = false;

this.open();
Expand All @@ -101,7 +93,7 @@ module.exports = class File extends TransportStream {
* @param {Function} callback - TODO: add param description.
* @returns {undefined}
*/
log(info, callback = noop) {
log(info, callback = () => {}) {
// Remark: (jcrugzz) What is necessary about this callback(null, true) now
// when thinking about 3.x? Should silent be handled in the base
// TransportStream _write method?
Expand Down Expand Up @@ -142,23 +134,6 @@ module.exports = class File extends TransportStream {
this._endStream(() => this._rotateFile());
}

if (this._drain) {
this._stream.once('drain', () => {
this._drain = false;
this.log(info, callback);
});
return;
}

const written = this._stream.write(output, logged.bind(this));
if (written === false) {
this._drain = true;
this._stream.once('drain', () => {
this._drain = false;
callback();
});
}

// Keep track of the pending bytes being written while files are opening
// in order to properly rotate the PassThrough this._stream when the file
// eventually does open.
Expand All @@ -169,11 +144,15 @@ module.exports = class File extends TransportStream {
this.rotatedWhileOpening = true;
}

debug('written', written, this._drain);
if (written) {
const written = this._stream.write(output, logged.bind(this));
if (!written) {
this._stream.once('drain', callback);
} else {
callback(); // eslint-disable-line callback-return
}

debug('written', written);

return written;
}

Expand Down

0 comments on commit 7ff756b

Please sign in to comment.