diff options
author | zhangzifa <tonyzzf@163.com> | 2017-08-14 09:59:15 +0800 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-01-12 17:44:42 +0100 |
commit | 1385e1bc63665b8c226e9f7bc8c46a9263195efc (patch) | |
tree | 64ff024620902398975bcb88ab46eede9fc2c1b2 /lib | |
parent | e8c491a801191bd6e6244195237adc377a4f755f (diff) | |
download | android-node-v8-1385e1bc63665b8c226e9f7bc8c46a9263195efc.tar.gz android-node-v8-1385e1bc63665b8c226e9f7bc8c46a9263195efc.tar.bz2 android-node-v8-1385e1bc63665b8c226e9f7bc8c46a9263195efc.zip |
timers: setInterval interval includes cb duration
setInterval callback should be scheduled on the interval
Fixes: https://github.com/nodejs/node/issues/7346
PR-URL: https://github.com/nodejs/node/pull/14815
Fixes: https://github.com/nodejs/node/issues/7346
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/timers.js | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/lib/timers.js b/lib/timers.js index 643731b105..8a74bcf2de 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -167,11 +167,15 @@ exports._unrefActive = function(item) { // Appends a timer onto the end of an existing timers list, or creates a new // TimerWrap backed list if one does not already exist for the specified timeout // duration. -function insert(item, unrefed) { +function insert(item, unrefed, start) { const msecs = item._idleTimeout; if (msecs < 0 || msecs === undefined) return; - item._idleStart = TimerWrap.now(); + if (typeof start === 'number') { + item._idleStart = start; + } else { + item._idleStart = TimerWrap.now(); + } const lists = unrefed === true ? unrefedLists : refedLists; @@ -446,16 +450,17 @@ function ontimeout(timer) { var args = timer._timerArgs; if (typeof timer._onTimeout !== 'function') return promiseResolve(timer._onTimeout, args[0]); + const start = TimerWrap.now(); if (!args) timer._onTimeout(); else Reflect.apply(timer._onTimeout, timer, args); if (timer._repeat) - rearm(timer); + rearm(timer, start); } -function rearm(timer) { +function rearm(timer, start) { // // Do not re-arm unenroll'd or closed timers. if (timer._idleTimeout === -1) return; @@ -464,7 +469,15 @@ function rearm(timer) { timer._handle.start(timer._repeat); } else { timer._idleTimeout = timer._repeat; - active(timer); + + const duration = TimerWrap.now() - start; + if (duration >= timer._repeat) { + // If callback duration >= timer._repeat, + // add 1 ms to avoid blocking eventloop + insert(timer, false, start + duration - timer._repeat + 1); + } else { + insert(timer, false, start); + } } } |