Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid open file handle limit by saving writes #15

Merged
merged 8 commits into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions lib/winston-logrotate.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ var Rotate = exports.Rotate = function (options) {

this.formatter = options.formatter;

this.state = 'uninitialized'
this.stringify = options.stringify || DEFAULT_STRINGIFY;

this.ready = false;
};

//
Expand All @@ -66,6 +65,9 @@ Rotate.prototype.name = 'rotate';
// #### @callback {function} Continuation to respond to when complete.
// Core logging method exposed to Winston. Metadata is optional.
//

var pendingWrites = {}

Rotate.prototype.log = function (level, msg, meta, callback) {
var self = this;

Expand All @@ -82,14 +84,16 @@ Rotate.prototype.log = function (level, msg, meta, callback) {
label: this.label
});

if (this.ready) {
this._write(level, output, callback);
if (this.state == 'uninitialized') {
this.state = 'initializing';
pendingWrites[this.file] = [{ level: level, output: output, callback: callback }];
this._init();
} else {
this._init().done(function () {
self._write(level, output, callback);
}, function (err) {
throw err;
});
if (this.state == 'initializing') {
pendingWrites[this.file].push({ level: level, output: output, callback: callback });
} else {
self._write(level, output, callback);
}
}
};

Expand All @@ -103,7 +107,6 @@ Rotate.prototype.close = function() {

Rotate.prototype._write = function (level, output, callback) {
var self = this;

this.log_stream.write(output + '\n', function () {
self.emit('logged');
callback(null, true);
Expand Down Expand Up @@ -132,8 +135,17 @@ Rotate.prototype._configure_log_stream = function (resolve, reject) {
});

log_stream.on('ready', function () {
self.ready = true;
self.state = 'ready'
self.emit('ready', log_stream);

if (self.file in pendingWrites) {
for (var i = 0; i < pendingWrites[self.file].length; ++i) {
var pendingWrite = pendingWrites[self.file][i]
self._write(pendingWrite.level, pendingWrite.output, pendingWrite.callback);
}
delete pendingWrites[self.file]
}

resolve();
});

Expand Down
16 changes: 16 additions & 0 deletions test/winston-logrotate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,20 @@ describe('winston logrotate tests', function () {

recursive_log(6);
});

it('max file handles', function(done) {
this.timeout(10000)
var temp_dir = temp.mkdirSync('winston-logrotate-test');
var file = path.join(temp_dir, 'rotate.log');
var logger = new Rotate({
file: file,
size: '1k',
keep: 1
});

for (var i = 0; i < 100000; ++ i) {
logger.log('max log test');
}
done();
})
});