Skip to content

Commit 8433b1a

Browse files
Trottaddaleax
authored andcommitted
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: #14681 Refs: #14674 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 4e15a6b commit 8433b1a

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

lib/readline.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const {
4646
kClearScreenDown
4747
} = CSI;
4848

49+
const now = process.binding('timer_wrap').Timer.now;
50+
4951
const kHistorySize = 30;
5052
const kMincrlfDelay = 100;
5153
// \r\n, \n, or \r followed by something other than \n
@@ -395,7 +397,7 @@ Interface.prototype._normalWrite = function(b) {
395397
}
396398
var string = this._decoder.write(b);
397399
if (this._sawReturnAt &&
398-
Date.now() - this._sawReturnAt <= this.crlfDelay) {
400+
now() - this._sawReturnAt <= this.crlfDelay) {
399401
string = string.replace(/^\n/, '');
400402
this._sawReturnAt = 0;
401403
}
@@ -408,7 +410,7 @@ Interface.prototype._normalWrite = function(b) {
408410
this._line_buffer = null;
409411
}
410412
if (newPartContainsEnding) {
411-
this._sawReturnAt = string.endsWith('\r') ? Date.now() : 0;
413+
this._sawReturnAt = string.endsWith('\r') ? now() : 0;
412414

413415
// got one or more newlines; process into "line" events
414416
var lines = string.split(lineEnding);
@@ -900,14 +902,14 @@ Interface.prototype._ttyWrite = function(s, key) {
900902

901903
switch (key.name) {
902904
case 'return': // carriage return, i.e. \r
903-
this._sawReturnAt = Date.now();
905+
this._sawReturnAt = now();
904906
this._line();
905907
break;
906908

907909
case 'enter':
908910
// When key interval > crlfDelay
909911
if (this._sawReturnAt === 0 ||
910-
Date.now() - this._sawReturnAt > this.crlfDelay) {
912+
now() - this._sawReturnAt > this.crlfDelay) {
911913
this._line();
912914
}
913915
this._sawReturnAt = 0;

0 commit comments

Comments
 (0)