diff options
author | hackademix | 2019-05-28 01:32:09 +0200 |
---|---|---|
committer | hackademix | 2019-05-28 01:35:44 +0200 |
commit | cd44c749f42c4fdb5b152d6039b39805f32f74ac (patch) | |
tree | bbd17181f9a92d23c3d999488e9ad35ff571fb90 /src | |
parent | 4d4fa3c6ed55469753a61d35e2112750984c2044 (diff) | |
download | noscript-cd44c749f42c4fdb5b152d6039b39805f32f74ac.tar.gz noscript-cd44c749f42c4fdb5b152d6039b39805f32f74ac.tar.xz noscript-cd44c749f42c4fdb5b152d6039b39805f32f74ac.zip |
Timing cap inferenced by call numbers when using low-resolution timers.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/Timing.js | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/src/lib/Timing.js b/src/lib/Timing.js index 5d09e3a..997ebd3 100644 --- a/src/lib/Timing.js +++ b/src/lib/Timing.js @@ -1,11 +1,12 @@ class Timing { - constructor(workSlot = 4, longTime = 20000, pauseTime = 20) { + constructor(workSlot = 10, longTime = 20000, pauseTime = 20) { this.workSlot = workSlot; this.longTime = longTime; this.pauseTime = pauseTime; this.interrupted = false; this.fatalTimeout = false; + this.maxCalls = 1000; this.reset(); } @@ -16,12 +17,20 @@ class Timing { async pause() { if (this.interrupted) throw new TimingException("Interrupted"); let now = Date.now(); + this.calls++; + let sinceLastCall = now - this.lastCall; + if (sinceLastCall > this.workSlot && this.calls > 1000) { + // low resolution (100ms) timer? Let's cap approximating by calls number + this.maxCalls = this.calls / sinceLastCall * this.workSlot; + } + this.lastCall = now; this.elapsed = now - this.timeOrigin; - if (now - this.lastPause > this.workSlot) { + if (now - this.lastPause > this.workSlot || this.calls > this.maxCalls) { this.tooLong = this.elapsed >= this.longTime; if (this.tooLong && this.fatalTimeout) { throw new TimingException(`Exceeded ${this.longTime}ms timeout`); } + this.calls = 0; await Timing.sleep(this.pauseTime); this.lastPause = Date.now(); return true; @@ -31,7 +40,8 @@ class Timing { reset() { this.elapsed = 0; - this.timeOrigin = this.lastPause = Date.now(); + this.calls = 0; + this.timeOrigin = this.lastPause = this.lastCall = Date.now(); this.tooLong = false; } } |