Skip to content

Commit fe54bc7

Browse files
committed
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: nodejs#14681 Refs: nodejs#14674 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 02371c7 commit fe54bc7

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
@@ -48,6 +48,8 @@ const {
4848
kClearScreenDown
4949
} = CSI;
5050

51+
const now = process.binding('timer_wrap').Timer.now;
52+
5153
const kHistorySize = 30;
5254
const kMincrlfDelay = 100;
5355
// \r\n, \n, or \r followed by something other than \n
@@ -409,7 +411,7 @@ Interface.prototype._normalWrite = function(b) {
409411
}
410412
var string = this._decoder.write(b);
411413
if (this._sawReturnAt &&
412-
Date.now() - this._sawReturnAt <= this.crlfDelay) {
414+
now() - this._sawReturnAt <= this.crlfDelay) {
413415
string = string.replace(/^\n/, '');
414416
this._sawReturnAt = 0;
415417
}
@@ -422,7 +424,7 @@ Interface.prototype._normalWrite = function(b) {
422424
this._line_buffer = null;
423425
}
424426
if (newPartContainsEnding) {
425-
this._sawReturnAt = string.endsWith('\r') ? Date.now() : 0;
427+
this._sawReturnAt = string.endsWith('\r') ? now() : 0;
426428

427429
// got one or more newlines; process into "line" events
428430
var lines = string.split(lineEnding);
@@ -916,14 +918,14 @@ Interface.prototype._ttyWrite = function(s, key) {
916918

917919
switch (key.name) {
918920
case 'return': // carriage return, i.e. \r
919-
this._sawReturnAt = Date.now();
921+
this._sawReturnAt = now();
920922
this._line();
921923
break;
922924

923925
case 'enter':
924926
// When key interval > crlfDelay
925927
if (this._sawReturnAt === 0 ||
926-
Date.now() - this._sawReturnAt > this.crlfDelay) {
928+
now() - this._sawReturnAt > this.crlfDelay) {
927929
this._line();
928930
}
929931
this._sawReturnAt = 0;

0 commit comments

Comments
 (0)