From 154eb8a513b6b0720fc38260936391f37449f443 Mon Sep 17 00:00:00 2001 From: Mingwei Samuel Date: Wed, 19 Dec 2018 10:24:02 -0800 Subject: [PATCH] add unref to setTimeout to prevent hanging --- dist/RateLimit/index.js | 1 + dist/RateLimiter/index.js | 2 ++ dist/RiotRateLimiter/index.js | 4 +++- src/RateLimit/index.ts | 1 + src/RateLimiter/index.ts | 2 ++ 5 files changed, 9 insertions(+), 1 deletion(-) diff --git a/dist/RateLimit/index.js b/dist/RateLimit/index.js index 6618dae..f1ee1a5 100644 --- a/dist/RateLimit/index.js +++ b/dist/RateLimit/index.js @@ -154,6 +154,7 @@ class RateLimit { console.log('starting resetTimeout for exceeded limit' + this._seconds * 1000, this.toString()); } this.resetTimeout = setTimeout(() => { this.reset(); }, this._seconds * 1000); + this.resetTimeout.unref(); } } notifyLimiters() { diff --git a/dist/RateLimiter/index.js b/dist/RateLimiter/index.js index 1e9fb4c..4ba1bda 100644 --- a/dist/RateLimiter/index.js +++ b/dist/RateLimiter/index.js @@ -274,6 +274,7 @@ class RateLimiter { if (this.queue.length !== 0) { const factorForEqualRights = Math.floor(Math.random() * 100); this.intervalProcessQueue = setInterval(() => { this.processBurstQueue(); }, 1000 + factorForEqualRights); + this.intervalProcessQueue.unref(); } } refreshSpreadLimiter() { @@ -281,6 +282,7 @@ class RateLimiter { this.intervalNextSpreadExecution = setInterval(() => { this.processSpreadLimitInterval(); }, this.getSpreadInterval()); + this.intervalNextSpreadExecution.unref(); } execute(fn, onSuccess, onError) { try { diff --git a/dist/RiotRateLimiter/index.js b/dist/RiotRateLimiter/index.js index 6307d9e..36bad68 100644 --- a/dist/RiotRateLimiter/index.js +++ b/dist/RiotRateLimiter/index.js @@ -17,7 +17,9 @@ class RiotRateLimiter { this.limitersPerPlatformId[platformId] = {}; } if (!this.limitersPerPlatformId[platformId][apiMethod]) { - console.log('creating sync rate limimter for ', platformId, apiMethod); + if (this.debug) { + console.log('creating sync rate limimter for ', platformId, apiMethod); + } this.limitersPerPlatformId[platformId][apiMethod] = new RateLimiter_1.RateLimiter({ limits: [RateLimiter_1.RateLimiter.createSyncRateLimit(this.debug)], strategy: this.strategy, diff --git a/src/RateLimit/index.ts b/src/RateLimit/index.ts index 71d97cd..05977a4 100644 --- a/src/RateLimit/index.ts +++ b/src/RateLimit/index.ts @@ -222,6 +222,7 @@ export class RateLimit implements Comparable, RateLimitOptions { } // NOTE: using timeout since interval is not testable for some reason with node-ts and sinon!? this.resetTimeout = setTimeout(() => {this.reset()}, this._seconds * 1000) + this.resetTimeout.unref() } } diff --git a/src/RateLimiter/index.ts b/src/RateLimiter/index.ts index 0c25520..3a878e4 100644 --- a/src/RateLimiter/index.ts +++ b/src/RateLimiter/index.ts @@ -403,6 +403,7 @@ export class RateLimiter { // rare case propably const factorForEqualRights = Math.floor(Math.random() * 100) this.intervalProcessQueue = setInterval(() => {this.processBurstQueue()}, 1000 + factorForEqualRights) + this.intervalProcessQueue.unref() } } @@ -412,6 +413,7 @@ export class RateLimiter { this.intervalNextSpreadExecution = setInterval(() => { this.processSpreadLimitInterval() }, this.getSpreadInterval()) + this.intervalNextSpreadExecution.unref() } /**