-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ability to specify backoff delay calculation function
- Loading branch information
1 parent
93c1b68
commit bddb11d
Showing
10 changed files
with
64 additions
and
53 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,2 +1,2 @@ | ||
export {retryBackoffExponential, RetryBackoffExponentialConfig} from './operators/retryBackoffExponential'; | ||
export {intervalBackoffExponential, IntervalBackoffExponentialConfig} from './observable/intervalBackoffExponential'; | ||
export {retryBackoff, RetryBackoffConfig} from './operators/retryBackoff'; | ||
export {intervalBackoff, IntervalBackoffConfig} from './observable/intervalBackoff'; |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Observable, of, timer } from "rxjs"; | ||
import { expand, mapTo } from "rxjs/operators"; | ||
|
||
import { defaultBackoffDelay, getDelay } from "../utils"; | ||
|
||
export interface IntervalBackoffConfig { | ||
initialInterval: number; | ||
maxInterval?: number; | ||
backoffDelay: (iteration: number, initialInterval: number) => number; | ||
} | ||
/** | ||
* Creates an Observable that emits sequential numbers with by default | ||
* exponentially increasing interval of time. | ||
*/ | ||
export function intervalBackoff( | ||
config: number | IntervalBackoffConfig | ||
): Observable<number> { | ||
const { | ||
initialInterval, | ||
maxInterval = Infinity, | ||
backoffDelay = defaultBackoffDelay | ||
} = | ||
typeof config === "number" ? { initialInterval: config } : config; | ||
return of(0).pipe( | ||
// Expend starts with number 1 and then recursively | ||
// projects each value to new Observable and puts it back in. | ||
expand(iteration => | ||
timer(getDelay(backoffDelay(iteration, initialInterval), maxInterval)) | ||
// Once timer is complete, iteration is increased | ||
.pipe(mapTo(iteration + 1)) | ||
) | ||
); | ||
} |
This file was deleted.
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,5 +1,10 @@ | ||
export function getDelay( | ||
iteration: number, initialInterval: number, maxInterval: number) { | ||
const currentDelay = Math.pow(2, iteration) * initialInterval; | ||
return Math.min(currentDelay, maxInterval); | ||
/** Calculates the actual delay which can be limited by maxInterval */ | ||
export function getDelay(backoffDelay: number, maxInterval: number) { | ||
return Math.min(backoffDelay, maxInterval); | ||
} | ||
|
||
/** Default backoff strategy is exponential delay */ | ||
export function defaultBackoffDelay( | ||
iteration: number, initialInterval: number) { | ||
return Math.pow(2, iteration) * initialInterval; | ||
} |