Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
9,839 additions
and
9,616 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ language: node_js | |
node_js: | ||
- "8" | ||
- "10" | ||
- "12" | ||
services: | ||
- docker | ||
before_install: | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
export interface IBackoffStrategy { | ||
/** | ||
* getDelay returns the amount of delay of the current backoff. | ||
/** | ||
* GetDelay returns the amount of delay of the current backoff. | ||
*/ | ||
getDelay(): number; | ||
getDelay(): number; | ||
|
||
/** | ||
/** | ||
* Next is called when a failure occurs on a host to | ||
* return the next backoff amount. | ||
*/ | ||
next(): IBackoffStrategy; | ||
next(): IBackoffStrategy; | ||
|
||
/** | ||
/** | ||
* Returns a strategy with a reset backoff counter. | ||
*/ | ||
reset(): IBackoffStrategy; | ||
reset(): IBackoffStrategy; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,66 @@ | ||
import { IBackoffStrategy } from './backoff'; | ||
import {IBackoffStrategy} from './backoff'; | ||
|
||
/** | ||
* IExponentialOptions are passed into the ExponentialBackoff constructor. The | ||
* backoff equation is, in general, min(max, initial ^ n), where `n` is | ||
* an incremented backoff factor. The result of the equation is a delay | ||
* given in milliseconds. | ||
* | ||
* @typedef {Object} IExponentialOptions | ||
* @property {Number} initial The initial delay passed to the equation. | ||
* @property {Number} random Random factor to subtract from the `n` count. | ||
* @property {Number} max Max is the maximum value of the delay. | ||
*/ | ||
export interface IExponentialOptions { | ||
/** | ||
/** | ||
* The initial delay passed to the equation. | ||
*/ | ||
initial: number; | ||
initial: number; | ||
|
||
/** | ||
/** | ||
* Random factor to subtract from the `n` count. | ||
*/ | ||
random: number; | ||
random: number; | ||
|
||
/** | ||
* max is the maximum value of the delay. | ||
/** | ||
* Max is the maximum value of the delay. | ||
*/ | ||
max: number; | ||
max: number; | ||
} | ||
|
||
/** | ||
* @class | ||
* @implements {IBackoffStrategy} | ||
* Exponential Backoff | ||
* @see https://en.wikipedia.org/wiki/Exponential_backoff | ||
*/ | ||
export class ExponentialBackoff implements IBackoffStrategy { | ||
private counter: number; | ||
private _counter: number; | ||
|
||
/** | ||
/** | ||
* Creates a new exponential backoff strategy. | ||
* @see https://en.wikipedia.org/wiki/Exponential_backoff | ||
* @param {IExponentialOptions} options | ||
* @param options | ||
*/ | ||
constructor(protected options: IExponentialOptions) { | ||
this.counter = 0; | ||
} | ||
constructor(protected options: IExponentialOptions) { | ||
this._counter = 0; | ||
} | ||
|
||
/** | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public getDelay(): number { | ||
const count = this.counter - Math.round(Math.random() * this.options.random); // tslint:disable-line | ||
return Math.min(this.options.max, this.options.initial * Math.pow(2, Math.max(count, 0))); | ||
} | ||
public getDelay(): number { | ||
const count = this._counter - Math.round(Math.random() * this.options.random); // Tslint:disable-line | ||
return Math.min(this.options.max, this.options.initial * Math.pow(2, Math.max(count, 0))); | ||
} | ||
|
||
/** | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public next(): IBackoffStrategy { | ||
const next = new ExponentialBackoff(this.options); | ||
next.counter = this.counter + 1; | ||
return next; | ||
} | ||
public next(): IBackoffStrategy { | ||
const next = new ExponentialBackoff(this.options); | ||
next._counter = this._counter + 1; | ||
return next; | ||
} | ||
|
||
/** | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public reset(): IBackoffStrategy { | ||
return new ExponentialBackoff(this.options); | ||
} | ||
public reset(): IBackoffStrategy { | ||
return new ExponentialBackoff(this.options); | ||
} | ||
} |
Oops, something went wrong.