小布工坊.
← 回到资源库

资源详情 · 消息处理

有限次数的重试计划器

针对模拟HTTP状态给出等待时间和停止原因;先确认可安全重复,再考虑有限次数的指数等待。

资源正文、示例和许可证保留原文。代码仅展示,不会在线执行或调用AI。

资源详情

适合学习失败处理策略。attempt包含已执行的首次尝试;代码只返回计划,不发请求、不自动等待。聊天发送、付款等未确认幂等的操作不得直接套用自动重试。

使用前准备

适用平台
Windows · macOS · Linux
内容更新
  • 准备Node.js 24和文本编辑器;本站仅在Node.js v24.19.0本地核对。
  • 先确认总尝试次数和幂等含义;示例状态码是模拟值。
node retry-policy.mjs

先在文件所在目录打开终端。无需 npm install;本例不联网、不读写用户文件,不能直接代替生产平台接入。

常见卡点

  • 按指定.mjs文件名保存,避免变成.mjs.txt;不需要npm install。
  • 此例不执行重试;不能直接给消息发送或付款套上自动重发。
官方网站与安装说明 ↗

完整正文

// SPDX-License-Identifier: MIT
// 原创教学示例:只返回计划;不联网、不计时、不执行重试。
export function planRetry(options = {}) {
  if (!options || typeof options !== "object" || Array.isArray(options)) throw new TypeError("选项需为对象");
  const { attempt = 1, maxAttempts = 3, status = 503, idempotent = false,
    baseDelayMs = 500, maxDelayMs = 10000 } = options;
  if (!Number.isInteger(maxAttempts) || maxAttempts < 1 || maxAttempts > 6) throw new RangeError("总尝试次数需为1至6");
  if (!Number.isInteger(attempt) || attempt < 1 || attempt > maxAttempts) throw new RangeError("attempt指已尝试次数,必须在上限以内");
  if (!Number.isInteger(status) || status < 100 || status > 599) throw new TypeError("需要有效HTTP状态码");
  if (typeof idempotent !== "boolean") throw new TypeError("必须明确是否可安全重复");
  if (![baseDelayMs, maxDelayMs].every(value => Number.isSafeInteger(value) && value >= 1 && value <= 60000) || baseDelayMs > maxDelayMs) throw new RangeError("等待范围无效");
  if (!idempotent) return { retry: false, delayMs: 0, reason: "未确认可安全重试" };
  if (![408, 429, 500, 502, 503, 504].includes(status)) return { retry: false, delayMs: 0, reason: "该状态不在重试范围" };
  if (attempt >= maxAttempts) return { retry: false, delayMs: 0, reason: "已达到总尝试上限" };
  const delayMs = Math.min(maxDelayMs, baseDelayMs * 2 ** (attempt - 1));
  return { retry: true, delayMs, reason: "仅返回计划,未执行" };
}

// 503只是模拟输入;没有真的发请求。总尝试3次,最多补做2次。
const plans = [1, 2, 3].map(attempt => planRetry({ attempt, maxAttempts: 3, status: 503, idempotent: true }));
console.log(JSON.stringify(plans));

使用教程

把503当作本地模拟输入,依次计算第1、2、3次尝试后的决定;不会真正重发。

  1. 保存完整教学代码

    点击下载代码,或复制正文保存为retry-policy.mjs。保留许可说明;不要混入Markdown三反引号,确认后缀不是.mjs.txt。

  2. 在文件夹中运行

    准备Node.js 24,在保存文件的目录打开终端,执行下方node命令;不需要账号、密钥或第三方依赖。

  3. 核对结果后再修改

    前两项等待500与1000毫秒;第3次停止,delayMs为0。把idempotent改为false,即使是503也不允许继续重试。

保存文件名
retry-policy.mjs
运行环境
Node.js 24;本站运行核验为v24.19.0。保存为.mjs,无需npm install。
依赖与费用
代码不联网、不调用模型、不读写用户文件;无接口费用。设备和电力成本由使用者承担,接入实际应用后的成本另行核对。

运行命令

node retry-policy.mjs

预期输出

[{"retry":true,"delayMs":500,"reason":"仅返回计划,未执行"},{"retry":true,"delayMs":1000,"reason":"仅返回计划,未执行"},{"retry":false,"delayMs":0,"reason":"已达到总尝试上限"}]

本地核验

2026-10-22 · Node.js v24.19.0

已在本站本机运行此页原样附带的教学代码,并核对演示输出及本批注明的边界测试;不代表生产验证、全平台实测或安全审计。

适合用在哪里

  • 学习有限次指数等待
  • 区分安全重试与可能重复产生副作用的操作

注意事项

  • attempt表示已经发出的总次数,包含第一次;maxAttempts为3不代表还要重试3次。
  • 这里只给建议,不实现请求、等待、随机抖动、服务端Retry-After或幂等键;接入前需遵守实际服务规则。
  • 没有收到响应不代表操作未成功。发送消息、下单和支付等不能只凭状态码就重发。

原作者与来源

署名
包子海梦 · AI辅助开发
原作者与来源
提示集贡献者

声明与修改说明

新编写的通用教学示例,发布前仍需核对来源与运行效果。

完整许可证

MIT

MIT License

Copyright (c) 2026 提示集贡献者

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

验证范围

教学代码已运行

已运行完整带许可的教学文件并核对输出;不代表生产部署、平台接入或全部边界情况都已验证。

2026-10-23 Node.js v24.19.0