Skip to content

Commit

Permalink
Revert "fixes #7 * notify RateLimits about idle RateLimiter (on pause…
Browse files Browse the repository at this point in the history
…/unpause) and have them stop timers if no connected RateLimiters are active"

This reverts commit 13c0c75

Seems to have introduced updating bugs
fixes #9
reopen #7
  • Loading branch information
Colorfulstan committed Dec 4, 2017
1 parent 0933611 commit f1e34ae
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 99 deletions.
3 changes: 0 additions & 3 deletions dist/RateLimit/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,5 @@ export declare class RateLimit implements Comparable, RateLimitOptions {
static compare(limit1: RateLimitOptions, limit2: RateLimitOptions): number;
compareTo(comparable: RateLimitOptions): number;
equals(limit: RateLimitOptions): boolean;
notifyAboutIdle(isIdle: any): void;
stopTimer(): void;
restartTimeout(): void;
private hasUnpausedLimiters();
}
37 changes: 3 additions & 34 deletions dist/RateLimit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,7 @@ class RateLimit {
if (this.debug && !this.check(index_1.STRATEGY.BURST)) {
console.log('starting resetTimeout for exceeded limit' + this._seconds * 1000, this.toString());
}
const secondsSinceLastReset = (Date.now() - this.timestampLastReset) / 1000;
let secondsLeftInLimitWindow = this.seconds - secondsSinceLastReset;
if (secondsLeftInLimitWindow < 0) {
secondsLeftInLimitWindow = this.seconds;
}
this.resetTimeout = setTimeout(() => { this.reset(); }, secondsLeftInLimitWindow * 1000);
this.resetTimeout = setTimeout(() => { this.reset(); }, this._seconds * 1000);
}
}
notifyLimiters() {
Expand All @@ -183,36 +178,10 @@ class RateLimit {
}
return this.compareTo(limit) === 0;
}
notifyAboutIdle(isIdle) {
if (isIdle && !this.hasUnpausedLimiters()) {
this.stopTimer();
}
if (!isIdle) {
this.startResetTimer();
}
}
stopTimer() {
restartTimeout() {
clearTimeout(this.resetTimeout);
this.resetTimeout = null;
}
restartTimeout() {
this.stopTimer();
if (this.hasUnpausedLimiters()) {
this.startResetTimer();
}
else {
if (this.debug) {
console.log('RateLimit "' + this.toString() + '" does not have any active RateLimiters attached anymore,' +
' keeping it paused');
}
}
}
hasUnpausedLimiters() {
let hasActiveLimiters = false;
this.limiters.forEach(limiter => {
hasActiveLimiters = hasActiveLimiters || !limiter.isIdle;
});
return hasActiveLimiters;
this.startResetTimer();
}
}
exports.RateLimit = RateLimit;
2 changes: 0 additions & 2 deletions dist/RateLimiter/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export declare class RateLimiter {
private strategy;
private limits;
private _isPaused;
readonly isIdle: boolean;
constructor({limits, strategy, debug}: RateLimiterOptions);
addOrUpdateLimit(limit: RateLimit): RateLimit;
removeLimit(limit: RateLimit): RateLimit;
Expand Down Expand Up @@ -72,7 +71,6 @@ export declare class RateLimiter {
reject: (reason?: any) => void;
}[];
private unpause();
private notifyLimitsAboutIdle(isIdle);
private getSpreadInterval();
isInitializing(): boolean;
static createSyncRateLimit(debug?: boolean): RateLimit;
Expand Down
10 changes: 0 additions & 10 deletions dist/RateLimiter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ class RateLimiter {
this.debug = debug;
limits.forEach(limit => limit.addLimiter(this));
}
get isIdle() {
return this.getQueueSize() === 0;
}
addOrUpdateLimit(limit) {
if (this.debug && limit.type === RateLimit_1.RATELIMIT_TYPE.BACKOFF || limit.type === RateLimit_1.RATELIMIT_TYPE.SYNC) {
console.log('adding ' + RateLimit_1.RATELIMIT_TYPE_STRINGS[limit.type] + ' limit', limit.toString());
Expand Down Expand Up @@ -169,7 +166,6 @@ class RateLimiter {
}
this._isPaused = true;
this.clearTimeoutAndInterval();
this.notifyLimitsAboutIdle(true);
}
setStrategy(strategy) {
this.strategy = strategy;
Expand Down Expand Up @@ -332,14 +328,8 @@ class RateLimiter {
console.log('unpausing limiter ' + this.toString());
}
this._isPaused = false;
this.notifyLimitsAboutIdle(this.isIdle);
this.refresh();
}
notifyLimitsAboutIdle(isIdle) {
this.limits.forEach(limit => {
limit.notifyAboutIdle(isIdle);
});
}
getSpreadInterval() {
return this.limits.reduce((longestInterval, limit) => {
const interval = limit.getSpreadInterval();
Expand Down
43 changes: 4 additions & 39 deletions src/RateLimit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,8 @@ export class RateLimit implements Comparable, RateLimitOptions {
if (this.debug && !this.check(STRATEGY.BURST)) {
console.log('starting resetTimeout for exceeded limit' + this._seconds * 1000, this.toString())
}

const secondsSinceLastReset = (Date.now() - this.timestampLastReset) / 1000
let secondsLeftInLimitWindow = this.seconds - secondsSinceLastReset
if (secondsLeftInLimitWindow < 0) {
secondsLeftInLimitWindow = this.seconds
}

// NOTE: using timeout since interval is not testable for some reason with node-ts and sinon!?
this.resetTimeout = setTimeout(() => {this.reset()}, secondsLeftInLimitWindow * 1000)
this.resetTimeout = setTimeout(() => {this.reset()}, this._seconds * 1000)
}
}

Expand Down Expand Up @@ -260,37 +253,9 @@ export class RateLimit implements Comparable, RateLimitOptions {
return this.compareTo(limit) === 0
}

notifyAboutIdle(isIdle) {
if (isIdle && !this.hasUnpausedLimiters()) {
this.stopTimer()
}
if (!isIdle) {
this.startResetTimer()
}
}

stopTimer() {
clearTimeout(this.resetTimeout);
this.resetTimeout = null;
}

restartTimeout() {
this.stopTimer()
if (this.hasUnpausedLimiters()) {
this.startResetTimer();
} else {
if (this.debug) {
console.log('RateLimit "' + this.toString() + '" does not have any active RateLimiters attached anymore,' +
' keeping it paused')
}
}
}

private hasUnpausedLimiters() {
let hasActiveLimiters = false
this.limiters.forEach(limiter => {
hasActiveLimiters = hasActiveLimiters || !limiter.isIdle
})
return hasActiveLimiters
clearTimeout(this.resetTimeout)
this.resetTimeout = null
this.startResetTimer()
}
}
11 changes: 0 additions & 11 deletions src/RateLimiter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ export class RateLimiter {
* A paused limiter will not execute any queued items and newly scheduled items will be added to the queue directly.
*/
private _isPaused: boolean;
public get isIdle(){
return this.getQueueSize() === 0
}

constructor({limits, strategy = RateLimiter.STRATEGY.BURST, debug = false}: RateLimiterOptions) {
if (!limits || !Array.isArray(limits) || limits.length === 0) {
Expand Down Expand Up @@ -253,8 +250,6 @@ export class RateLimiter {

this._isPaused = true
this.clearTimeoutAndInterval()
this.notifyLimitsAboutIdle(true)

}

/**
Expand Down Expand Up @@ -485,14 +480,8 @@ export class RateLimiter {
}

this._isPaused = false
this.notifyLimitsAboutIdle(this.isIdle)
this.refresh()
}
private notifyLimitsAboutIdle(isIdle) {
this.limits.forEach(limit => {
limit.notifyAboutIdle(isIdle)
})
}

private getSpreadInterval(): number {
return this.limits.reduce((longestInterval: number, limit: RateLimit) => {
Expand Down

0 comments on commit f1e34ae

Please sign in to comment.