retry-async-wrapper 1.0.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.
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="JavaScriptLibraryMappings">
4
+ <includedPredefinedLibrary name="Node.js Core" />
5
+ </component>
6
+ </project>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/veepai_retry.iml" filepath="$PROJECT_DIR$/.idea/veepai_retry.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,12 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="WEB_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$">
5
+ <excludeFolder url="file://$MODULE_DIR$/.tmp" />
6
+ <excludeFolder url="file://$MODULE_DIR$/temp" />
7
+ <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
+ </content>
9
+ <orderEntry type="inheritedJdk" />
10
+ <orderEntry type="sourceFolder" forTests="false" />
11
+ </component>
12
+ </module>
package/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
package/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # retry-async-wrapper
2
+
3
+ 一个基于 `retry` 库的 Promise 重试工具,支持单次任务执行超时控制和自定义重试策略。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install retry-async-wrapper
9
+ ```
10
+
11
+ ## 使用示例
12
+
13
+ ```javascript
14
+ const retryPromise = require('retry-async-wrapper');
15
+
16
+ // 定义一个可能失败的任务
17
+ const task = (attempt) => {
18
+ console.log(`第 ${attempt} 次尝试`);
19
+ return new Promise((resolve, reject) => {
20
+ if (Math.random() > 0.3) {
21
+ reject(new Error('任务失败'));
22
+ } else {
23
+ resolve('任务成功');
24
+ }
25
+ });
26
+ };
27
+
28
+ // 使用默认配置重试任务
29
+ retryPromise(task)
30
+ .then(result => console.log('成功:', result))
31
+ .catch(error => console.error('最终失败:', error));
32
+ ```
33
+
34
+ ## 配置选项
35
+
36
+ 可以通过 `options` 参数自定义重试行为,支持的配置如下:
37
+
38
+ | 属性 | 类型 | 默认值 | 描述 |
39
+ |-------------|-----------|--------------|----------------------------------------------------------------------|
40
+ | `retries` | `number` | `10` | 最大重试次数 |
41
+ | `factor` | `number` | `2` | 重试间隔时间的指数因子 |
42
+ | `minTimeout`| `number` | `1000` | 最小重试间隔时间(毫秒) |
43
+ | `maxTimeout`| `number` | `Infinity` | 最大重试间隔时间(毫秒) |
44
+ | `randomize` | `boolean` | `false` | 是否随机化重试间隔时间 |
45
+ | `timeout` | `number` | `5000` | 单次任务的超时时间(毫秒) |
46
+
47
+ ## 坑点
48
+
49
+ 1. **超时控制**:如果任务本身没有超时机制,建议设置 `timeout` 参数,否则任务可能会无限期挂起。
50
+ 2. **重试次数**:默认重试次数为 10 次,如果任务失败率较高,可能需要调整 `retries` 参数。
51
+ 3. **随机化间隔**:启用 `randomize` 可以避免重试风暴,但可能会导致重试间隔不可预测。
52
+ 4. **错误处理**:任务失败时,确保错误信息足够清晰,以便调试。
53
+ 5. **性能影响**:频繁的重试可能会对性能产生影响,尤其是在高并发场景下。
54
+
55
+ ## 贡献
56
+
57
+ 欢迎提交 Issue 或 Pull Request!
58
+
59
+ ## 许可证
60
+
61
+ MIT
package/index.js ADDED
@@ -0,0 +1,51 @@
1
+ const retry = require('retry');
2
+
3
+ /**
4
+ * 默认重试配置选项
5
+ * @property {number} retries - 最大重试次数,默认为10
6
+ * @property {number} factor - 重试间隔时间的指数因子,默认为2
7
+ * @property {number} minTimeout - 最小重试间隔时间(毫秒),默认为1000
8
+ * @property {number} maxTimeout - 最大重试间隔时间(毫秒),默认为Infinity
9
+ * @property {boolean} randomize - 是否随机化重试间隔时间,默认为false
10
+ * @property {number} timeout - 单次任务的超时时间(毫秒),默认为5000
11
+ */
12
+ const DEFAULT_OPTIONS = {
13
+ retries: 10,
14
+ factor: 2,
15
+ minTimeout: 1000,
16
+ maxTimeout: Infinity,
17
+ randomize: false,
18
+ timeout: 5000
19
+ };
20
+
21
+ function withTimeout(promise, ms) {
22
+ return new Promise((resolve, reject) => {
23
+ const timer = setTimeout(() => reject(new Error('retry timeout')), ms);
24
+ promise.then(
25
+ val => { clearTimeout(timer); resolve(val); },
26
+ err => { clearTimeout(timer); reject(err); }
27
+ );
28
+ });
29
+ }
30
+
31
+ function retryPromise(fn, options = {}) {
32
+ const opts = { ...DEFAULT_OPTIONS, ...options };
33
+
34
+ return new Promise((resolve, reject) => {
35
+ const operation = retry.operation(opts);
36
+
37
+ operation.attempt((currentAttempt) => {
38
+ const exec = Promise.resolve(fn(currentAttempt));
39
+
40
+ withTimeout(exec, opts.timeout)
41
+ .then(resolve)
42
+ .catch(err => {
43
+ if (!operation.retry(err)) {
44
+ return reject(operation.mainError());
45
+ }
46
+ });
47
+ });
48
+ });
49
+ }
50
+
51
+ module.exports = retryPromise;
package/package.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "retry-async-wrapper",
3
+ "version": "1.0.0",
4
+ "description": "基于 retry 二次封装的重试包,保留了 retry 的所有功能,简化了使用方式",
5
+ "main": "index.js",
6
+ "private": false,
7
+ "dependencies": {
8
+ "retry": "^0.13.1"
9
+ }
10
+ }