检查数字/重试
版权 2021-2026 检查 Digit, LLC
这个 @checkdigit/retry 模块执行推荐的“检查数字”重试算法.
用于生产使用的默认推荐行为是用指数回放重试8次.
重试编号前 n (从 1 开始),最大后退为 (2^(n - 1) * 100) 毫秒内,
财务报告和审定财务报表 完全抖动 选择从0到最大延迟.
默认的8次重试,最大延迟为100,200,400,800,1600,3200,6400,
和12,800毫秒。 这是最多25.5秒的总后退, 大约12.75秒
平均而言,完全紧张,不包括最初尝试的执行时间和8次重复.
此逻辑符合
AWS 建议的算法 财务报告和审定财务报表
AWS 指数回放和焦急 Doc。 。 。 .
然而,违约 waitRatio (100个), retries (8), (中文),
jitter (真实)和 maximumBackoff (+无穷)可以被压倒.
就试验情景而言,设置 waitRatio 改为 0 强迫立即复试.
如果超过允许重试的次数,
(单位:千美元) RetryError 被抛与 options 属性设置到输入选项,
财务报告和审定财务报表 cause 设置到上次重试时所抛出的错误.
注:本单元假定所有工作都是一能的,可多次重复试验而无后果。 如果这是 不使用此模块.
安装
npm install @checkdigit/retry
使用
import retry from '@checkdigit/retry';
// do some simple work, with the default of 8 retries and a wait ratio of 100.
const worker = retry(async (item: string) => {
/* idempotent network requests */
});
const result = await worker(someInput);
// Do some simple work, with a wait ratio of 0. This means retries occur immediately, useful for test scenarios.
const testWorker = retry(
async (item: string) => {
/* ...idempotent network requests... */
},
{ waitRatio: 0 },
);
const testResult = await testWorker(someTestInput);
// catch a RetryError
const errorWorker = retry(
async (item: string) => {
/* ...repeated failures... */
},
{ waitRatio: 0 },
);
try {
await errorWorker(someTestInput);
} catch (error) {
if (error instanceof RetryError) {
console.log('failed after ' + error.options.retries);
console.log(error.cause);
}
}
使用方式 @checkdigit/timeout
在某些情况下,建议将本单元与下列方案结合起来:
@checkdigit/timeout。 。 。 .
import retry from '@checkdigit/retry';
import timeout from '@checkdigit/timeout';
// wait up to 60 seconds on each attempt, retry on timeout
const worker = await retry((item) =>
timeout(
(async (input) => {
/* ...work... */
})(item),
),
);
const result = await worker(someInput);
许可证
麻省理工学院