Repository navigation
someday 发信节流实现(补遗 v1.0):读码核实+计划+实现(Daniel) - #35
Merged
todosdev[bot] merged 1 commit intoOct 29, 2026
Merged
Conversation
按发信节流补遗实现:单轮收件人上限 + 封间隔与回调超时预算、限流类错误不烧 attempts 改走 15/30/60 分钟退避并熔断整轮、每收件人唯一 Message-ID 落库与落款 信件编号;新增 schema_v3–v6 迁移、send_throttle_config/send_paused_until 内键与 管理接口,resend 一并清空退避状态。 Checkpoint: Z1us6ZozCHvVkWSuFM6Hw
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
interval_ms + random(0, jitter_ms),单轮最多处理max_recipients_per_run个收件人,超出的信留在scheduled顺延到下一轮——把「一轮爆发」摊成「多轮细流」,避免同一 From、同一模板的几十封信在几十秒内打满 Lark 的反垃圾风控。attempts:改走next_attempt_at+throttle_attempts的 15 → 30 → 60 分钟退避,并写send_paused_until熔断本轮剩余发送;永久类错误(550/551/553 等)仍走原路径计次、达 3 次置failed。Message-ID(<sr.{letter}.{recipient}.{rand}@{From 域名}>)并落库,邮件头加X-Someday-Letter-Id,落款追加「信件编号」;运行日志扩为scanned / claimed / delivered / failed / throttled / deferred_by_cap / paused_until。Changes
投递调度(
worker/src/services/scheduler.ts)processDueLetters(env, { timeoutSeconds }):入口先看send_paused_until(熔断期内直接返回、不抢占);单轮预算 = 回调带来的timeoutSeconds(现配置 120 秒)留 10 秒余量,额度与时间在抢占之前计数,用尽即顺延并计入deferred_by_cap;本轮首封必发,保证每轮有进展。pending、信回scheduled(attempts/last_error不动),绝不卡在sending;成功则置sent并清空退避状态。delivery send:日志(信件/收件人 id、时刻、等待时长、成败),便于按时间戳核间隔。节流与错误分类(新增
worker/src/services/send-throttle.ts)system_settings.send_throttle_config,逐字段校验「整数 + 范围」,缺失/非法逐项回默认并console.warn;默认 4000 / 2000 / 10。classifySendFailure集中判定:回复码优先(421/450/451/452 → throttle,550/551/553 → permanent),再匹配关键词表(含中文「频繁/限流/超过上限」),其余保持原路径;throttleBackoffMs出 15/30/60 分钟序列(封顶 60);sleepMs用运行时原生setTimeout。发送层(
email.ts/smtp-transport.ts)<ts.rand@post>且不落库 —— 改为按补遗格式生成、随letter_recipients.message_id落库,默认形态的域名也改用 From 地址域名;新增SmtpResponseError(code, response),把 SMTP 回复码与原文一并带回供分类,不再靠解析错误串猜。数据与接口
schema_v3–schema_v6(各一条 DDL,账本重跑语义才幂等):letters.next_attempt_at、letters.throttle_attempts、letter_recipients.message_id、索引(status, next_attempt_at, send_at)。GET/PUT /api/admin/settings/send-throttle(管理员会话,v1 无前端页面;PUT 越界/非整数 400);POST /api/letters/:id/resend一并清空next_attempt_at/throttle_attempts。README.md补迁移清单、运行日志字段、节流参数与上线顺序:部署后需管理员先跑POST /api/admin/db-upgrade,否则扫描会因缺列报错。Testing
pnpm typecheck、pnpm build全通过;pnpm test= web 19 文件 / 151 用例 + worker 20 文件 / 214 用例全绿。timeoutSeconds预算透传。