typescript-retry-decorator 2.1.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +28 -9
- package/.github/workflows/npm-publish.yml +27 -7
- package/CODE_OF_CONDUCT.md +128 -0
- package/CONTRIBUTING.md +16 -0
- package/README.md +44 -10
- package/dist/retry.decorator.d.ts +22 -2
- package/dist/retry.decorator.d.ts.map +1 -1
- package/dist/retry.decorator.js +36 -4
- package/dist/retry.decorator.js.map +1 -1
- package/dist/retry.decorator.test.js +52 -1
- package/dist/retry.decorator.test.js.map +1 -1
- package/package.json +1 -1
- package/src/retry.decorator.test.ts +41 -2
- package/src/retry.decorator.ts +45 -5
- package/tsconfig.json +1 -0
package/.github/workflows/ci.yml
CHANGED
|
@@ -11,16 +11,35 @@ jobs:
|
|
|
11
11
|
|
|
12
12
|
runs-on: ubuntu-latest
|
|
13
13
|
|
|
14
|
-
strategy:
|
|
15
|
-
matrix:
|
|
16
|
-
node-version: [14.x, 16.x]
|
|
17
|
-
|
|
18
14
|
steps:
|
|
19
|
-
- uses: actions/checkout@
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
- uses: actions/checkout@v3
|
|
16
|
+
|
|
17
|
+
- name: Install Node.js
|
|
18
|
+
uses: actions/setup-node@v3
|
|
19
|
+
with:
|
|
20
|
+
node-version: 18
|
|
21
|
+
|
|
22
|
+
- uses: pnpm/action-setup@v2
|
|
23
|
+
name: Install pnpm
|
|
24
|
+
id: pnpm-install
|
|
22
25
|
with:
|
|
23
|
-
|
|
24
|
-
|
|
26
|
+
version: 7
|
|
27
|
+
run_install: false
|
|
28
|
+
|
|
29
|
+
- name: Get pnpm store directory
|
|
30
|
+
id: pnpm-cache
|
|
31
|
+
shell: bash
|
|
32
|
+
run: |
|
|
33
|
+
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
|
|
34
|
+
|
|
35
|
+
- uses: actions/cache@v3
|
|
36
|
+
name: Setup pnpm cache
|
|
37
|
+
with:
|
|
38
|
+
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
|
|
39
|
+
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
|
|
40
|
+
restore-keys: |
|
|
41
|
+
${{ runner.os }}-pnpm-store-
|
|
42
|
+
|
|
43
|
+
- run: pnpm install
|
|
25
44
|
- run: npm run build
|
|
26
45
|
- run: npm test
|
|
@@ -10,16 +10,36 @@ on:
|
|
|
10
10
|
jobs:
|
|
11
11
|
publish-npm:
|
|
12
12
|
runs-on: ubuntu-latest
|
|
13
|
-
strategy:
|
|
14
|
-
matrix:
|
|
15
|
-
node-version: [14.x]
|
|
16
13
|
steps:
|
|
17
|
-
- uses: actions/checkout@
|
|
18
|
-
-
|
|
14
|
+
- uses: actions/checkout@v3
|
|
15
|
+
- name: Install Node.js
|
|
16
|
+
uses: actions/setup-node@v3
|
|
19
17
|
with:
|
|
20
|
-
node-version:
|
|
18
|
+
node-version: 18
|
|
21
19
|
registry-url: https://registry.npmjs.org/
|
|
22
|
-
|
|
20
|
+
|
|
21
|
+
- uses: pnpm/action-setup@v2
|
|
22
|
+
name: Install pnpm
|
|
23
|
+
id: pnpm-install
|
|
24
|
+
with:
|
|
25
|
+
version: 7
|
|
26
|
+
run_install: false
|
|
27
|
+
|
|
28
|
+
- name: Get pnpm store directory
|
|
29
|
+
id: pnpm-cache
|
|
30
|
+
shell: bash
|
|
31
|
+
run: |
|
|
32
|
+
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
|
|
33
|
+
|
|
34
|
+
- uses: actions/cache@v3
|
|
35
|
+
name: Setup pnpm cache
|
|
36
|
+
with:
|
|
37
|
+
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
|
|
38
|
+
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
|
|
39
|
+
restore-keys: |
|
|
40
|
+
${{ runner.os }}-pnpm-store-
|
|
41
|
+
|
|
42
|
+
- run: pnpm install
|
|
23
43
|
- run: npm run build
|
|
24
44
|
- run: npm test
|
|
25
45
|
- run: npm publish
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity
|
|
10
|
+
and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the
|
|
26
|
+
overall community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or
|
|
31
|
+
advances of any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email
|
|
35
|
+
address, without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official e-mail address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
.
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series
|
|
86
|
+
of actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or
|
|
93
|
+
permanent ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within
|
|
113
|
+
the community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.0, available at
|
|
119
|
+
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct
|
|
122
|
+
enforcement ladder](https://github.com/mozilla/diversity).
|
|
123
|
+
|
|
124
|
+
[homepage]: https://www.contributor-covenant.org
|
|
125
|
+
|
|
126
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
127
|
+
https://www.contributor-covenant.org/faq. Translations are available at
|
|
128
|
+
https://www.contributor-covenant.org/translations.
|
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
When contributing to this repository, please first discuss the change you wish to make via issue,
|
|
4
|
+
email, or any other method with the owners of this repository before making a change.
|
|
5
|
+
|
|
6
|
+
Please note we have a code of conduct, please follow it in all your interactions with the project.
|
|
7
|
+
|
|
8
|
+
## Pull Request Process
|
|
9
|
+
|
|
10
|
+
1. Ensure any install or build dependencies are removed before the end of the layer when doing a
|
|
11
|
+
build.
|
|
12
|
+
2. Update the README.md with details of changes to the interface, this includes new environment
|
|
13
|
+
variables, exposed ports, useful file locations and container parameters.
|
|
14
|
+
3. Increase the version numbers in `package.json` to the new version that this
|
|
15
|
+
Pull Request would represent. The versioning scheme we use is [SemVer](http://semver.org/).
|
|
16
|
+
4. You may merge the Pull Request in once you have the sign-off, or the maintainer will do the merge.
|
package/README.md
CHANGED
|
@@ -8,15 +8,25 @@ Import and use it. Retry for `Promise` is supported as long as the `runtime` has
|
|
|
8
8
|
> npm install typescript-retry-decorator
|
|
9
9
|
|
|
10
10
|
### Options
|
|
11
|
-
| Option Name |
|
|
12
|
-
|
|
13
|
-
| maxAttempts |
|
|
14
|
-
| backOff |
|
|
15
|
-
| backOffPolicy |
|
|
16
|
-
| exponentialOption |
|
|
17
|
-
| doRetry |
|
|
18
|
-
| value |
|
|
19
|
-
| useConsoleLogger |
|
|
11
|
+
| Option Name | Type | Required? | Default | Description |
|
|
12
|
+
|:-----------------:|:------------------------:|:---------:|:---------------------------------------:|:-----------------------------------------------------------------------------------------------------------------:|
|
|
13
|
+
| maxAttempts | number | Yes | - | The max attempts to try |
|
|
14
|
+
| backOff | number | No | 0 | number in `ms` to back off. If not set, then no wait |
|
|
15
|
+
| backOffPolicy | enum | No | FixedBackOffPolicy | can be fixed or exponential |
|
|
16
|
+
| exponentialOption | object | No | `{ maxInterval: 2000, multiplier: 2 }` | This is for the `ExponentialBackOffPolicy` <br/> The max interval each wait and the multiplier for the `backOff`. For `backoffStrategy`, more details below |
|
|
17
|
+
| doRetry | (e: any) => boolean | No | - | Function with error parameter to decide if repetition is necessary. |
|
|
18
|
+
| value | Error/Exception class | No | [ ] | An array of Exception types that are retryable. |
|
|
19
|
+
| useConsoleLogger | boolean | No | true | Print errors on console. |
|
|
20
|
+
| useOriginalError | throw original exception | No | false | `MaxAttemptsError` by default. if this is set to *true*, the `original` exception would be thrown instead. |
|
|
21
|
+
|
|
22
|
+
#### Exponential options
|
|
23
|
+
|
|
24
|
+
The `exponentialOption` allows you to fine-tune the exponential backoff strategy using several options:
|
|
25
|
+
- `maxInterval`: The maximum interval between two retries. The default value is 2000 ms.
|
|
26
|
+
- `multiplier`: The multiplier to use to generate the next backoff interval from the last one. The default value is 2.
|
|
27
|
+
- `backoffStrategy`: Optional. If specified, determines the strategy used to introduce "jitter" between retry intervals. For an explanation of the available strategies and why you might select one over the other, check out [this article](https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/).
|
|
28
|
+
- `ExponentialBackoffStrategy.FullJitter`: The base backoff interval is multiplied by a random number between 0 and 1.
|
|
29
|
+
- `ExponentialBackoffStrategy.EqualJitter`: The backoff interval is (base interval / 2) + (random value between 0 and base interval / 2).
|
|
20
30
|
|
|
21
31
|
### Example
|
|
22
32
|
```typescript
|
|
@@ -84,6 +94,17 @@ class RetryExample {
|
|
|
84
94
|
console.info(`Calling ExponentialBackOffRetry backOff 1s, multiplier=3 for the ${count++} time at ${new Date().toLocaleTimeString()}`);
|
|
85
95
|
throw new Error('I failed!');
|
|
86
96
|
}
|
|
97
|
+
|
|
98
|
+
@Retryable({
|
|
99
|
+
maxAttempts: 3,
|
|
100
|
+
backOffPolicy: BackOffPolicy.ExponentialBackOffPolicy,
|
|
101
|
+
backOff: 1000,
|
|
102
|
+
exponentialOption: { maxInterval: 4000, multiplier: 2, backoffStrategy: ExponentialBackoffStrategy.EqualJitter }
|
|
103
|
+
})
|
|
104
|
+
static async ExponentialBackOffWithJitterRetry() {
|
|
105
|
+
console.info(`Calling ExponentialBackOffWithJitterRetry backOff 1s, multiplier=2 for the ${count++} time at ${new Date().toLocaleTimeString()}`);
|
|
106
|
+
throw new Error('I failed!');
|
|
107
|
+
}
|
|
87
108
|
}
|
|
88
109
|
|
|
89
110
|
(async () => {
|
|
@@ -121,7 +142,14 @@ class RetryExample {
|
|
|
121
142
|
} catch (e) {
|
|
122
143
|
console.info(`All retry done as expected, final message: '${e.message}'`);
|
|
123
144
|
}
|
|
124
|
-
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
resetCount();
|
|
148
|
+
await RetryExample.ExponentialBackOffWithJitterRetry();
|
|
149
|
+
} catch (e) {
|
|
150
|
+
console.info(`All retry done as expected, final message: '${e.message}'`);
|
|
151
|
+
}
|
|
152
|
+
|
|
125
153
|
})();
|
|
126
154
|
|
|
127
155
|
function resetCount() {
|
|
@@ -163,4 +191,10 @@ Calling ExponentialBackOffRetry backOff 1s, multiplier=3 for the 3 time at 4:12:
|
|
|
163
191
|
Calling ExponentialBackOffRetry backOff 1s, multiplier=3 for the 4 time at 4:13:03 PM
|
|
164
192
|
I failed!
|
|
165
193
|
All retry done as expected, final message: 'Failed for 'ExponentialBackOffRetry' for 3 times.'
|
|
194
|
+
Calling ExponentialBackOffWithJitterRetry backOff 1s, multiplier=2 for the 1 time at 4:13:03 PM
|
|
195
|
+
Calling ExponentialBackOffWithJitterRetry backOff 1s, multiplier=2 for the 2 time at 4:13:03 PM
|
|
196
|
+
Calling ExponentialBackOffWithJitterRetry backOff 1s, multiplier=2 for the 3 time at 4:13:05 PM
|
|
197
|
+
Calling ExponentialBackOffWithJitterRetry backOff 1s, multiplier=2 for the 4 time at 4:13:09 PM
|
|
198
|
+
I failed!
|
|
199
|
+
All retry done as expected, final message: 'Failed for 'ExponentialBackOffWithJitterRetry' for 3 times.'
|
|
166
200
|
```
|
|
@@ -8,20 +8,40 @@ export declare class MaxAttemptsError extends Error {
|
|
|
8
8
|
code: string;
|
|
9
9
|
}
|
|
10
10
|
export interface RetryOptions {
|
|
11
|
-
maxAttempts: number;
|
|
12
11
|
backOffPolicy?: BackOffPolicy;
|
|
13
12
|
backOff?: number;
|
|
14
13
|
doRetry?: (e: any) => boolean;
|
|
15
|
-
value?: ErrorConstructor[];
|
|
16
14
|
exponentialOption?: {
|
|
17
15
|
maxInterval: number;
|
|
18
16
|
multiplier: number;
|
|
17
|
+
/**
|
|
18
|
+
* Optional. If provided, the backoff time will include jitter using the desired strategy.
|
|
19
|
+
* For more information, see https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
|
|
20
|
+
*/
|
|
21
|
+
backoffStrategy?: ExponentialBackoffStrategy;
|
|
19
22
|
};
|
|
23
|
+
maxAttempts: number;
|
|
24
|
+
value?: ErrorConstructor[];
|
|
20
25
|
useConsoleLogger?: boolean;
|
|
26
|
+
useOriginalError?: boolean;
|
|
21
27
|
}
|
|
22
28
|
export declare enum BackOffPolicy {
|
|
23
29
|
FixedBackOffPolicy = "FixedBackOffPolicy",
|
|
24
30
|
ExponentialBackOffPolicy = "ExponentialBackOffPolicy"
|
|
25
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Represents different strategies for applying jitter to backoff times.
|
|
34
|
+
* @see https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
|
|
35
|
+
*/
|
|
36
|
+
export declare enum ExponentialBackoffStrategy {
|
|
37
|
+
/**
|
|
38
|
+
* The backoff time will be (base backoff time) * (random number between 0 and 1).
|
|
39
|
+
*/
|
|
40
|
+
FullJitter = "FullJitter",
|
|
41
|
+
/**
|
|
42
|
+
* The backoff time will be (base backoff time / 2) + (random number between 0 and (base backoff time / 2)).
|
|
43
|
+
*/
|
|
44
|
+
EqualJitter = "EqualJitter"
|
|
45
|
+
}
|
|
26
46
|
export declare type DecoratorFunction = (target: Record<string, any>, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any>;
|
|
27
47
|
//# sourceMappingURL=retry.decorator.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retry.decorator.d.ts","sourceRoot":"","sources":["../src/retry.decorator.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,YAAY,GAAG,iBAAiB,
|
|
1
|
+
{"version":3,"file":"retry.decorator.d.ts","sourceRoot":"","sources":["../src/retry.decorator.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,YAAY,GAAG,iBAAiB,CAyFlE;AAED,qBAAa,gBAAiB,SAAQ,KAAK;IACzC,IAAI,SAAS;CAMd;AAED,MAAM,WAAW,YAAY;IAC3B,aAAa,CAAC,EAAE,aAAa,CAAC;IAC9B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,OAAO,CAAC;IAC9B,iBAAiB,CAAC,EAAE;QAClB,WAAW,EAAE,MAAM,CAAC;QACpB,UAAU,EAAE,MAAM,CAAC;QACnB;;;WAGG;QACH,eAAe,CAAC,EAAE,0BAA0B,CAAC;KAC9C,CAAC;IACF,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,gBAAgB,EAAE,CAAC;IAC3B,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B;AAED,oBAAY,aAAa;IACvB,kBAAkB,uBAAuB;IACzC,wBAAwB,6BAA6B;CACtD;AAED;;;GAGG;AACH,oBAAY,0BAA0B;IACpC;;OAEG;IACH,UAAU,eAAe;IACzB;;OAEG;IACH,WAAW,gBAAgB;CAC5B;AAED,oBAAY,iBAAiB,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,uBAAuB,CAAC,GAAG,CAAC,KAAK,uBAAuB,CAAC,GAAG,CAAC,CAAC"}
|
package/dist/retry.decorator.js
CHANGED
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.BackOffPolicy = exports.MaxAttemptsError = exports.Retryable = void 0;
|
|
12
|
+
exports.ExponentialBackoffStrategy = exports.BackOffPolicy = exports.MaxAttemptsError = exports.Retryable = void 0;
|
|
13
13
|
const utils_1 = require("./utils");
|
|
14
14
|
/**
|
|
15
15
|
* retry decorator which is nothing but a high order function wrapper
|
|
@@ -56,6 +56,8 @@ function Retryable(options) {
|
|
|
56
56
|
catch (e) {
|
|
57
57
|
if (--maxAttempts < 0) {
|
|
58
58
|
(typeof options.useConsoleLogger !== 'boolean' || options.useConsoleLogger) && (e === null || e === void 0 ? void 0 : e.message) && console.error(e.message);
|
|
59
|
+
if (options.useOriginalError)
|
|
60
|
+
throw e;
|
|
59
61
|
const maxAttemptsErrorInstance = new MaxAttemptsError(e === null || e === void 0 ? void 0 : e.message);
|
|
60
62
|
// Add the existing error stack if present
|
|
61
63
|
if (e === null || e === void 0 ? void 0 : e.stack) {
|
|
@@ -66,10 +68,9 @@ function Retryable(options) {
|
|
|
66
68
|
if (!canRetry(e)) {
|
|
67
69
|
throw e;
|
|
68
70
|
}
|
|
69
|
-
backOff && (yield (0, utils_1.sleep)(backOff));
|
|
71
|
+
backOff && (yield (0, utils_1.sleep)(applyBackoffStrategy(backOff)));
|
|
70
72
|
if (options.backOffPolicy === BackOffPolicy.ExponentialBackOffPolicy) {
|
|
71
|
-
|
|
72
|
-
backOff = newBackOff > options.exponentialOption.maxInterval ? options.exponentialOption.maxInterval : newBackOff;
|
|
73
|
+
backOff = Math.min(backOff * options.exponentialOption.multiplier, options.exponentialOption.maxInterval);
|
|
73
74
|
}
|
|
74
75
|
return retryAsync.apply(this, [fn, args, maxAttempts, backOff]);
|
|
75
76
|
}
|
|
@@ -89,6 +90,22 @@ function Retryable(options) {
|
|
|
89
90
|
!options.backOff && (options.backOff = 1000);
|
|
90
91
|
options.exponentialOption = Object.assign({ maxInterval: 2000, multiplier: 2 }, options.exponentialOption);
|
|
91
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Calculate the actual backoff using the specified backoff strategy, if any
|
|
95
|
+
* @see https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
|
|
96
|
+
* @param baseBackoff - base backoff time in ms
|
|
97
|
+
*/
|
|
98
|
+
function applyBackoffStrategy(baseBackoff) {
|
|
99
|
+
var _a;
|
|
100
|
+
const { backoffStrategy } = (_a = options.exponentialOption) !== null && _a !== void 0 ? _a : {};
|
|
101
|
+
if (backoffStrategy === ExponentialBackoffStrategy.EqualJitter) {
|
|
102
|
+
return baseBackoff / 2 + (Math.random() * baseBackoff / 2);
|
|
103
|
+
}
|
|
104
|
+
if (backoffStrategy === ExponentialBackoffStrategy.FullJitter) {
|
|
105
|
+
return Math.random() * baseBackoff;
|
|
106
|
+
}
|
|
107
|
+
return baseBackoff;
|
|
108
|
+
}
|
|
92
109
|
}
|
|
93
110
|
exports.Retryable = Retryable;
|
|
94
111
|
class MaxAttemptsError extends Error {
|
|
@@ -108,4 +125,19 @@ var BackOffPolicy;
|
|
|
108
125
|
BackOffPolicy["FixedBackOffPolicy"] = "FixedBackOffPolicy";
|
|
109
126
|
BackOffPolicy["ExponentialBackOffPolicy"] = "ExponentialBackOffPolicy";
|
|
110
127
|
})(BackOffPolicy = exports.BackOffPolicy || (exports.BackOffPolicy = {}));
|
|
128
|
+
/**
|
|
129
|
+
* Represents different strategies for applying jitter to backoff times.
|
|
130
|
+
* @see https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
|
|
131
|
+
*/
|
|
132
|
+
var ExponentialBackoffStrategy;
|
|
133
|
+
(function (ExponentialBackoffStrategy) {
|
|
134
|
+
/**
|
|
135
|
+
* The backoff time will be (base backoff time) * (random number between 0 and 1).
|
|
136
|
+
*/
|
|
137
|
+
ExponentialBackoffStrategy["FullJitter"] = "FullJitter";
|
|
138
|
+
/**
|
|
139
|
+
* The backoff time will be (base backoff time / 2) + (random number between 0 and (base backoff time / 2)).
|
|
140
|
+
*/
|
|
141
|
+
ExponentialBackoffStrategy["EqualJitter"] = "EqualJitter";
|
|
142
|
+
})(ExponentialBackoffStrategy = exports.ExponentialBackoffStrategy || (exports.ExponentialBackoffStrategy = {}));
|
|
111
143
|
//# sourceMappingURL=retry.decorator.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retry.decorator.js","sourceRoot":"","sources":["../src/retry.decorator.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mCAAgC;AAEhC;;;;GAIG;AACH,SAAgB,SAAS,CAAC,OAAqB;IAC7C;;;;;;;;OAQG;IACH,OAAO,UAAS,MAA2B,EAAE,WAAmB,EAAE,UAAwC;QACxG,MAAM,UAAU,GAAG,UAAU,CAAC,KAAK,CAAC;QACpC,iDAAiD;QACjD,IAAI,OAAO,CAAC,aAAa,KAAK,aAAa,CAAC,wBAAwB,EAAE;YACpE,kCAAkC,EAAE,CAAC;SACtC;QACD,UAAU,CAAC,KAAK,GAAG,UAAe,GAAG,IAAW;;gBAC9C,IAAI;oBACF,OAAO,MAAM,UAAU,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,UAAU,EAAE,IAAI,EAAE,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;iBAC/F;gBAAC,OAAO,CAAC,EAAE;oBACV,IAAI,CAAC,YAAY,gBAAgB,EAAE;wBACjC,MAAM,SAAS,GAAG,eAAe,WAAW,SAAS,OAAO,CAAC,WAAW,SAAS,CAAC;wBAClF,CAAC,CAAC,OAAO,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,SAAS,oBAAoB,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;qBACjF;oBACD,MAAM,CAAC,CAAC;iBACT;YACH,CAAC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AAW,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,EAAE,EACpC,OAAO,CAAC,iBAAiB,CAC7B,CAAC;IACJ,CAAC;IAED;;;;OAIG;IACH,SAAS,oBAAoB,CAAC,WAAmB;;QAC/C,MAAM,EAAE,eAAe,EAAE,GAAG,MAAA,OAAO,CAAC,iBAAiB,mCAAI,EAAE,CAAC;QAC5D,IAAI,eAAe,KAAK,0BAA0B,CAAC,WAAW,EAAE;YAC9D,OAAO,WAAW,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,WAAW,GAAG,CAAC,CAAC,CAAC;SAC5D;QACD,IAAI,eAAe,KAAK,0BAA0B,CAAC,UAAU,EAAE;YAC7D,OAAO,IAAI,CAAC,MAAM,EAAE,GAAG,WAAW,CAAC;SACpC;QACD,OAAO,WAAW,CAAC;IACrB,CAAC;AACH,CAAC;AAzFD,8BAyFC;AAED,MAAa,gBAAiB,SAAQ,KAAK;IAA3C;;QACE,SAAI,GAAG,KAAK,CAAC;QACb;;;;cAIM;IACR,CAAC;CAAA;AAPD,4CAOC;AAqBD,IAAY,aAGX;AAHD,WAAY,aAAa;IACvB,0DAAyC,CAAA;IACzC,sEAAqD,CAAA;AACvD,CAAC,EAHW,aAAa,GAAb,qBAAa,KAAb,qBAAa,QAGxB;AAED;;;GAGG;AACH,IAAY,0BASX;AATD,WAAY,0BAA0B;IACpC;;OAEG;IACH,uDAAyB,CAAA;IACzB;;OAEG;IACH,yDAA2B,CAAA;AAC7B,CAAC,EATW,0BAA0B,GAA1B,kCAA0B,KAA1B,kCAA0B,QASrC"}
|
|
@@ -53,12 +53,23 @@ class TestClass {
|
|
|
53
53
|
yield this.called();
|
|
54
54
|
});
|
|
55
55
|
}
|
|
56
|
+
exponentialBackOffWithJitterRetry() {
|
|
57
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
58
|
+
console.info(`Calling ExponentialBackOffRetry backOff 1s, multiplier=2 for the ${++this.count} time at ${new Date().toLocaleTimeString()}`);
|
|
59
|
+
yield this.called();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
56
62
|
noLog() {
|
|
57
63
|
return __awaiter(this, void 0, void 0, function* () {
|
|
58
64
|
console.log(`test method is called for ${++this.count} time`);
|
|
59
65
|
yield this.called();
|
|
60
66
|
});
|
|
61
67
|
}
|
|
68
|
+
useOriginalError() {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
yield this.called();
|
|
71
|
+
});
|
|
72
|
+
}
|
|
62
73
|
called() {
|
|
63
74
|
return __awaiter(this, void 0, void 0, function* () {
|
|
64
75
|
return 'from real implementation';
|
|
@@ -108,12 +119,28 @@ __decorate([
|
|
|
108
119
|
__metadata("design:paramtypes", []),
|
|
109
120
|
__metadata("design:returntype", Promise)
|
|
110
121
|
], TestClass.prototype, "exponentialBackOffRetry", null);
|
|
122
|
+
__decorate([
|
|
123
|
+
(0, retry_decorator_1.Retryable)({
|
|
124
|
+
maxAttempts: 3,
|
|
125
|
+
backOffPolicy: retry_decorator_1.BackOffPolicy.ExponentialBackOffPolicy,
|
|
126
|
+
exponentialOption: { maxInterval: 4000, multiplier: 2, backoffStrategy: retry_decorator_1.ExponentialBackoffStrategy.FullJitter },
|
|
127
|
+
}),
|
|
128
|
+
__metadata("design:type", Function),
|
|
129
|
+
__metadata("design:paramtypes", []),
|
|
130
|
+
__metadata("design:returntype", Promise)
|
|
131
|
+
], TestClass.prototype, "exponentialBackOffWithJitterRetry", null);
|
|
111
132
|
__decorate([
|
|
112
133
|
(0, retry_decorator_1.Retryable)({ maxAttempts: 2, useConsoleLogger: false }),
|
|
113
134
|
__metadata("design:type", Function),
|
|
114
135
|
__metadata("design:paramtypes", []),
|
|
115
136
|
__metadata("design:returntype", Promise)
|
|
116
137
|
], TestClass.prototype, "noLog", null);
|
|
138
|
+
__decorate([
|
|
139
|
+
(0, retry_decorator_1.Retryable)({ maxAttempts: 2, useOriginalError: true }),
|
|
140
|
+
__metadata("design:type", Function),
|
|
141
|
+
__metadata("design:paramtypes", []),
|
|
142
|
+
__metadata("design:returntype", Promise)
|
|
143
|
+
], TestClass.prototype, "useOriginalError", null);
|
|
117
144
|
describe('Capture original error data Test', () => {
|
|
118
145
|
test('exceed max retry', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
119
146
|
const testClass = new TestClass();
|
|
@@ -151,7 +178,7 @@ describe('Retry Test', () => {
|
|
|
151
178
|
yield testClass.testMethod();
|
|
152
179
|
}
|
|
153
180
|
catch (e) {
|
|
154
|
-
expect(e).
|
|
181
|
+
expect(e).toBeInstanceOf(retry_decorator_1.MaxAttemptsError);
|
|
155
182
|
expect(e.message.includes(errorMsg));
|
|
156
183
|
}
|
|
157
184
|
expect(calledSpy).toHaveBeenCalledTimes(3);
|
|
@@ -205,6 +232,16 @@ describe('Retry Test', () => {
|
|
|
205
232
|
catch (e) { }
|
|
206
233
|
expect(calledSpy).toHaveBeenCalledTimes(4);
|
|
207
234
|
}));
|
|
235
|
+
test('exponential backOff policy with jitter', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
236
|
+
jest.setTimeout(60000);
|
|
237
|
+
const calledSpy = jest.spyOn(testClass, 'called');
|
|
238
|
+
calledSpy.mockImplementation(() => { throw new Error(); });
|
|
239
|
+
try {
|
|
240
|
+
yield testClass.exponentialBackOffWithJitterRetry();
|
|
241
|
+
}
|
|
242
|
+
catch (e) { }
|
|
243
|
+
expect(calledSpy).toHaveBeenCalledTimes(4);
|
|
244
|
+
}));
|
|
208
245
|
test('no log', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
209
246
|
const calledSpy = jest.spyOn(testClass, 'called');
|
|
210
247
|
const errorSpy = jest.spyOn(console, 'error');
|
|
@@ -214,5 +251,19 @@ describe('Retry Test', () => {
|
|
|
214
251
|
expect(calledSpy).toHaveBeenCalledTimes(2);
|
|
215
252
|
expect(errorSpy).not.toHaveBeenCalled();
|
|
216
253
|
}));
|
|
254
|
+
// CustomError for testing.
|
|
255
|
+
class CustomError extends Error {
|
|
256
|
+
}
|
|
257
|
+
test('throw original error', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
258
|
+
jest.setTimeout(60000);
|
|
259
|
+
const calledSpy = jest.spyOn(testClass, 'called');
|
|
260
|
+
calledSpy.mockImplementation(() => { throw new CustomError(); });
|
|
261
|
+
try {
|
|
262
|
+
yield testClass.useOriginalError();
|
|
263
|
+
}
|
|
264
|
+
catch (e) {
|
|
265
|
+
expect(e).toBeInstanceOf(CustomError);
|
|
266
|
+
}
|
|
267
|
+
}));
|
|
217
268
|
});
|
|
218
269
|
//# sourceMappingURL=retry.decorator.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retry.decorator.test.js","sourceRoot":"","sources":["../src/retry.decorator.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"retry.decorator.test.js","sourceRoot":"","sources":["../src/retry.decorator.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,uDAAyG;AAEzG,MAAM,SAAS;IAEb;QACE,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;IACjB,CAAC;IAEK,UAAU;;YACd,OAAO,CAAC,GAAG,CAAC,6BAA6B,EAAE,IAAI,CAAC,KAAK,OAAO,CAAC,CAAC;YAC9D,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;QACtB,CAAC;KAAA;IAGK,uBAAuB;;YAC3B,OAAO,CAAC,GAAG,CAAC,6BAA6B,EAAE,IAAI,CAAC,KAAK,OAAO,CAAC,CAAC;YAC9D,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;QACtB,CAAC;KAAA;IAQK,WAAW;;YACf,OAAO,CAAC,IAAI,CAAC,2BAA2B,EAAE,IAAI,CAAC,KAAK,YAAY,IAAI,IAAI,EAAE,CAAC,kBAAkB,EAAE,EAAE,CAAC,CAAC;YACnG,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;QACtB,CAAC;K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CAAC,CAAC;IAEH,2BAA2B;IAC3B,MAAM,WAAY,SAAQ,KAAK;KAAI;IAEnC,IAAI,CAAC,sBAAsB,EAAE,GAAS,EAAE;QACtC,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;QACvB,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;QAClD,SAAS,CAAC,kBAAkB,CAAC,GAAG,EAAE,GAAG,MAAM,IAAI,WAAW,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QACjE,IAAI;YACF,MAAM,SAAS,CAAC,gBAAgB,EAAE,CAAC;SACpC;QAAC,OAAO,CAAC,EAAE;YACV,MAAM,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC;SACvC;IACH,CAAC,CAAA,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {BackOffPolicy, ExponentialBackoffStrategy, MaxAttemptsError, Retryable} from './retry.decorator';
|
|
2
2
|
|
|
3
3
|
class TestClass {
|
|
4
4
|
count: number;
|
|
@@ -48,12 +48,27 @@ class TestClass {
|
|
|
48
48
|
await this.called();
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
@Retryable({
|
|
52
|
+
maxAttempts: 3,
|
|
53
|
+
backOffPolicy: BackOffPolicy.ExponentialBackOffPolicy,
|
|
54
|
+
exponentialOption: { maxInterval: 4000, multiplier: 2, backoffStrategy: ExponentialBackoffStrategy.FullJitter },
|
|
55
|
+
})
|
|
56
|
+
async exponentialBackOffWithJitterRetry(): Promise<void> {
|
|
57
|
+
console.info(`Calling ExponentialBackOffRetry backOff 1s, multiplier=2 for the ${++this.count} time at ${new Date().toLocaleTimeString()}`);
|
|
58
|
+
await this.called();
|
|
59
|
+
}
|
|
60
|
+
|
|
51
61
|
@Retryable({ maxAttempts: 2, useConsoleLogger: false })
|
|
52
62
|
async noLog(): Promise<void> {
|
|
53
63
|
console.log(`test method is called for ${++this.count} time`);
|
|
54
64
|
await this.called();
|
|
55
65
|
}
|
|
56
66
|
|
|
67
|
+
@Retryable({ maxAttempts: 2, useOriginalError: true })
|
|
68
|
+
async useOriginalError(): Promise<void> {
|
|
69
|
+
await this.called();
|
|
70
|
+
}
|
|
71
|
+
|
|
57
72
|
async called(): Promise<string> {
|
|
58
73
|
return 'from real implementation';
|
|
59
74
|
}
|
|
@@ -102,7 +117,7 @@ describe('Retry Test', () => {
|
|
|
102
117
|
try {
|
|
103
118
|
await testClass.testMethod();
|
|
104
119
|
} catch (e) {
|
|
105
|
-
expect(e).
|
|
120
|
+
expect(e).toBeInstanceOf(MaxAttemptsError);
|
|
106
121
|
expect(e.message.includes(errorMsg));
|
|
107
122
|
}
|
|
108
123
|
expect(calledSpy).toHaveBeenCalledTimes(3);
|
|
@@ -160,6 +175,16 @@ describe('Retry Test', () => {
|
|
|
160
175
|
expect(calledSpy).toHaveBeenCalledTimes(4);
|
|
161
176
|
});
|
|
162
177
|
|
|
178
|
+
test('exponential backOff policy with jitter', async () => {
|
|
179
|
+
jest.setTimeout(60000);
|
|
180
|
+
const calledSpy = jest.spyOn(testClass, 'called');
|
|
181
|
+
calledSpy.mockImplementation(() => { throw new Error(); });
|
|
182
|
+
try {
|
|
183
|
+
await testClass.exponentialBackOffWithJitterRetry();
|
|
184
|
+
} catch (e) {}
|
|
185
|
+
expect(calledSpy).toHaveBeenCalledTimes(4);
|
|
186
|
+
});
|
|
187
|
+
|
|
163
188
|
test('no log', async () => {
|
|
164
189
|
const calledSpy = jest.spyOn(testClass, 'called');
|
|
165
190
|
const errorSpy = jest.spyOn(console, 'error');
|
|
@@ -169,5 +194,19 @@ describe('Retry Test', () => {
|
|
|
169
194
|
expect(calledSpy).toHaveBeenCalledTimes(2);
|
|
170
195
|
expect(errorSpy).not.toHaveBeenCalled();
|
|
171
196
|
});
|
|
197
|
+
|
|
198
|
+
// CustomError for testing.
|
|
199
|
+
class CustomError extends Error { }
|
|
200
|
+
|
|
201
|
+
test('throw original error', async () => {
|
|
202
|
+
jest.setTimeout(60000);
|
|
203
|
+
const calledSpy = jest.spyOn(testClass, 'called');
|
|
204
|
+
calledSpy.mockImplementation(() => { throw new CustomError(); });
|
|
205
|
+
try {
|
|
206
|
+
await testClass.useOriginalError();
|
|
207
|
+
} catch (e) {
|
|
208
|
+
expect(e).toBeInstanceOf(CustomError);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
172
211
|
});
|
|
173
212
|
|
package/src/retry.decorator.ts
CHANGED
|
@@ -41,6 +41,7 @@ export function Retryable(options: RetryOptions): DecoratorFunction {
|
|
|
41
41
|
} catch (e) {
|
|
42
42
|
if (--maxAttempts < 0) {
|
|
43
43
|
(typeof options.useConsoleLogger !== 'boolean' || options.useConsoleLogger) && e?.message && console.error(e.message);
|
|
44
|
+
if(options.useOriginalError) throw e;
|
|
44
45
|
const maxAttemptsErrorInstance = new MaxAttemptsError(e?.message);
|
|
45
46
|
// Add the existing error stack if present
|
|
46
47
|
if(e?.stack) {
|
|
@@ -52,10 +53,9 @@ export function Retryable(options: RetryOptions): DecoratorFunction {
|
|
|
52
53
|
if (!canRetry(e)) {
|
|
53
54
|
throw e;
|
|
54
55
|
}
|
|
55
|
-
backOff && (await sleep(backOff));
|
|
56
|
+
backOff && (await sleep(applyBackoffStrategy(backOff)));
|
|
56
57
|
if (options.backOffPolicy === BackOffPolicy.ExponentialBackOffPolicy) {
|
|
57
|
-
|
|
58
|
-
backOff = newBackOff > options.exponentialOption.maxInterval ? options.exponentialOption.maxInterval : newBackOff;
|
|
58
|
+
backOff = Math.min(backOff * options.exponentialOption.multiplier, options.exponentialOption.maxInterval);
|
|
59
59
|
}
|
|
60
60
|
return retryAsync.apply(this, [fn, args, maxAttempts, backOff]);
|
|
61
61
|
}
|
|
@@ -78,6 +78,22 @@ export function Retryable(options: RetryOptions): DecoratorFunction {
|
|
|
78
78
|
...options.exponentialOption,
|
|
79
79
|
};
|
|
80
80
|
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Calculate the actual backoff using the specified backoff strategy, if any
|
|
84
|
+
* @see https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
|
|
85
|
+
* @param baseBackoff - base backoff time in ms
|
|
86
|
+
*/
|
|
87
|
+
function applyBackoffStrategy(baseBackoff: number): number {
|
|
88
|
+
const { backoffStrategy } = options.exponentialOption ?? {};
|
|
89
|
+
if (backoffStrategy === ExponentialBackoffStrategy.EqualJitter) {
|
|
90
|
+
return baseBackoff / 2 + (Math.random() * baseBackoff / 2);
|
|
91
|
+
}
|
|
92
|
+
if (backoffStrategy === ExponentialBackoffStrategy.FullJitter) {
|
|
93
|
+
return Math.random() * baseBackoff;
|
|
94
|
+
}
|
|
95
|
+
return baseBackoff;
|
|
96
|
+
}
|
|
81
97
|
}
|
|
82
98
|
|
|
83
99
|
export class MaxAttemptsError extends Error {
|
|
@@ -90,13 +106,22 @@ export class MaxAttemptsError extends Error {
|
|
|
90
106
|
}
|
|
91
107
|
|
|
92
108
|
export interface RetryOptions {
|
|
93
|
-
maxAttempts: number;
|
|
94
109
|
backOffPolicy?: BackOffPolicy;
|
|
95
110
|
backOff?: number;
|
|
96
111
|
doRetry?: (e: any) => boolean;
|
|
112
|
+
exponentialOption?: {
|
|
113
|
+
maxInterval: number;
|
|
114
|
+
multiplier: number;
|
|
115
|
+
/**
|
|
116
|
+
* Optional. If provided, the backoff time will include jitter using the desired strategy.
|
|
117
|
+
* For more information, see https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
|
|
118
|
+
*/
|
|
119
|
+
backoffStrategy?: ExponentialBackoffStrategy;
|
|
120
|
+
};
|
|
121
|
+
maxAttempts: number;
|
|
97
122
|
value?: ErrorConstructor[];
|
|
98
|
-
exponentialOption?: { maxInterval: number; multiplier: number };
|
|
99
123
|
useConsoleLogger?: boolean;
|
|
124
|
+
useOriginalError?: boolean;
|
|
100
125
|
}
|
|
101
126
|
|
|
102
127
|
export enum BackOffPolicy {
|
|
@@ -104,5 +129,20 @@ export enum BackOffPolicy {
|
|
|
104
129
|
ExponentialBackOffPolicy = 'ExponentialBackOffPolicy'
|
|
105
130
|
}
|
|
106
131
|
|
|
132
|
+
/**
|
|
133
|
+
* Represents different strategies for applying jitter to backoff times.
|
|
134
|
+
* @see https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
|
|
135
|
+
*/
|
|
136
|
+
export enum ExponentialBackoffStrategy {
|
|
137
|
+
/**
|
|
138
|
+
* The backoff time will be (base backoff time) * (random number between 0 and 1).
|
|
139
|
+
*/
|
|
140
|
+
FullJitter = 'FullJitter',
|
|
141
|
+
/**
|
|
142
|
+
* The backoff time will be (base backoff time / 2) + (random number between 0 and (base backoff time / 2)).
|
|
143
|
+
*/
|
|
144
|
+
EqualJitter = 'EqualJitter',
|
|
145
|
+
}
|
|
146
|
+
|
|
107
147
|
export type DecoratorFunction = (target: Record<string, any>, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any>;
|
|
108
148
|
|