Browse Source

lib: use Timer.now() in readline module

Using Date.now() introduces problems when operating under load or
otherwise with constrained resources. Use Timer.now() to mitigate.

The problem was identified in `test-readline-interface` where under
heavy load, `\r` and `\n` were received so far apart that they were
treated as separate line endings rather than a single line ending.
Switching to `Timer.now()` prevented this from happening.

PR-URL: https://github.com/nodejs/node/pull/14681
Refs: https://github.com/nodejs/node/issues/14674
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
v6
Rich Trott 8 years ago
parent
commit
fe54bc7b69
  1. 10
      lib/readline.js

10
lib/readline.js

@ -48,6 +48,8 @@ const {
kClearScreenDown
} = CSI;
const now = process.binding('timer_wrap').Timer.now;
const kHistorySize = 30;
const kMincrlfDelay = 100;
// \r\n, \n, or \r followed by something other than \n
@ -409,7 +411,7 @@ Interface.prototype._normalWrite = function(b) {
}
var string = this._decoder.write(b);
if (this._sawReturnAt &&
Date.now() - this._sawReturnAt <= this.crlfDelay) {
now() - this._sawReturnAt <= this.crlfDelay) {
string = string.replace(/^\n/, '');
this._sawReturnAt = 0;
}
@ -422,7 +424,7 @@ Interface.prototype._normalWrite = function(b) {
this._line_buffer = null;
}
if (newPartContainsEnding) {
this._sawReturnAt = string.endsWith('\r') ? Date.now() : 0;
this._sawReturnAt = string.endsWith('\r') ? now() : 0;
// got one or more newlines; process into "line" events
var lines = string.split(lineEnding);
@ -916,14 +918,14 @@ Interface.prototype._ttyWrite = function(s, key) {
switch (key.name) {
case 'return': // carriage return, i.e. \r
this._sawReturnAt = Date.now();
this._sawReturnAt = now();
this._line();
break;
case 'enter':
// When key interval > crlfDelay
if (this._sawReturnAt === 0 ||
Date.now() - this._sawReturnAt > this.crlfDelay) {
now() - this._sawReturnAt > this.crlfDelay) {
this._line();
}
this._sawReturnAt = 0;

Loading…
Cancel
Save