Last active
June 3, 2019 17:53
-
-
Save MrMjauh/e237ae1adf6f97516d6b0c73d7a0b6a4 to your computer and use it in GitHub Desktop.
Backoff strategy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Linear backoff strategy | |
*/ | |
export class Backoff { | |
private timeMillisToSleep: number; | |
private start: number; | |
private max: number; | |
private step: number; | |
constructor(start: number, max: number, stepSize: number = 25) { | |
this.timeMillisToSleep = start; | |
this.start = start; | |
this.max = max; | |
this.step = (max - start) / stepSize; | |
} | |
public reset(): void { | |
this.timeMillisToSleep = this.start; | |
} | |
public backoff(): void { | |
this.timeMillisToSleep = Math.min(this.timeMillisToSleep + this.step, this.max); | |
} | |
public getSleepTime(): number { | |
return this.timeMillisToSleep; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment