Skip to content

Instantly share code, notes, and snippets.

@MrMjauh
Last active June 3, 2019 17:53
Show Gist options
  • Save MrMjauh/e237ae1adf6f97516d6b0c73d7a0b6a4 to your computer and use it in GitHub Desktop.
Save MrMjauh/e237ae1adf6f97516d6b0c73d7a0b6a4 to your computer and use it in GitHub Desktop.
Backoff strategy
/**
* 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