优化 CF 资源占用:定时器与 KV 热路径 - #466
Open
Shinku-Chen wants to merge 1 commit into
Open
Conversation
扩展传输(XHTTP)、定时器与 KV 读取三处的资源浪费,均不改变对外行为。 1. 移除扩展传输连接的 45 秒绝对超时 原先 Promise.race 里混了一个 sleep(值超时值),正在正常传数据的 连接也会被无条件掐断,客户端随即重连——一条活跃连接每天要烧掉 近 2000 次请求。终止兜底仍由 downloader 自带的空闲超时提供 (45 秒无下行数据即中断),并发计数 值值197 一定会被归还。 2. 修掉挂起不清理的超时定时器 新增 创建可取消延时(),建连超时在 finally 中必定 cancel。 原先建连成功后那个 5 秒 setTimeout 仍挂在 isolate 上, 每次建连都漏一个,白算 duration。 3. 每连接的 5 秒 setInterval 改为惰性重排定时器 到点回看最后活动时间,未超时则按剩余时间续一次。忙时由每 5 秒 醒一次降到每 45 秒一次,空闲判定精度反而更准(原为 45~50s 抖动)。 同时把停止函数提到外层,让 abort() 也能立刻掐掉定时器——原先 读循环卡在 read() 上时,abort 唤不醒它,定时器要挂到超时才醒。 4. 数据面请求走长 KV 缓存窗口 新增 键值热路径缓存期限(5 分钟),WebSocket 升级与扩展传输 POST 使用;控制面(面板/API/订阅)仍为 30 秒。避免每条新连接都触发一次 KV 读。/api/ 下的 POST 是配置写入,显式排除在外,否则会拿着过期 快照回写、覆盖别处刚改的配置。 未改 KV 的 cacheTtl:默认已是 60 秒,且调高 c 键会破坏 c_ver 跨 isolate 失效机制(版本号变了但配置仍在边缘缓存中)。 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
byJoey
force-pushed
the
main
branch
2 times, most recently
from
August 19, 2026 16:57
8f2d2d6 to
d860e4a
Compare
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.
背景
部署到 Cloudflare 后资源用量增长偏快。定位到四处与定时器、KV 读取有关的浪费,本 PR 只修这四处,均不改变对外行为。
1. 移除扩展传输(XHTTP)连接的 45 秒绝对超时
处理扩展超文本客户端的Promise.race里混了一个sleep(值超时值),等于给每条 XHTTP 连接加了 45 秒硬上限——正在正常传数据的连接也会被无条件 close,客户端随即重连。按 45 秒一轮算,一条活跃连接每天要消耗近 2000 次请求,免费额度很容易被吃满。终止兜底仍然存在:
创建扩展超文本值里的空闲超时保证「45 秒无下行数据即中断」,所以连接不会永久悬挂,并发计数值值197也一定会被归还。2. 修掉挂起不清理的超时定时器
新增
创建可取消延时(),连接值远程扩展超文本的建连超时在finally中必定cancel()。原先建连成功后那个 5 秒setTimeout仍挂在 isolate 上,每次建连都漏一个。3. 每连接的 5 秒 setInterval 改为惰性重排定时器
创建扩展超文本值原先用setInterval(..., 5000)轮询空闲状态,连接活多久就空转多久。改为到点回看最后活动时间、未超时则按剩余时间续一次:忙时由每 5 秒醒一次降到每 45 秒一次,空闲判定精度反而更准(原为 45~50s 抖动,现在正好 45s)。顺带修了一个原有问题:停止函数原先声明在 Promise executor 作用域内,
abort()拿不到它。读循环卡在read()上时 abort 唤不醒它,定时器要挂到超时才醒。现在把句柄提到外层。4. 数据面请求走长 KV 缓存窗口
新增
键值热路径缓存期限(5 分钟),供 WebSocket 升级与扩展传输 POST 使用;控制面(面板 / API / 订阅)仍为 30 秒,避免每条新连接都触发一次 KV 读。/api/下的 POST 是配置写入,显式排除在外——否则会拿着过期快照回写、覆盖别处刚改的配置。没有改的
KV 的
cacheTtl没动:默认已是 60 秒,而调高c键会破坏c_ver跨 isolate 失效机制(版本号变了但配置仍在边缘缓存里)。说明
明文源吗。混淆产物少年你相信光吗由 CI 生成,未包含在本 PR 中。node --check验证语法。