-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #185.
- Loading branch information
Showing
11 changed files
with
403 additions
and
57 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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export { TimeoutException } from './policies/proactive/timeoutPolicy/timeoutException'; | ||
export { TimeoutPolicy } from './policies/proactive/timeoutPolicy/timeoutPolicy'; | ||
export { FallbackChainExhaustedException } from './policies/reactive/fallbackPolicy/fallbackChainExhaustedException'; | ||
export { FallbackPolicy } from './policies/reactive/fallbackPolicy/fallbackPolicy'; | ||
export { BackoffStrategyFactory } from './policies/reactive/retryPolicy/backoffStrategyFactory'; | ||
export { RetryPolicy } from './policies/reactive/retryPolicy/retryPolicy'; |
1 change: 1 addition & 0 deletions
1
src/policies/reactive/fallbackPolicy/fallbackChainExhaustedException.ts
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 @@ | ||
export class FallbackChainExhaustedException extends Error {} |
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 @@ | ||
export type FallbackChainLink<ResultType> = () => ResultType | Promise<ResultType>; |
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,112 @@ | ||
import { OnFinallyFn } from '../../../types/onFinallyFn'; | ||
import { ReactivePolicy } from '../reactivePolicy'; | ||
import { FallbackChainExhaustedException } from './fallbackChainExhaustedException'; | ||
import { FallbackChainLink } from './fallbackChainLink'; | ||
import { OnFallbackFn } from './onFallbackFn'; | ||
|
||
export class FallbackPolicy<ResultType> extends ReactivePolicy<ResultType> { | ||
private readonly fallbackChain: Array<FallbackChainLink<ResultType>> = []; | ||
private readonly onFallbackFns: Array<OnFallbackFn<ResultType>> = []; | ||
private readonly onFinallyFns: OnFinallyFn[] = []; | ||
private executing = 0; | ||
|
||
public fallback(fallbackChainLink: FallbackChainLink<ResultType>): void { | ||
if (this.executing > 0) { | ||
throw new Error('cannot modify policy during execution'); | ||
} | ||
|
||
this.fallbackChain.push(fallbackChainLink); | ||
} | ||
|
||
public onFallback(onFallbackFn: OnFallbackFn<ResultType>): void { | ||
if (this.executing > 0) { | ||
throw new Error('cannot modify policy during execution'); | ||
} | ||
|
||
this.onFallbackFns.push(onFallbackFn); | ||
} | ||
|
||
public onFinally(fn: OnFinallyFn): void { | ||
if (this.executing > 0) { | ||
throw new Error('cannot modify policy during execution'); | ||
} | ||
|
||
this.onFinallyFns.push(fn); | ||
} | ||
|
||
public async execute(fn: () => ResultType | Promise<ResultType>): Promise<ResultType> { | ||
try { | ||
this.executing++; | ||
|
||
const remainingFallbackChain = [...this.fallbackChain]; | ||
let executor = fn; | ||
|
||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
try { | ||
const result = await executor(); | ||
|
||
const shouldFallbackOnResult = await this.isResultHandled(result); | ||
if (!shouldFallbackOnResult) { | ||
return result; | ||
} | ||
|
||
const nextExecutor = remainingFallbackChain.shift(); | ||
if (nextExecutor === undefined) { | ||
throw new FallbackChainExhaustedException(); | ||
} | ||
|
||
executor = nextExecutor; | ||
|
||
for (const onFallbackFn of this.onFallbackFns) { | ||
try { | ||
await onFallbackFn(result, undefined); | ||
} catch (onFallbackError) { | ||
// ignored | ||
} | ||
} | ||
|
||
continue; | ||
} catch (ex) { | ||
if (ex instanceof FallbackChainExhaustedException) { | ||
throw ex; | ||
} | ||
|
||
const shouldFallbackOnException = await this.isExceptionHandled(ex); | ||
if (!shouldFallbackOnException) { | ||
throw ex; | ||
} | ||
|
||
const nextExecutor = remainingFallbackChain.shift(); | ||
if (nextExecutor === undefined) { | ||
throw new FallbackChainExhaustedException(); | ||
} | ||
|
||
executor = nextExecutor; | ||
|
||
for (const onFallbackFn of this.onFallbackFns) { | ||
try { | ||
await onFallbackFn(undefined, ex); | ||
} catch (onFallbackError) { | ||
// ignored | ||
} | ||
} | ||
|
||
continue; | ||
} | ||
} | ||
} finally { | ||
try { | ||
for (const onFinallyFn of this.onFinallyFns) { | ||
try { | ||
await onFinallyFn(); | ||
} catch (onFinallyError) { | ||
// ignored | ||
} | ||
} | ||
} finally { | ||
this.executing--; | ||
} | ||
} | ||
} | ||
} |
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,4 @@ | ||
export type OnFallbackFn<ResultType> = ( | ||
result: ResultType | undefined, | ||
error: unknown | undefined, | ||
) => void | Promise<void>; |
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 @@ | ||
export type BackoffStrategy = (currentRetryCount: number) => number | Promise<number>; |
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,5 @@ | ||
export type OnRetryFn<ResultType> = ( | ||
result: ResultType | undefined, | ||
error: unknown | undefined, | ||
currentRetryCount: number, | ||
) => void | Promise<void>; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export type OnFinallyFn = () => void | Promise<void>; |
Oops, something went wrong.