高速数据流与高效并发:使用 Rust 和 Tokio 减少累积的等待

价格更新已经到达您的交易应用,但要过一段时间才能影响决策。随着更新数量增加,应用状态与市场之间的差距进一步扩大。出现这种症状时,需要同时检查数据的获取方式及其到达后的处理路径。
接收更新、提取数据、更新状态、检查条件、保存记录。如果多个阶段都要等待前一步操作完成才能继续,数据到达决策逻辑所需的时间就会变长。如果后续更新在此期间持续到达,这些等待还会进一步累积。要让高速数据流转化为响应迅速的应用,需要在保持必要顺序的同时,分离可以独立推进的工作。
本文面向使用 TypeScript、Python 或类似语言实现价格接收与决策逻辑的开发者。我们跟随价格源中的一条更新,观察应用如何接收它、将其应用于状态、检查条件,并将结果保留为辅助记录。保存目标是应用侧的文件或数据库;本文可运行的示例使用文件。
本文以辅助保存为例,将有序处理与可以分别推进的工作之间的边界落实到代码中。我们首先诊断接收路径在等待什么,并对现有 TypeScript 实现作出合理改进。然后考察 Rust/Tokio 如何让您控制所有权、共享状态、执行位置和并发上限,这是我们构建低延迟接收与决策引擎时首先推荐的选择。本文通过实现与验证,继续展开上一篇低延迟架构文章中对热路径和辅助工作的分离。
此处保存的是辅助记录,无须在下一次决策前完成持久化。对于正确性所依赖的步骤,例如发送订单前必须完成的持久化,不能用同样的替换方式将其移除。在接收与保存已经分离的应用中,问题也可能是未完成保存的数量不断增加。请追踪调用方,确定属于哪种情况。
本文的目标是帮助您认识产生等待的处理模式,以及如何重构这些模式。您应能够读懂自己代码中的依赖关系,判断哪些顺序必须保留、哪些工作可以分别推进。以价格 100 → 101 为例的图示说明原理,小型可运行示例则验证决策结果、顺序和记录保留。本文不报告真实环境中的改善百分比,也不按速度对语言排名。
即使交易条件编写正确,仍然需要调查新价格到达这些条件之前的等待。追踪下一次决策能够继续前必须完成的操作,再选择哪些部分需要保持有序、哪些可以分别推进。掌握这些知识后,在请求 AI 修复时,您也能够准确告诉它应调查哪里、保留哪些条件,并评估其建议的更改是否确实有帮助。
本文的主要主题是并发:在等待文件或网络通信等输入/输出(I/O)期间,推进其他工作。我们将其与并行区分开,后者指在多个 CPU 核心上同时执行计算。无论 Rust 还是 TypeScript,仅仅写上
async 都不会把繁重的计算移到另一个 CPU 上。[S03][S07][S08][S13]第 1、2 节将症状与设计联系起来。第 3、4 节使用 TypeScript 和 Rust 跟踪相同的六个价格。第 5、6 节讨论队列满、故障和关闭;第 7 节将设计映射到实际接收代码;第 8 节提供执行步骤。如果您从诊断现有 TypeScript 或 Python 代码开始,可以先阅读第 1–3 节,再阅读第 5–8 节,在考虑 Rust 实现时返回第 4 节。如果希望从原理入手,可以结合第 1、2、5、6 节的文字和图示,跟踪决策与保存的推进过程。
第 10 节是供您在自己的仓库中工作时完整复制的附录。理解正文中的原因和需要保留的条件后,可以用它请求 AI 智能体诊断、修改并验证您的代码。
轻触或点击图示,即可以原始尺寸打开图片。
1. 下一条更新能够继续处理前,必须先完成什么?
首先检查什么条件允许下一条更新开始处理。以下代码应用价格并检查交易条件,然后保存一条辅助记录。
typescript
for await (const update of updates) {
state.apply(update);
const decision = decide(state);
await saveAuxiliary({ seq: update.seq, price: state.price, decision });
}for await (const update of updates) {
state.apply(update);
const decision = decide(state);
await saveAuxiliary({ seq: update.seq, price: state.price, decision });
}异步 I/O 尚未完成时,Node.js 事件循环可以推进其他工作。但这个循环在保存的 Promise 兑现之前,不会进入下一次迭代。即使保存价格 100 的记录期间,价格 101 的更新已经到达,这个循环也要等保存完成后才能使用价格 101。[S12][S14]
检查现有代码并进行小规模本地回放,以确认是否存在这种依赖关系。针对同一个更新 ID,对照以下时间戳。
| 时间戳 | 在代码中的测量位置 | 检查内容 |
|---|---|---|
| 处理开始 | 取出更新后立即记录 | 是否必须等前一次保存完成,才能取出下一条更新 |
| 条件检查完成 | 应用状态并检查条件后立即记录 | 是状态应用与条件检查耗时较长,还是处理在开始前就受到阻塞 |
| 保存开始与完成 | 调用保存前和保存完成后立即记录 | 等待保存是否延迟了后续处理的开始 |
例如,在只回放一小组固定输入时,可以按以下方式设置测量点。本示例展示的是测量位置。
finiteUpdates 最多包含六个输入,trace 只保留这六个输入的测量结果;显示或文件输出在回放结束后进行。如果保存失败,不记录完成时间,并让异常传递给现有的故障处理程序。typescript
const trace = [];
for (const update of finiteUpdates) {
const started = performance.now();
state.apply(update);
const decision = decide(state);
const decided = performance.now();
const record = { seq: update.seq, price: state.price, decision };
const saveStarted = performance.now();
let saved: number | undefined;
try {
await saveAuxiliary(record);
saved = performance.now();
} finally {
trace.push({ seq: update.seq, started, decided, saveStarted, saved });
}
}const trace = [];
for (const update of finiteUpdates) {
const started = performance.now();
state.apply(update);
const decision = decide(state);
const decided = performance.now();
const record = { seq: update.seq, price: state.price, decision };
const saveStarted = performance.now();
let saved: number | undefined;
try {
await saveAuxiliary(record);
saved = performance.now();
} finally {
trace.push({ seq: update.seq, started, decided, saveStarted, saved });
}
}如果下一条更新在前一次保存完成后才开始处理,并且调用关系也显示相同依赖,就可以确认处理在等待保存。但仅凭这一点,不能断定所有等待都由保存造成。输入本身可能尚未到达,因此还应比较预定到达时间或可获取的接收时间戳。本文的小型可运行示例在开始时就已准备好相同的六个输入,并未重现实际到达间隔。使用真实数据时,还应记录捕获的输入序列以及测量开始的边界。
对于实际数据流,要说明“接收时间”是指套接字收到数据、SDK 回调开始,还是应用取出更新的时刻。未检查时钟偏差前,不要用本地时钟减去服务器发送时间戳。
在 SDK 回调中应查看哪里
SDK 是让应用能够使用数据接收等功能的库。如果您向 SDK 注册一个每次更新都会被调用的函数,即回调,也应在该入口记录更新 ID 和时间戳。在入口处记录
received,并将 state.apply 真正开始的时刻单独记录为 started。如果两者之间还有您自己的处理队列,更新可能已经到达回调,但尚未开始应用状态。例如,考虑仅包含两条更新的本地回放:让价格 100 的保存保持未完成,并在此期间送入价格 101。将以下差异与上面的
started、decided 和 saved 时间戳进行对照。| 观察项 | 决策与保存在同一个循环中 | 每个事件都有一个异步回调 |
|---|---|---|
| 在价格 100 保存完成前收到价格 101 | 接收已被记录,但价格 101 的状态应用尚未开始 | 价格 101 的回调可能被调用,从而推进状态应用与条件检查 |
| 价格 101 的条件检查开始 | 价格 100 已保存完成 | 价格 100 可能仍在保存中 |
| 接下来追踪什么 | 接收与决策处理之间的队列,以及等待保存完成的循环 | 重叠保存的数量、它们的完成顺序,以及 await 前后的状态更新 |
Node.js
EventEmitter 在调用下一个事件前,不会等待异步监听器返回的 Promise 完成。[S21] 要判断 SDK 是否另行等待 Promise,需要将其调用方实现与观察结果对照。第 7 节检查 Solana Stream SDK 的实际调用模式。如果价格 100 尚未保存完成,价格 101 的决策就已经推进,就不能将这条路径诊断为保存阻塞了下一次决策。在后一种情况下,如果前一次保存尚未完成,新输入就持续到达,未完成的保存会相互重叠。保存也可能不按顺序完成,因此设计需要控制启动数量和顺序。本文采用一个写入器和一个有界队列。我们还在执行顺序示例中,用两条固定更新和手动完成的模拟保存检查了这种差异。这是在诊断允许下一次决策开始的条件,不是在比较耗时。其中既没有真实 SDK 连接,也没有真实 I/O 的性能测量。
检查何时可以推迟保存
保存并非等待的唯一来源。应测量每个阶段,选择能够针对原因的更改。
| 检查区域 | 延迟来源 | 可以考虑更改的边界 |
|---|---|---|
| 保存到数据库或文件 | 等待 I/O 完成的时间 | 将允许在下一次条件检查之后完成的辅助保存交给单独的处理过程 |
| JSON 编码、压缩与计算 | 占用 CPU 的时间 | 减少传递的数据,并在必要时将工作移至用于 CPU 工作的执行资源 |
| 有界队列 | 等待容量的时间 | 检查容量、输入量、处理能力和队列满时的策略 |
| Mutex 及类似机制 | 等待锁释放的时间 | 重新审视谁负责更新状态、加锁范围以及持锁时长 |
状态更新和使用该状态的条件检查有其顺序。将每个阶段独立并行化并不足够。此处移除的依赖,是等待下一次条件检查并不需要的辅助保存。文件名或“日志”标签不能决定某条记录是否属于辅助记录。
| 保存内容示例 | 在规格与代码中检查什么 | 关于分离的判断 |
|---|---|---|
| 用于观察的价格与决策历史 | 下一次决策能否使用内存状态,并容忍延迟保存?其他处理或恢复路径是否依赖这次保存完成? | 确认这些条件后,可以考虑本文所示的分离方式 |
| 防止重启后重复下单的记录 | 约定是否要求发送订单前成功持久化标识符? | 如果有此要求,发送订单前必须保留对保存的等待 |
| 余额缓存或报价历史 | 这是可以重新获取的显示数据,还是下一次结算、余额更新或恢复的依据? | 追踪使用方与恢复处理后再作决定 |
即使记录的是同一价格和决策结果,仅用于观察还是作为恢复依据,也会要求不同的处理方式。检查保存结果的读取位置、故障处理、重启处理、规格和现有测试。如果依赖关系不清楚,应在调查期间保留对保存的等待。这个可运行示例涵盖交易条件的计算,并不涵盖根据决策结果发送订单的前提条件。
2. 让相同的工作分别推进
一边保存价格 100 的记录,一边使用价格 101 检查交易条件。让我们看看这两项操作分别推进时,减少的是哪一段等待。
决策规则是:前一个价格低于 101 且当前价格至少为 101 时为
enter;反向变化时为 exit;其他情况和第一条更新均为 hold。这条规则需要前一个状态,因此也说明了更新顺序为何重要。它只是用于比较设计的简单条件检查,不会发送订单。图 1 展示了等待保存完成的循环。这是一个解释模型,假设价格 100 在 0 ms 到达,价格 101 在 10 ms 到达,条件检查耗时 1 ms,保存每条记录耗时 30 ms。输入时间与处理开始时间是不同的,两种方式收到的是相同更新。图中的时间是设定值,并非测量结果。
更改前,循环为价格 100 判定
hold,然后从 1 ms 到 31 ms 等待保存。价格 101 在 10 ms 到达,但其条件检查直到 31 ms 才能开始,在 32 ms 产生 enter。从输入到条件检查完成的时间为 22 ms。更改后,输入侧将已经确定的价格与决策结果交给写入器,随后继续处理下一条更新。价格 101 在 10 ms 到达时,输入侧开始检查条件,并在 11 ms 产生
enter。从输入到条件检查完成的时间为 1 ms。与此同时,写入器仍在保存价格 100 的记录。两种方式都按顺序评估相同的两条更新,并按顺序保存相同的两条记录。更改前全部保存在 62 ms 完成,更改后在 61 ms 完成;两次各 30 ms 的保存工作依然存在。得到改善的是下一次条件检查等待前一次保存所花费的时间。
如果前一次辅助保存的完成阻碍了已经到达的后续更新的决策,这种设计就可能有帮助。如果下一次决策不依赖该次保存成功,队列尚有空间,且决策处理能够运行,那么下一次决策便可以在保存期间继续。移除不必要的完成依赖,才是可能降低延迟的原因。交接和调度也会增加成本,因此应在自己的实现中使用相同输入和工作,验证改善幅度。
但是,如果输入持续超过保存能力,有限队列会填满,输入侧也会等待。仅仅分离保存不会增加目标端的处理能力。如果下一次决策原本就能继续,或者主要原因是 CPU 计算或锁,则需要其他更改。既要检查是否把工作交给单独的处理过程,也要检查阻碍决策的依赖是否减少。[S04][S06][S07]
写入器收到的是该时刻已经确定的价格与决策结果。对于第一条更新,就是类似
{ seq: 0, price: 100, decision: "hold" } 的记录。即使下一条更新把 state.price 改为 101,正在保存的记录中的价格仍然是 100。相反,如果传递可变的 state 本身并在保存时再次读取价格,就可能记录成另一条更新的价格。正常完成时,所有记录都会保存。队列满时,输入侧持有当前记录并等待空间。如果写入器关闭或失败,需要核对已保存内容和尚未完成的内容。第 5、6 节讨论这些边界。
3. 在 TypeScript 中分离状态更新与保存
现有 TypeScript 代码也可以让状态更新与辅助保存分别推进。这里使用六个有限输入:100 → 101 → 102 → 100 → 101 → 99。决策依次为
hold、enter、hold、exit、enter 和 exit。所有输入在开始时已经准备好;没有用于模拟到达的计时器,也没有为了减慢保存而人为加入的等待。每条记录都写入真实的 JSONL 文件,保存会一直等待到 datasync() 完成。这个小型可运行示例用于追踪决策顺序、保存交接、队列满、故障和关闭。六个输入的处理速度不能证明语言排名或生产性能。完整版本见 TypeScript 实现,其输入为 prices.json。
首先,在一个函数中结合状态更新与记录创建。两种方式使用相同的
decide,并且只有输入侧更新 previousPrice。typescript
export function decide(state: State, input: Input): Record {
let decision: Decision = "hold";
if (state.previousPrice !== undefined && state.previousPrice < 101 && input.price >= 101) decision = "enter";
else if (state.previousPrice !== undefined && state.previousPrice >= 101 && input.price < 101) decision = "exit";
state.previousPrice = input.price;
return Object.freeze({ seq: input.seq, price: input.price, decision });
}export function decide(state: State, input: Input): Record {
let decision: Decision = "hold";
if (state.previousPrice !== undefined && state.previousPrice < 101 && input.price >= 101) decision = "enter";
else if (state.previousPrice !== undefined && state.previousPrice >= 101 && input.price < 101) decision = "exit";
state.previousPrice = input.price;
return Object.freeze({ seq: input.seq, price: input.price, decision });
}这条记录的字段是数字和字符串,创建后不再修改。两种方式也都在输入侧通过相同的
encode 进行 JSON 转换。写入器收到的是该输入已经确定的记录与 JSON 字符串。它不会在市场状态改变后再次读取该状态。分离前的流程如下。为了只展示依赖关系,省略了报告相关代码行。
typescript
const record = decide(state, update);
const pending = encode(record);
await save(file, pending, options);
// この保存が終わってから、次のupdateへ進む。const record = decide(state, update);
const pending = encode(record);
await save(file, pending, options);
// この保存が終わってから、次のupdateへ進む。分离后,启动一个写入器和一个有界队列。图 2 中,输入与决策处理位于左侧,队列位于中间,写入器位于右侧。状态仍由输入侧更新;等待保存完成的工作则移交给单独的角色。
完整版本中,以下一行在输入循环之前启动一次写入器,并保留返回的 Promise。
queue 存在于分离版本中。此时,输入循环继续运行,不等待写入器完成。typescript
const writerTask = queue === undefined ? undefined : writer(queue, file, options, emit);const writerTask = queue === undefined ? undefined : writer(queue, file, options, emit);继续处理下一条更新前,输入侧会检查
queue.put 的结果。这里的 put 是此可运行示例中的方法,并非标准 Node.js API。有空间时,它接受记录;队列满时,它保留当前记录并等待。写入器取出一条记录后,输入侧就能将记录放入空出的槽位并继续。这段代码只有一个输入循环,在继续下一条更新前等待
put。如果只是将它放入 SDK 为每条更新调用、且不等待其 Promise 完成的回调中,也会为每条更新创建一个新的入队等待。第 7 节检查调用方和上游保留方式。从这里开始,“已接受”表示队列已取得记录的所有权,与保存开始和保存完成分别计数。typescript
const wasAccepted = await queue.put(pending, () => emit("full_wait", record.seq));
if (!wasAccepted) {
unaccepted.push(record);
status = "closed";
break;
}
accepted.push(record.seq);const wasAccepted = await queue.put(pending, () => emit("full_wait", record.seq));
if (!wasAccepted) {
unaccepted.push(record);
status = "closed";
break;
}
accepted.push(record.seq);false 表示目标队列已关闭,无法接受记录。应保留这条记录并停止处理后续输入。这不同于队列满时等待空间。emit 和 accepted 为这些有限输入记录执行顺序和接受数量。只启动一个写入器,并将其 Promise 保留在
writerTask 中。不要为每个输入创建新的保存任务。因为输入侧也逐条等待 put,最多只有一个生产者等待空间。这可以防止未完成的保存离开队列后在其他位置不断增长。写入器对取出的每条记录调用共享的
save。这个函数在两种方式中执行的真实 I/O 完全相同。typescript
await file.writeFile(item.bytes);
await file.datasync();await file.writeFile(item.bytes);
await file.datasync();写入器只有在保存完成后才取下一条记录,从而保持该文件的写入顺序。为了分离异步 I/O,无须为每条记录创建一个 JavaScript worker。如果希望把 CPU 密集型 JavaScript 计算移至其他线程,可以考虑工作线程。[S12][S13]
输入结束后,关闭队列以停止进一步提交,并等待写入器的结果。这里使用的
close() 不会丢弃剩余记录。写入器持续取出记录,直到队列为空,然后退出。输入异常或提前停止也会进入同一条关闭路径。typescript
queue?.close();
if (writerTask !== undefined) writes = await writerTask;
try { await file.close(); } catch (error) { fileCloseError = String(error); }queue?.close();
if (writerTask !== undefined) writes = await writerTask;
try { await file.close(); } catch (error) { fileCloseError = String(error); }在完整版本中,这段代码位于
finally 内。收到写入器结果后才关闭文件。第 6 节讨论保存失败时的记录恢复。仅仅把调用替换为
void save(record),不能控制同时启动多少次保存、故障、顺序或关闭。本示例中保存输入、决策历史和执行顺序的数组,也只是用于核对最多六个输入的教学示例。对于持续运行的数据流,应将其替换为明确规定数量或保留上限的记录方式。将设计映射到现有 Python 代码
Python 可以通过
asyncio.Queue(maxsize=...) 和一个写入器 Task 建立相同的边界。使用 asyncio.create_task(writer(...)) 启动一个写入器并保留该 Task。在 Python 中,仅调用异步函数并不会开始执行,因此还需要正确对应启动方法。[S15][S16]如果直接执行同步 I/O 或长时间 CPU 工作,即使把工作移到另一个 Task,仍可能占用同一个事件循环。还应检查实际保存 API 在哪里等待、在哪里执行。[S16]
| 本文采用的设计 | Python 中需要检查的对应行为 |
|---|---|
| 启动一个写入器 | 使用 create_task 启动一次,不是每条更新启动一次 |
| 在容量范围内交接记录 | 一个输入生产者使用 await queue.put(record)。队列满时,保留当前记录并等待 |
| 关闭时恢复记录 | 在 Python 3.13 或更高版本中,处理 QueueShutDown 并保留生产者持有的未被接受记录 |
| 正常关闭时处理剩余内容 | 在 Python 3.13 或更高版本中,调用 shutdown(immediate=False) 后,继续用 get() 消费剩余记录,并等待写入器 Task 的结果 |
保存失败时,停止新提交,恢复失败记录以及队列中的剩余记录。仅有队列关闭通知,不能保证保存成功。此表用于指导如何将设计应用于 Python,并非完整的 Python 实现或性能结果。本文针对现有环境提供的可运行示例仅限于 Node.js。
4. 使用 Rust/Tokio 定义所有权与执行边界
我们继续使用第 3 节中相同的六个价格。输入 100 → 101 → 102 → 100 → 101 → 99,得到
hold → enter → hold → exit → enter → exit。沿用图 2、3 中的价格与记录 ID,跟踪 Rust 中的启动、交接与关闭。[L06]这个 Rust 示例的更改前后版本调用相同的
decide、encode 和 save。顺序版本在输入循环内完成一次 save(...).await 后才继续。在分离版本中,输入侧作出决策、编码 JSON,并将已经确定的记录交给一个写入器。这项更改分离了保存的执行位置,没有改变所执行的工作。[L06]从一个输入任务交接给一个写入器
首先,将以下四项操作与代码对应起来。这些角色表示任务职责,不代表为每个角色分别分配一个操作系统线程或 CPU 核心。
| 操作 | 在代码中追踪的位置 |
|---|---|
| 启动一次写入器 | 使用 mpsc::channel 创建交接通道,将接收端和文件传给 tokio::spawn |
| 按顺序决策,并确定该时刻的记录 | 输入循环中的 decide 和 encode。只有这个循环更新状态 |
| 交出当前记录 | 成功时继续。遇到 Full 时保留同一条记录并等待接受。遇到 Closed 时恢复记录并停止后续输入 |
| 检查已接受的保存是否完成 | 使用 drop(tx) 结束进一步提交,通过 writer_task.await 接收写入器结果 |
以下代码摘自完整可运行版本中的
run_after。其中省略了输入验证、文件创建、状态与报告数组初始化、测试停止与观察,以及最终 Report 的构建。CAPACITY 为 2。这个设置便于使用六个输入追踪队列满时的行为,并非生产环境的推荐容量。options.failure 和 options.gate 是用于检查故障与队列满的注入设置,正常运行不会使用。rust
let (tx, mut rx) = mpsc::channel(CAPACITY);
let writer_task = tokio::spawn(writer(rx, file, options.failure, options.gate));
for &input in inputs {
let pending = encode(decide(&mut state, input));
decided.push(input.seq);
let sent = match tx.try_send(pending) {
Ok(()) => Ok(()),
Err(mpsc::error::TrySendError::Full(pending)) => {
tx.send(pending).await.map_err(|error| error.0)
}
Err(mpsc::error::TrySendError::Closed(pending)) => Err(pending),
};
match sent {
Ok(()) => accepted.push(input.seq),
Err(pending) => {
unaccepted.push(pending.record);
break;
}
}
}
drop(tx);
let writes = writer_task.await.map_err(|error| {
io::Error::other(format!(
"writer task failed; no complete report: {error}; accepted IDs: {accepted:?}"
))
})?;let (tx, mut rx) = mpsc::channel(CAPACITY);
let writer_task = tokio::spawn(writer(rx, file, options.failure, options.gate));
for &input in inputs {
let pending = encode(decide(&mut state, input));
decided.push(input.seq);
let sent = match tx.try_send(pending) {
Ok(()) => Ok(()),
Err(mpsc::error::TrySendError::Full(pending)) => {
tx.send(pending).await.map_err(|error| error.0)
}
Err(mpsc::error::TrySendError::Closed(pending)) => Err(pending),
};
match sent {
Ok(()) => accepted.push(input.seq),
Err(pending) => {
unaccepted.push(pending.record);
break;
}
}
}
drop(tx);
let writes = writer_task.await.map_err(|error| {
io::Error::other(format!(
"writer task failed; no complete report: {error}; accepted IDs: {accepted:?}"
))
})?;mpsc::channel 创建发送端 tx 和接收端 rx,tokio::spawn 启动一个写入器。接收端与文件的所有权转移给写入器。只有输入循环更新价格 state,它不与写入器共享。[S03][S04][L06]decide 应用当前价格并确定决策,encode 则将该记录和 JSON 字节打包为 Pending。writer 将通过 recv().await 收到的每个项目传给共享的 save,只有在 write_all → flush → sync_data 完成后才取下一项。保存不会在价格状态改变后再次读取它。[S09][S10][L06]如果
try_send 成功,输入侧就可以继续下一个价格。队列满时,Full(pending) 返回同一个尚未被接受的值。将这个值传给 send(pending).await,等待容量,不重复决策,也不为每条更新创建另一个等待任务。发送 Future 在等待期间拥有该记录。输入侧等待时,Tokio 可以推进写入器;但如果保存持续停滞,输入侧也会继续等待。[S04][S05][L06]如果通道关闭,从
Closed(pending) 或挂起发送的错误中恢复值。map_err(|error| error.0) 提取发送错误持有的未被接受的 Pending,使两条关闭路径进入相同的恢复处理。将已经完成决策的记录保留在 unaccepted 中,不再继续后续输入。break 退出输入循环后,执行仍会继续到 drop(tx) 和 writer_task.await。前者结束进一步提交,后者检查写入器的关闭结果。应分别检查提交成功、保存完成和任务完成。[S04][S06][S34][L06]本示例不会取消挂起的发送。如果
select! 的另一个分支或任务中止丢弃了 send Future,它所拥有的记录也可能丢失。保存出错时,写入器恢复失败记录并返回;通过 writes.error 和 Report 中的各个类别检查结果。例如,如果任务自身发生 panic,且 writer_task.await 返回 JoinError,示例会返回错误,而不是完整的 Report。不要将本示例视为保证能从 panic、强制终止或崩溃中恢复。[S04][S34][L06]转移所有权不需要深拷贝
Pending 包含已经确定的记录和一个 Vec<u8>,属于非 Copy 类型。通道接受它之后,输入侧不能继续以所有者身份使用同一个值。如果通道已满或已关闭,输入侧收到返回值,就可以重新处理其所有权。这把“仍由输入侧持有的记录”和“已交给写入器的记录”与类型和代码联系起来。[S05][S22][L06]这种交接不需要深拷贝整条记录。不过,
encode 会为 JSON 字节分配内存。仅仅使用所有权转移,并不会让整个系统变成 zero-copy 或完全不分配内存。价格示例最多处理六条字段固定的小记录。由于只有输入侧持有可变状态,无须通过 Arc 或 Mutex 与写入器共享市场状态。第 7 节会单独讨论将设计适配到变长 Geyser 更新时添加的字节上限。
Arc、Send/Sync 与 Tokio 承担不同职责
您可以使用
Arc<Config>,在多个角色之间共享大型不可变配置等数据。Arc::clone 为同一块内存分配添加一个共享所有者,不会深拷贝内容。安全地并发修改内部值需要另一个机制,而原子引用计数操作也有成本。在配套的 Geyser 回放示例中,Arc 共享字节预算的 Semaphore,而预算的安全获取、释放和关闭由 Semaphore 本身提供。[S01][S11][S30][L04]| 机制 | 控制什么 | 不会自动保证什么 |
|---|---|---|
| 所有权转移 | 哪个角色持有记录和字节 | Zero-copy、零分配或保存成功 |
Arc | 对同一份数据的共享所有权 | 安全地并发修改内部值 |
Send | 类型是否允许拥有的值在线程之间移动 | 在另一个线程上执行 |
Sync | 类型是否允许跨线程使用共享引用 | 更新顺序或不会死锁 |
| Tokio | 异步任务、I/O 等待和通道的推进 | 专用 CPU、优先级或决策截止时间 |
Send 和 Sync 是类型的属性,Arc 是共享所有权工具,Tokio 是执行运行时。普通 tokio::spawn 要求 Future 及其输出满足 Send + 'static。这里的 'static 是类型约束,用来防止任务依赖短生命周期的借用。它不表示值永远存活;通过所有权转移进入任务的值,可以在不再需要时释放。[S02][S03]这个 Rust 价格示例的更改前后版本都在
current_thread 上运行。即使只有一个异步执行线程,在等待 I/O 时,其他任务仍可推进。文件 I/O 也会涉及 Tokio 内部的阻塞执行资源,因此这不代表整个进程只使用一个操作系统线程。配套的 Geyser 回放示例使用带两个 worker 的 multi_thread;请分别记录这些执行设置。[S08][S09][L04][L06]TypeScript 也能分离工作,为何首先推荐 Rust?
分离保存的设计同样可以用 TypeScript 实现。如果只更换语言,却保留相同的保存依赖,等待前一次保存的要求仍然存在。
当需要深入构建一个新的低延迟接收与决策引擎时,我们首先推荐 Rust。它让您能够把哪些值被拥有、复制或共享,与类型和交接联系起来:只提取决策所需的字段,将字节的所有权转移给保存过程,固定状态更新方,并限制 CPU 工作的执行位置和并发度。您可以逐项检查这些选择,将改进集中在确有需要的地方。[S01][S02][S07][S11][S22]
例如,如果输入侧成功发送后还试图修改同一个
pending,Rust 会在编译时拒绝这个所有权错误。在细化实现时,把单一决策任务与有界队列的设计,和交接值的类型检查连接起来,是一个优势。[S22]对于现有 TypeScript 或 Python 代码,应先在当前语言中修正依赖关系,并检查在所需输入条件下是否满足决策截止时间、顺序和保留要求。如果分离辅助保存就能满足这些要求,可以继续使用该实现。如果仍不满足,且解码或复制等某个特定阶段仍有成本,再考虑将该阶段提取到 Rust 中。决策时要纳入交接与运维成本;并不要求一次性重写 UI、配置和监控。
不要把首先推荐 Rust 的理由,重新解读为它在某个特定输入上具有速度优势的证据。应将控制复制、分配、共享、同步和执行位置的能力,与实际减少了什么、减少多少分别说明,并将两者各自关联到实现与验证结果。
5. 队列满时持有一条记录,空间释放后恢复
有界队列可以吸收短暂的输入增长。但容量用尽后,输入侧也必须等待空间。Tokio 的
send().await 和本示例 TypeScript 的 put 都会引入这种等待。分离保存并不意味着设计在任何情况下都无需等待。[S04]跟随这六个输入,观察它们如何交给容量为 2 的队列。记录 ID 0 对应价格 100 的
hold,ID 1 对应价格 101 的 enter,ID 2 对应价格 102 的 hold,ID 3 对应价格 100 的 exit。随后是 ID 4,对应价格 101 的 enter,以及 ID 5,对应价格 99 的 exit。ID 是输入顺序中的序号,不是价格。写入器写入 ID 0 时,ID 1 和 2 填满队列的两个槽位。输入侧已经完成 ID 3 的决策,但还无法交给写入器。因此,它持有 ID 3 并等待,不继续处理 ID 4 或更后面的输入。
ID 0 保存完成、写入器取出 ID 1 后,队列中空出一个槽位。输入侧交出一直保留的 ID 3,然后继续 ID 4。它不会重复 ID 3 的决策,而是保留同一条记录并继续交接。
此时,输入侧只有一项工作在等待。如果继续为每条更新创建一个等待
send 的任务,只是把无界等待移到了队列之外。这里的输入侧会等当前提交完成后,才处理下一条输入。| 写入器状态 | 输入侧行为 |
|---|---|
| 队列有空间 | 交出已经确定的记录,并继续下一条输入 |
| 队列已满,写入器仍在运行 | 保留当前记录,等待空间,然后恢复 |
| 写入器已关闭 | 保留未被接受的记录,或其恢复来源与 ID,并停止后续输入 |
队列满时的等待称为背压。如果输入持续超过保存能力,等待时间也会增长。要用有限内存保留所有必需记录,必须决定在哪里控制负载:让上游等待、停止接受输入,或提供可以用于恢复的已存储输入。
队列容量限制的是队列内部的记录。正在保存的记录、输入侧持有的记录、变长载荷、接收缓冲区和聚合结果还会消耗额外内存。处理真实数据的实现除了数量上限,也需要字节上限。[S04][S06]
在这个小示例中,所有输入仍然保留在文件里。对于实时 gRPC 或 WebSocket 输入,需要检查应用等待期间由谁保留更新,以及这种等待能够向上游传播多远。如果 SDK 已经丢弃了更新,仅限制保存队列并不能保留每条更新。
缺口之后获取最新价格,也不同于重建缺口期间发生的决策。如果处理停止时价格经历了 100 → 101 → 100,仅凭最新价格 100 无法重建中间的
enter 和 exit。如果要求保留每条更新的决策,就必须先恢复所需历史,才能恢复处理。恢复历史并不意味着它可用于当前决策
图 3 中,空间释放后恢复的是此前停止的输入处理。在实时交易应用中,除了不丢记录地处理更新,还需要检查这些更新是否仍足够新,能够用于决策。即使按顺序回放旧输入可以恢复状态,回放期间达到的
enter 或 exit 条件,也未必能直接用于当前订单。| 应用需要的信息 | 从队列满或数据缺口恢复时需要定义什么 |
|---|---|
| 决策需要每个中间状态转换 | 恢复必要的历史和之前的状态,然后按顺序处理。将历史回放产生的结果是否可以用于下单,与当前决策分开定义 |
| 决策可以从最新状态开始 | 验证这一规格,重新同步到一致的最新状态,再作决策。不要将其视为已经重建中间条件触发 |
| 当前决策有新鲜度截止要求 | 定义可接受的最大更新年龄和决策截止时间,以及超限后停止决策与恢复的条件 |
增加容量可以保留更多记录,但也允许旧更新等待更久。除了记录数量与字节数,还应检查最旧的更新已经等待多久。不要在超过截止时间后默默丢弃必需记录;应将记录保留与当前决策处理可以运行的条件分开。这个教学示例按顺序处理全部六个输入,没有实现实时输入的重新获取或自动恢复下单。
6. 区分提交成功、保存完成与关闭
提交成功时,记录只是交给了写入器。只有在规定的写入与同步操作完成后,才能把保存计为成功。在这个 TypeScript 示例中,每条记录的完成条件是
writeFile → datasync;在 Rust 验证中,则是 write_all → flush → sync_data。[S09][S10][S12]Tokio 的
flush 完成进行中的写入,与请求将操作系统缓冲区同步到设备的 sync_data 不同。我们可以验证这些 API 调用成功,但这不等于测试了断电、新建文件的目录同步,或故障设备的恢复。正常关闭会结束新输入处理、停止进一步提交,并等待写入器处理完已接受的记录。使用 Tokio 时,释放所有发送句柄并等待写入器的
JoinHandle。在 TypeScript 示例中,调用 queue.close(),然后等待 writerTask。如果输入处理因异常或提前停止而结束,也必须进入这条关闭路径。[S03][S06]保存失败时,区分未写入记录与结果未知的记录
保存失败时,写入器关闭队列。等待空间的输入侧也会获知队列已关闭,并停止后续处理。TypeScript 和 Rust 价格示例在 Report 中保留未写入、保存结果未知及未被接受保存的完整记录。对于未处理的输入,Rust 在 Report 中保留完整 Input,而 TypeScript 使用 Report 的 ID 和原始输入文件进行追踪。关闭后,Rust 写入器还会取出队列中的剩余项目,在退出前将其恢复为未写入记录。[S06][L03][L06]
配套的 Rust Geyser 回放示例在账本中保留未完成的 ID 与状态,并在不可变 protobuf 文件中保留原始输入。由于保留方式不同,应检查报告中的每个 ID 是否都能追溯到原始输入或记录。仅仅丢弃接收端句柄,不会保存尚未读取的载荷。对于实时输入,应另行设计未保存载荷的重新获取或保留方式。[S06][L04]
如果记录在开始写入前失败,就可以确定尚未写入。但开始写入后发生错误,可能留下部分写入。如果写入后无法确认同步,也没有满足规定的保存完成条件。这些示例将此类记录保留为保存结果未知,不将其计为成功。
关闭时,将输入 ID 分类为以下五种状态。验证这五个集合互不重叠,并且覆盖所有原始输入。
| 关闭时的状态 | 含义 |
|---|---|
| 保存完成 | 决策以及规定的写入、同步操作均已完成 |
| 已接受,未写入 | 已交给写入器,但因写入前失败或从剩余队列恢复而仍未写入 |
| 已接受,保存结果未知 | 无法确认规定的保存完成,例如开始写入后发生故障 |
| 已决策,未被接受保存 | 决策已完成,但关闭导致无法交给写入器 |
| 未处理 | 输入仍保留着,但尚未应用状态和处理决策 |
已决策数量不是要在这五个集合之外另加的集合。它统计前四类中已完成决策的输入。应将其与交给队列的数量和成功保存的数量分开检查。
盲目重试结果未知的追加写入,可能产生重复记录或不完整的行。恢复需要使用记录 ID、目标端事务以及去重约定。这些示例涵盖停止后识别并报告未完成项目,没有实现自动重试或崩溃后的恢复。
如果存储目标不返回,还需要另行决定关闭过程应等待多久。仅写上
await writerTask 或 task.await,无法解决关闭截止时间或安全取消已开始 I/O 的问题。如有需要,应结合存储目标的约定,验证到达截止时间时能够保留和报告什么。7. 在实际接收代码中找到改进位置
将这种分离引入自己的交易应用时,要从接收更新的函数回溯到调用它的代码。即使保存函数包含
await,调用方是否等待其完成,也会改变问题本身和需要修复的位置。先检查以下三项。| 在代码中检查什么 | 确认后可以考虑的更改 |
|---|---|
| 已经到达的下一条更新,是否必须等待前一次辅助保存才能作出决策? | 移除决策不需要的保存等待,并将已经确定的记录交给独立写入器 |
| 下一次决策在推进,但未完成保存是否持续累积? | 限制并发保存和等待工作的数量,并决定队列满时在哪里保留输入 |
| 必需的更新到达保存队列前,是否已被接收侧丢弃? | 回溯到丢弃分支,重新审视输入保留、重新获取,以及出现缺口后恢复决策的条件 |
同样是“跟不上更新”的症状,可能需要这些不同的修复。关联下一条更新的处理开始、保存开始与完成、排队工作和被丢弃更新,可以缩小改进位置的范围。
给 TypeScript 接收示例添加保存
官方 Solana Stream SDK 的 Geyser 接收示例,也使用队列将收到的更新交给处理逻辑。在所检查版本的 TypeScript 示例中,
runHandler 为每条更新调用 onUpdate,并为返回的 Promise 附加错误处理,但不等待其完成。因此,即使按下面的方式实现保存,后续更新处理也可能在前一次保存完成前开始。[S24][S25]以下插入代码展示了这个问题。
decideAndSnapshot 是用于说明的函数名,代表现有应用的状态更新、决策以及已确定记录的创建。typescript
// この形を登録しただけでは、保存の並行数と順序は決まらない。
onUpdate: async (update) => {
const record = decideAndSnapshot(update);
await saveAuxiliary(record);
}// この形を登録しただけでは、保存の並行数と順序は決まらない。
onUpdate: async (update) => {
const record = decideAndSnapshot(update);
await saveAuxiliary(record);
}如果这里的未完成保存不断累积,就属于上表的第二种情况。有限的接收队列,并不限制项目取出后产生的 Promise 或保存操作。这个示例的
processedUpdates 也不能用作已完成保存的数量。[S24]在这种情况下,可以考虑让一个决策任务按顺序创建已经确定的记录,并交给有界保存路径。如果只是把第 3 节的
await queue.put(...) 放入每个回调,队列满时也会为每条更新产生更多等待中的回调。应在负责交接输入的位置,阻止继续处理下一条输入,直到当前记录被接受,并检查在此期间接收侧哪里可以保留输入。在所检查的 TypeScript 示例中,
processEvents 内的 eventQueue.shift() 取出更新,然后依次传给 processEvent、runHandler 和 onUpdate。在自己的实现中,找到负责将下一条更新交给处理过程的对应位置。还要检查等待保存路径接受记录的 Promise 是否返回给调用方,使等待能够反向传播到取出更新的组件。如果将该组件改为等待保存接受,更新就会累积在它前面的 SDK 队列中。除了保存函数本身,还要处理停止出队位置上游的保留问题。[S24]例如,若已确认上游能够保留尚未取出的更新,而且单个处理任务可以在获取下一条更新前等待,就按以下顺序推进。这是定位修改点的步骤,并不是用于替换某个 SDK 专用 API 的完整代码。
- 输入任务取出下一条更新。
- 更新状态、检查条件,并确定该时刻的记录。
- 保留当前记录,直到保存路径接受它。不要为每条更新创建更多等待任务。
- 接受后,再继续下一次获取。关闭时,恢复未被接受的记录,停止后续输入,并检查写入器的关闭结果。
利用 SDK 和提供方的约定,确定此任务等待期间更新会发生什么。
| 已确认的接收侧条件 | 连接前必须确定的内容 |
|---|---|
| 可以等待再取下一条更新,上游也能保留必需更新 | 将等待集中到一个出队任务。还要检查上游缓冲区的数量与字节上限,以及满时的行为 |
| 回调无法停止,但所需历史可以重新获取 | 保留捕获位置、缺失范围和之前的状态,建立从停止决策、恢复历史到恢复运行的路径 |
| 回调无法停止,且所需历史无法恢复 | 当前连接不满足保留每条更新的条件。重新审视订阅范围、输入保留方式和接收侧容量 |
不要假设 SDK 支持暂停或重新获取历史。不仅要检查是否存在 API,还要检查停止期间更新保留在哪里,以及所需范围是否能够恢复。如果规格允许重新同步到最新状态,应按第 5 节中的另一种条件处理,不要将其等同于保留每条更新。
这项更改旨在控制未完成工作和保存顺序。将保存改为逐条执行,会改变原本已并发执行的 I/O 的性能,因此需要重新测量决策延迟与保存吞吐量。
此外,所检查版本的 Rust 和 TypeScript 示例都存在一条路径,在内部队列满时丢弃普通更新。在下游添加保存队列,无法恢复上游已经丢失的更新。如果决策还需要中间更新,设计中就必须包含队列满时的保留与重新获取。这是所引用示例的行为,不是所有 Geyser 客户端共有的限制。[S24][S25]
对于实时输入,需要检查停止期间由谁保留更新,以及处理可以从哪里恢复。与输入文件始终可用的教学示例不同,仅停止循环不一定能保留未处理更新。如果无法恢复所需历史,应区分回到最新状态和重建缺失区间内的决策。
所引用示例的版本、函数映射及队列满分支,记录在源码检查说明和来源 [S24][S25] 中。检查自己版本的调用模式,判断是否适用相同模式。
将价格示例适配为 Geyser 格式更新
到目前为止,分离的是有序输入处理与其固定结果的保存。配套的 Rust 回放示例解码保留在文件中的 Geyser 格式交易更新,并提取对指定程序的调用。其计算内容与价格条件检查不同,但关系相同:一个输入任务确定结果,再交给一个写入器。[L04]
| 角色 | 六个价格的示例 | Geyser 格式回放示例 |
|---|---|---|
| 输入 | 带 ID 的价格更新 | 保存在文件中的 protobuf 更新 |
| 输入侧工作 | 应用价格并确定决策 | 解码更新并提取程序调用 |
| 交给保存的内容 | 该更新的价格与决策结果 | 从该更新提取的结果 |
| 保存顺序 | 按输入顺序逐条保存 | 按输入顺序逐条保存 |
| 受限内容 | 最多六个输入,以及容量为 2 的小记录队列 | 除队列记录数外,还限制交接的变长字节总量 |
Geyser 回放示例的更改前后版本也使用相同的
process_update、encode_result 和 save。它们先使用 try_reserve 预留一个队列槽位;如果队列满,则调用 reserve().await,同时输入侧继续持有当前字节。获得槽位后,slot.send(Pending { ... }) 转移所有权。值的交接时机与价格示例的 try_send 不同,但两者都在接受前持续追踪当前项目,不会提前继续后续输入。[S04][S05][L04]变长记录需要独立于记录数量的字节上限。此回放示例的验证设置为队列容量 64 条记录,以及由 Semaphore 实施的 1 MiB 字节预算。预算涵盖获取许可后输入侧当前记录、队列中的记录和正在保存的记录。
Pending 持有许可,处理后释放许可便归还容量。如果单条记录超过上限,示例会返回错误,而不是永远等待空间。[S30][L04]这 1 MiB 不是整个进程的内存上限。获取字节容量前的当前记录、已解码输入、原始测试数据和账本分别消耗内存。无论 64 条记录还是 1 MiB,都不是应该直接用于生产环境的数值。应根据实际输入大小、可接受积压和保存能力进行选择。[L04]
此回放示例是用于检查这些边界如何在本地输入下工作的应用型教学材料。它不是能够直接接入实时 SDK 回调的完整适配器。请根据自己的接收路径,实现前面检查过的调用模式、上游保留方式及缺口后的恢复条件。
8. 用少量输入检查处理顺序与保留情况
重构设计后,先验证相同输入仍然产生相同的决策和保存记录。即使分离保存让下一次决策更早推进,决策顺序或保存内容的改变也会违背预期更改的目的。少量输入便于将每条记录追踪到底。
为顺序版本和分离版本提供相同的六个输入
为 TypeScript 和 Rust 价格示例提供相同的六个输入:100 → 101 → 102 → 100 → 101 → 99。在每种语言内部,顺序版本和分离版本使用相同的决策函数、JSON 编码与保存函数。预期决策为
hold → enter → hold → exit → enter → exit。[L03][L06]bash
node example.ts --mode before --out run-before
node example.ts --mode after --capacity 2 --out run-after
node --test checks/example.test.mjsnode example.ts --mode before --out run-before
node example.ts --mode after --capacity 2 --out run-after
node --test checks/example.test.mjs在
records.jsonl 中检查保存内容与顺序,在 report.json 中检查决策、保存、未完成 ID 和执行顺序。已验证正常完成时,两种方式都按相同顺序对全部六个输入作出决策并保存,JSONL 的每个字节都一致。示例不建立外部连接,使用真实文件写入与 datasync()。[L03]在同一个 ZIP 的
price-rust 目录内运行 Rust 版本。另见相同六个价格的执行说明。该版本已在 release 构建中验证,使用 Rust 1.100.0、Tokio 1.54.1,以及由 Cargo.lock 固定的依赖项。[L06]bash
cargo build --release --locked
cargo run --release --locked -- before out/before
cargo run --release --locked -- after out/after
cargo test --release --lockedcargo build --release --locked
cargo run --release --locked -- before out/before
cargo run --release --locked -- after out/after
cargo test --release --locked对于 Rust,也应检查每个输出目录中的
records.jsonl 和 report.json。顺序版本和分离版本的六个输入决策、保存顺序以及 JSONL 的每个字节都一致。两种语言得到相同的预期值,验证了教学示例执行相同工作。这里不比较处理时间,也不比较语言速度排名。再次运行时,请使用新的输出目录。[L06]填满队列,查看当前记录保留在哪里
队列满测试使用一个需要手动释放写入器的门控。与第 5 节图 3 一样,它在 ID 0 开始写入前停止写入器,将 ID 1 和 2 放入队列,并让 ID 3 留在输入侧。测试验证处理不会继续到 ID 4,并且写入器恢复后,输入侧能够交出保留的 ID 3 并继续。
这验证了只要还有容量,即使写入器暂停,决策也能推进;容量用尽后,输入侧保留当前记录并等待。无需比较耗时,就能检查分离与 Full 处理。
Rust 的 Full 测试在
report.json 中记录以下观察。这是从 full 数组中提取的一项。[L06]json
{"current_seq":3,"decided_count":4,"accepted_count":3,"queue_depth":2}{"current_seq":3,"decided_count":4,"accepted_count":3,"queue_depth":2}截至 ID 3,已有四个输入完成决策;ID 0–2 这三个已被接受;ID 1 和 2 这两个位于队列中。挂起的发送持有当前的 ID 3。保存完成在
writes.saved 中单独检查。门控释放、处理正常结束后的报告中,ID 0–5 按顺序出现在该字段中。此次运行的 full 数组开头还包含 ID 2 的一次观察。由于测试会等到 current_seq 等于 3 的观察后才恢复保存,不要将第一项读成 ID 3 时的状态。[L06]| 要测试的场景 | 需要验证的行为 |
|---|---|
| 正常完成 | 对全部原始输入按相同顺序决策,并保存相同记录 |
| 队列满 | 保留当前记录,不继续下一条输入,空间释放后交出同一条记录 |
| 写入器关闭 | 保留未被接受的记录或其恢复来源与 ID,并停止后续输入 |
| 保存前失败 | 区分尚未写入的记录和尚未决策的输入 |
| 写入后、确认同步前失败 | 保留未知结果,不计为保存完成 |
| 提前停止或输入处理异常 | 进入关闭流程,处理完已接受的记录,并检查写入器结果 |
在每种情况下,都要检查第 6 节的五种状态是否无重叠地覆盖全部原始输入。这能避免因为丢失必要工作而显得更快的更改。TypeScript 价格示例检查了表中的每个场景。Rust 价格示例使用六项功能检查,验证正常完成、从 Full 恢复、Closed、保存前和写入后失败,以及在三条记录之后停止输入。输入处理异常或 panic 的恢复,不在这个 Rust 价格示例的验证范围内。[L03][L06]
配套的 Geyser 回放示例使用明确标明为合成的 protobuf 输入,检查提取结果、保存内容、顺序、数量与字节上限、故障和停止。其输入与工作内容,和验证价格决策的新示例是区分开的。[L04]
在自己的实现中验证等待是否减少
应用这项设计时,使用第 1 节的测量点,检查下一次决策能否不等前一次保存完成就开始。更改前后使用相同的输入、决策和保存条件,并将决策完成与全部保存完成分开检查。把交接成本和队列满等待纳入其中,才能确定自己实现中哪些等待减少了、哪些仍然存在。
原始运行记录、故障情况下的数量、构建条件和补充测量方法说明,汇总在验证说明中。正文图示是用于追踪依赖关系的解释模型;其中假设的时间不是真实环境中的改善测量。
9. 找出下一处等待,在并发与并行之间选择
分离保存等待,不会消除接收后的所有延迟。重新测量解码、状态更新、条件检查、保存数据准备、队列和锁,找到下一个耗时位置。同时检查,将操作移入另一个任务后,交接或同步等待是否增加。
并发可以在 I/O 等待期间推进其他工作;本文的写入器就是一个例子。对于长时间占用 CPU 的工作,应考虑减少计算或在独立执行资源上运行。Tokio 的
spawn 不会为每个任务创建专用线程,仅添加 async 也不会把繁重计算移到另一个 CPU。[S03][S07][S08]例如,在另一个 CPU 上准备或压缩大量数据时,要定义在保留输入的同时允许交接多少数据、worker 数量和队列上限。选择 Tokio 的
spawn_blocking 或 CPU 工作执行资源时,不仅要限制运行中操作的数量,还要限制等待启动的工作。不要假设已经开始的阻塞操作可以轻易取消。[S07]依赖前一次更新结果的状态更新和条件检查,仍必须保持顺序。即使独立计算并行执行,也要定义结果的应用顺序、过时结果的处理方式,以及缺失输入后的重新同步。决策前必需的保存,其依赖不能移除。应检查在保留必要依赖的前提下等待是否减少,而不只是看启动的操作是否更多。
Node.js 工作线程和独立的 Python 进程同样可以分离 CPU 工作。比较时应包含传递数据的转换与复制、队列等待和结果返回时间。还应分别检查所使用的 Python 构建、GIL 和扩展模块带来的条件。[S13][S16][S17]
对于新的低延迟接收与决策引擎,Rust 是首选。在现有 TypeScript 或 Python 应用中,一种做法是保留 UI、配置、监控和研究工作,只将测量表明确有需要的部分提取到 Rust。要将设计更改带来的改善,与迁移后实现的差异分开,并使用相同输入、工作和正确性要求进行比较。验证分离 I/O 保存,不能证明 CPU 并行化的收益。请用实际将使用的载荷和处理进行检查。
10. 附录:使用 AI 智能体诊断和改进代码
这两个提示词可以分别独立交给能够读取您仓库的 AI。第一个涵盖等待诊断、最小更改和相同条件下的比较;第二个涵盖队列满、故障和关闭。延续既有权限与工作规则,并根据哪些等待减少了,以及顺序和记录是否得到保留,来评估建议的更改。
1. 诊断接收后的等待,并实现必要的分离
text
Investigate the path in this repository from receiving an update through state updates and decision processing. Identify where waiting for saving or data preparation to finish delays the next update, or where unfinished auxiliary work keeps accumulating. Carry the work through to an evidence-based minimal fix and a before/after comparison under the same conditions.
First check the existing working rules, permissions, uncommitted changes, and versions of the language, runtime, SDK, and dependencies. Do not overwrite other changes. Production changes, load tests against external services, transaction submission, and paid contracts are outside scope. Proceed with the necessary local investigation, modifications, and small replay checks. Do not output private keys, credentials, or customer data.
1. Trace the actual files and functions for reception, decoding, state updates, decisions, and saving, including their callers. Check whether callers wait for the Promise/Future returned by an async function, invoke it independently for each event, and when they take items from intervening queues. Do not infer from the name async alone that the next reception is blocked or that the work runs on another thread. Also check whether the SDK's processed count includes save completion and whether its default full-queue handling discards updates.
2. Distinguish I/O waits, CPU occupancy, queue waits, and lock waits, and separate confirmed facts, hypotheses about causes, and unmeasured areas. Use consumers, recovery processing, README files, specifications, and existing tests to classify records that must be durable before a decision versus auxiliary records that may be saved later. If the required update order, stopping decisions and resynchronizing after gaps, or the save-failure contract is unclear, investigate the evidence without removing the dependency. Also investigate the maximum acceptable update age, decision deadlines, and the conditions for resuming current decisions after history recovery. If these are undefined, report them as undefined rather than inventing values.
3. Before making changes, fix the finite input and expected correctness results. Distinguish input captured from a real stream from input created for illustration, and record the input hash, count, order, and capture range. Do not call the entry point where an SDK delivers decoded values a wire-reception timestamp, or call re-encoded values the original wire bytes. Where usable input or an execution environment is unavailable, do not label an unexecuted check as a completed measurement.
4. Define measurement start and end points. Using the monotonic clock of the same process, distinguish the handoff/replay schedule, decision start and completion, save acceptance, and save start and completion. If reproducing arrival intervals, do not delay the schedule to match completion of the previous save. If measuring a finite sequence with all inputs ready at the start, state that condition and do not call it latency from actual reception. Do not draw conclusions about a language's computation speed from numbers that mix timer-resumption time, actual processing, and real I/O. Accompany p50/p95/p99 with sample counts and calculation methods, and reconcile processed, unprocessed, discarded, duplicate, and save-unconfirmed IDs, decision results, order, and saved contents. The denominator for the fraction completed within the deadline is all target inputs. For queue counts, bytes, the oldest wait, CPU, and memory, distinguish what was verified from what was not measured.
5. First make the minimal fix in the existing environment for the confirmed cause. If separating auxiliary work, define ownership of immutable records, a single updater of mutable state, a bounded queue, and the number of writers. send().await waits when full. For Full from a nonblocking submission, use the contract to decide whether to retain the returned record and wait or stop, and report either the waiting time or the unprocessed range. For Closed, retain the unaccepted record and handle stopping and recovery. Also check upstream retention. Distinguish successful submission from save completion, and write completion from sync/commit. Submission failure after a decision is “decided, not accepted for saving”; do not count it as unprocessed input. Do not make a change appear faster by using an unbounded queue, a waiting task per update, or discarding required updates.
6. Compare before and after using the same input, arrival conditions, work, correctness requirements, and save-completion conditions. Test normal operation, a short burst, a full queue, save failure, writer closure, and draining on normal shutdown; reconcile the location and order of every input. Check whether shutdown handling is reached even on an input exception or early return, and retain unknown save outcomes as unknown. Also report conditions under which there is no improvement.
7. If costs remain after improving the existing environment and the requirements justify it, evaluate extracting the low-latency reception and decision processing into Rust. Specify the target processing and costs of copying, allocation, sharing, synchronization, and boundary crossings. Record the release build and dependency lock, runtime, and SDK/native layers, and compare under the same conditions. Arc provides shared ownership, Send/Sync are type properties, and Tokio is the execution runtime. Arc alone does not guarantee safe concurrent mutation of inner values or zero-copy. Do not conclude that a complete migration is needed or rank languages by speed without measurement evidence.
Finally, report the diagnostic evidence, modified files and functions, reproduction commands, before/after results, correctness and retention checks, unmeasured scope, and the specific problems that remain. Do not unnecessarily change areas that have no problem or are already separated. If the evidence does not support an article's or proposal's central claim, do not treat it as complete merely by adding limitations; explicitly state whether that claim is retained or rejected and which work remains unfinished.Investigate the path in this repository from receiving an update through state updates and decision processing. Identify where waiting for saving or data preparation to finish delays the next update, or where unfinished auxiliary work keeps accumulating. Carry the work through to an evidence-based minimal fix and a before/after comparison under the same conditions.
First check the existing working rules, permissions, uncommitted changes, and versions of the language, runtime, SDK, and dependencies. Do not overwrite other changes. Production changes, load tests against external services, transaction submission, and paid contracts are outside scope. Proceed with the necessary local investigation, modifications, and small replay checks. Do not output private keys, credentials, or customer data.
1. Trace the actual files and functions for reception, decoding, state updates, decisions, and saving, including their callers. Check whether callers wait for the Promise/Future returned by an async function, invoke it independently for each event, and when they take items from intervening queues. Do not infer from the name async alone that the next reception is blocked or that the work runs on another thread. Also check whether the SDK's processed count includes save completion and whether its default full-queue handling discards updates.
2. Distinguish I/O waits, CPU occupancy, queue waits, and lock waits, and separate confirmed facts, hypotheses about causes, and unmeasured areas. Use consumers, recovery processing, README files, specifications, and existing tests to classify records that must be durable before a decision versus auxiliary records that may be saved later. If the required update order, stopping decisions and resynchronizing after gaps, or the save-failure contract is unclear, investigate the evidence without removing the dependency. Also investigate the maximum acceptable update age, decision deadlines, and the conditions for resuming current decisions after history recovery. If these are undefined, report them as undefined rather than inventing values.
3. Before making changes, fix the finite input and expected correctness results. Distinguish input captured from a real stream from input created for illustration, and record the input hash, count, order, and capture range. Do not call the entry point where an SDK delivers decoded values a wire-reception timestamp, or call re-encoded values the original wire bytes. Where usable input or an execution environment is unavailable, do not label an unexecuted check as a completed measurement.
4. Define measurement start and end points. Using the monotonic clock of the same process, distinguish the handoff/replay schedule, decision start and completion, save acceptance, and save start and completion. If reproducing arrival intervals, do not delay the schedule to match completion of the previous save. If measuring a finite sequence with all inputs ready at the start, state that condition and do not call it latency from actual reception. Do not draw conclusions about a language's computation speed from numbers that mix timer-resumption time, actual processing, and real I/O. Accompany p50/p95/p99 with sample counts and calculation methods, and reconcile processed, unprocessed, discarded, duplicate, and save-unconfirmed IDs, decision results, order, and saved contents. The denominator for the fraction completed within the deadline is all target inputs. For queue counts, bytes, the oldest wait, CPU, and memory, distinguish what was verified from what was not measured.
5. First make the minimal fix in the existing environment for the confirmed cause. If separating auxiliary work, define ownership of immutable records, a single updater of mutable state, a bounded queue, and the number of writers. send().await waits when full. For Full from a nonblocking submission, use the contract to decide whether to retain the returned record and wait or stop, and report either the waiting time or the unprocessed range. For Closed, retain the unaccepted record and handle stopping and recovery. Also check upstream retention. Distinguish successful submission from save completion, and write completion from sync/commit. Submission failure after a decision is “decided, not accepted for saving”; do not count it as unprocessed input. Do not make a change appear faster by using an unbounded queue, a waiting task per update, or discarding required updates.
6. Compare before and after using the same input, arrival conditions, work, correctness requirements, and save-completion conditions. Test normal operation, a short burst, a full queue, save failure, writer closure, and draining on normal shutdown; reconcile the location and order of every input. Check whether shutdown handling is reached even on an input exception or early return, and retain unknown save outcomes as unknown. Also report conditions under which there is no improvement.
7. If costs remain after improving the existing environment and the requirements justify it, evaluate extracting the low-latency reception and decision processing into Rust. Specify the target processing and costs of copying, allocation, sharing, synchronization, and boundary crossings. Record the release build and dependency lock, runtime, and SDK/native layers, and compare under the same conditions. Arc provides shared ownership, Send/Sync are type properties, and Tokio is the execution runtime. Arc alone does not guarantee safe concurrent mutation of inner values or zero-copy. Do not conclude that a complete migration is needed or rank languages by speed without measurement evidence.
Finally, report the diagnostic evidence, modified files and functions, reproduction commands, before/after results, correctness and retention checks, unmeasured scope, and the specific problems that remain. Do not unnecessarily change areas that have no problem or are already separated. If the evidence does not support an article's or proposal's central claim, do not treat it as complete merely by adding limitations; explicitly state whether that claim is retained or rejected and which work remains unfinished.2. 检查队列满、故障和关闭期间,更新与记录是否始终可核对
text
Examine the separation of reception and decision processing from auxiliary saving in this repository, and verify that updates and records remain accounted for when the queue is full, saving fails, the writer closes, or processing ends normally. Make minimal fixes only for defects supported by evidence, and verify before and after using the same small local input.
Check the existing working rules, permissions, uncommitted changes, and versions of the language, runtime, SDK, and dependencies. Do not overwrite other changes, and limit modifications to the retention and shutdown conditions in this task. Production changes, load tests against external services, transaction submission, and paid contracts are outside scope. Proceed with the necessary local investigation, modifications, and replay checks. Do not output private keys, credentials, or customer data.
1. Identify the actual files and functions, the owner of received input, the updater of mutable state, queue capacity and the per-record byte limit, the number of writers, retries, and shutdown paths. Also investigate whether SDK callbacks wait for async processing to finish, whether processed counts mean save completion, and whether an upstream queue discards items when full. Separate confirmed facts, hypotheses, and unmeasured areas. If saving before a decision is required, preserve that dependency and do not treat the record as auxiliary.
2. Classify the same input IDs as unprocessed; decided, not accepted for saving; accepted, not written; accepted, save outcome unknown; or save complete. Verify that these five sets are disjoint and cover all input, and detect discards, duplicates, order violations, and double-counting. Do not arbitrarily classify records with unknown outcomes, such as partial writes, as successful or unwritten. Do not add the decided count on top of the five sets. Local sequence numbers assigned to a real stream identify the captured sequence; they are not proof that every server update was captured. Record disconnected, retransmitted, and uncaptured ranges separately.
3. For nonblocking submission, trace where values returned by Full/Closed go. If send().await or reserve().await is used, check whether that wait stops the next update. Also check for unbounded waiting in Promises or tasks after leaving the queue, and in retry destinations. Do not assume that finite memory can handle sustained overload without loss or stopping.
4. Before making changes, use finite input to reproduce normal operation, a short burst, insufficient capacity, save failure, a channel closed by the saving side, and a normal stop partway through input. Do not treat successful submission as save completion. Check that acceptance stops after failure, accepted items are drained or recovered, all send handles are released, the save task's result is checked, and flush/sync/commit conditions are met. Check whether shutdown handling runs even on an input exception or early return. Retain records with unknown outcomes, such as partial writes, as unknown.
5. Check what stopping can preserve. Retaining a local input file differs from requesting retransmission or retention of live input. State which input ID and which preceding state processing will resume from. Do not treat SDK pausing or history refetching as available without verification. Do not assume that the latest state alone can reconstruct conditions reached during a gap. Separate state recovery through historical replay from the conditions that permit current decisions to resume, and report freshness or deadline requirements as unknown if they are unknown. Do not unconditionally retry appends whose saving is unconfirmed and thereby create more duplicates or incomplete lines.
6. Implement the necessary minimal fix and rerun under the same conditions. Compare counts and IDs for processed, unprocessed, discarded, save-unconfirmed, and duplicate inputs, as well as decision results, save order, completion criteria, and exit codes. Avoid parallel saving that breaks required order or changes that discard unsaved records to finish. Report queue counts, bytes, the oldest wait, latency, CPU, and memory only where measured. Explicitly state if panic, process crash, forced termination, power loss, or shutdown deadlines have not been tested.
Finally, report the confirmed problems and evidence, minimal fixes, reproduction commands, whether every input is accounted for consistently in each scenario, and the remaining failure scope. Do not change areas that already meet the requirements. Zero locally discarded items alone does not guarantee that all items were saved, crash durability, gap-free reception of a real stream, or production correctness.Examine the separation of reception and decision processing from auxiliary saving in this repository, and verify that updates and records remain accounted for when the queue is full, saving fails, the writer closes, or processing ends normally. Make minimal fixes only for defects supported by evidence, and verify before and after using the same small local input.
Check the existing working rules, permissions, uncommitted changes, and versions of the language, runtime, SDK, and dependencies. Do not overwrite other changes, and limit modifications to the retention and shutdown conditions in this task. Production changes, load tests against external services, transaction submission, and paid contracts are outside scope. Proceed with the necessary local investigation, modifications, and replay checks. Do not output private keys, credentials, or customer data.
1. Identify the actual files and functions, the owner of received input, the updater of mutable state, queue capacity and the per-record byte limit, the number of writers, retries, and shutdown paths. Also investigate whether SDK callbacks wait for async processing to finish, whether processed counts mean save completion, and whether an upstream queue discards items when full. Separate confirmed facts, hypotheses, and unmeasured areas. If saving before a decision is required, preserve that dependency and do not treat the record as auxiliary.
2. Classify the same input IDs as unprocessed; decided, not accepted for saving; accepted, not written; accepted, save outcome unknown; or save complete. Verify that these five sets are disjoint and cover all input, and detect discards, duplicates, order violations, and double-counting. Do not arbitrarily classify records with unknown outcomes, such as partial writes, as successful or unwritten. Do not add the decided count on top of the five sets. Local sequence numbers assigned to a real stream identify the captured sequence; they are not proof that every server update was captured. Record disconnected, retransmitted, and uncaptured ranges separately.
3. For nonblocking submission, trace where values returned by Full/Closed go. If send().await or reserve().await is used, check whether that wait stops the next update. Also check for unbounded waiting in Promises or tasks after leaving the queue, and in retry destinations. Do not assume that finite memory can handle sustained overload without loss or stopping.
4. Before making changes, use finite input to reproduce normal operation, a short burst, insufficient capacity, save failure, a channel closed by the saving side, and a normal stop partway through input. Do not treat successful submission as save completion. Check that acceptance stops after failure, accepted items are drained or recovered, all send handles are released, the save task's result is checked, and flush/sync/commit conditions are met. Check whether shutdown handling runs even on an input exception or early return. Retain records with unknown outcomes, such as partial writes, as unknown.
5. Check what stopping can preserve. Retaining a local input file differs from requesting retransmission or retention of live input. State which input ID and which preceding state processing will resume from. Do not treat SDK pausing or history refetching as available without verification. Do not assume that the latest state alone can reconstruct conditions reached during a gap. Separate state recovery through historical replay from the conditions that permit current decisions to resume, and report freshness or deadline requirements as unknown if they are unknown. Do not unconditionally retry appends whose saving is unconfirmed and thereby create more duplicates or incomplete lines.
6. Implement the necessary minimal fix and rerun under the same conditions. Compare counts and IDs for processed, unprocessed, discarded, save-unconfirmed, and duplicate inputs, as well as decision results, save order, completion criteria, and exit codes. Avoid parallel saving that breaks required order or changes that discard unsaved records to finish. Report queue counts, bytes, the oldest wait, latency, CPU, and memory only where measured. Explicitly state if panic, process crash, forced termination, power loss, or shutdown deadlines have not been tested.
Finally, report the confirmed problems and evidence, minimal fixes, reproduction commands, whether every input is accounted for consistently in each scenario, and the remaining failure scope. Do not change areas that already meet the requirements. Zero locally discarded items alone does not guarantee that all items were saved, crash durability, gap-free reception of a real stream, or production correctness.这里发布的两个提示词的日文原版,均由与实现者不同的 AI 完整复制,并在独立本地副本中分别应用于 TypeScript 示例和 Rust 价格示例。每份副本的试验都检查了八项 TypeScript 功能测试和 release 构建中的六项 Rust 功能测试,以及顺序、分离 CLI 和故障场景。目标文件、原因诊断、复现命令、原始提示词的哈希和结果,记录在等待诊断与分离试验以及 Full、故障与关闭试验中。
对于已经满足要求的分离版本,没有作出不必要的更改。此教学材料没有定义可接受的数据年龄、决策截止时间,或恢复历史后重新开始当前决策的条件。依照提示词,试验将这些事项报告为未定义、未验证,而没有自行编造数值。本地回放中的保留检查,并不能证明已验证生产决策恢复或真实 Geyser 性能。
这些 AI 试验检查的是提示词的具体程度,以及与文章的一致性。它们不同于证明真实读者理解程度、满意度、再次阅读意愿,或在未知仓库中成功率的研究。
11. 将高速数据转化为快速响应
即使数据流很快,接收后累积的不必要等待,仍会延迟数据到达决策逻辑。检查实现中在等待什么、哪些顺序是必要的,再只分离能够独立推进的工作。分离保存后,继续测量 CPU 工作、共享状态和队列等待,逐一细化必要边界。
应用内部等待减少后,将接收前的数据新鲜度、RPC 或流访问、网络路径和执行位置作为独立阶段进行检查。定义所需响应时间、输入量以及停止和恢复条件,然后使用实际会采用的协议、地区、时段和负载比较 ERPC 环境。选择高速数据和高效使用它的实现,以满足同一组要求。
参考资料与可运行材料
-
[S01]
Arc::clone添加一个共享同一堆内存分配的所有者,不会深拷贝内容。原子引用计数操作有成本,仅靠 Arc 也不能让内部值的并发修改变得安全。https://doc.rust-lang.org/std/sync/struct.Arc.html -
[S02]
Send是允许值的所有权在线程之间移动的属性;Sync允许跨线程使用共享引用。它们本身不是执行器、锁或顺序保证。https://doc.rust-lang.org/book/ch16-04-extensible-concurrency-sync-and-send.html -
[S03]
tokio::spawn向执行器注册异步任务。普通 spawn 要求 future 及其输出满足 Send 和 static 约束,执行位置取决于运行时配置。运行时关闭时,不保证未完成任务能够运行至完成。https://docs.rs/tokio/1.54.1/tokio/task/fn.spawn.html 区分并发与并行,以及在单线程上运行任务:https://tokio.rs/tokio/tutorial/spawning -
[S04] 有界 Sender 的
send().await会等待容量。try_send在队列满或接收端关闭时返回。发送成功不意味着已接收或保存完成。https://docs.rs/tokio/1.54.1/tokio/sync/mpsc/struct.Sender.html -
[S05]
TrySendError::Full(T)和Closed(T)返回未发送的值。忽略该值而不恢复,可能丢失必需记录。https://docs.rs/tokio/1.54.1/tokio/sync/mpsc/error/enum.TrySendError.html -
[S06] 所有 Sender 都被丢弃且剩余队列被处理完后,recv 返回 None。丢弃 Receiver 也会丢弃未读取的值。接收端也可以先关闭,再处理完剩余内容。https://docs.rs/tokio/1.54.1/tokio/sync/mpsc/index.html
-
[S07]
spawn_blocking将执行移到阻塞池。CPU 工作需要并发上限,达到上限后工作会排队。对已经开始工作的中止,不能与中止普通异步任务同等处理。https://docs.rs/tokio/1.54.1/tokio/task/fn.spawn_blocking.html -
[S08] 可以选择
current_thread或multi_thread。worker_threads设置多线程运行时的 worker 数量,并不表示将 worker 绑定到 CPU 核心。https://docs.rs/tokio/1.54.1/tokio/runtime/struct.Builder.html -
[S09] Tokio File 的 flush 完成尚未结束的写入,这与保证操作系统缓冲区已持久化到设备不同。sync_all/sync_data 是独立操作。https://docs.rs/tokio/1.54.1/tokio/fs/struct.File.html
-
[S10] write_all 和 flush 是不同的 AsyncWriteExt 方法。不要省略错误与取消处理。https://docs.rs/tokio/1.54.1/tokio/io/trait.AsyncWriteExt.html
-
[S11] Tokio 中的共享状态可以通过短时间持锁,或由拥有状态的任务配合消息传递来处理。应考虑不跨越 await 持锁的设计。https://tokio.rs/tokio/tutorial/shared-state
-
[S12] Node 的异步 fs API 在事件循环之外运行,但不保证对同一文件的多次修改自动有序。应避免不等待而使写入重叠的更改。sync 是单独的持久化请求。https://nodejs.org/api/fs.html
-
[S13] Node worker_threads 可以并行执行 JavaScript CPU 工作。内置异步 I/O 适合 I/O,CPU 工作通常也通过 worker 池进行控制。https://nodejs.org/api/worker_threads.html
-
[S14] 在 Node 中,应区分长时间占用 CPU 的工作与异步 I/O 等待。https://nodejs.org/learn/asynchronous-work/dont-block-the-event-loop
-
[S15] Python asyncio.Queue(maxsize>0) 设置容量;队列满时 put 等待,put_nowait 抛出 QueueFull。asyncio.Queue 不是线程安全的。https://docs.python.org/3/library/asyncio-queue.html
-
[S16] Python 也通过 asyncio 任务支持并发。to_thread 的 CPU 并行能力取决于 GIL、实现和扩展模块。https://docs.python.org/3/library/asyncio-task.html
-
[S17] Python 提供线程、解释器和进程执行器。应将 worker 数量与等待执行的工作量分别控制。https://docs.python.org/3/library/concurrent.futures.html
-
[S21] Node EventEmitter 的监听器调用,以及异步监听器的 Promise 与拒绝处理。事件发出和监听器返回的 Promise 完成,是分别处理的。https://nodejs.org/api/events.html
-
[S22] Rust 所有权和向函数传值。非 Copy 值移动后,原始变量不能继续作为其所有者使用。调用方可以接收返回值。https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html
-
[S24] Solana Stream SDK 的 TypeScript Geyser 运行器:
runHandler不等待 Promise;队列满时的丢弃,以及记录已处理数量的位置。源码检查固定到某个提交。https://github.com/ValidatorsDAO/solana-stream/blob/3b22307033ec6d7b78242d3b4cc9f3168209dcfd/client/geyser-ts/src/lib/geyserRunner.ts -
[S25] 同一 SDK 的 Rust Geyser 运行器:
stream.next()接收入口、try_send、在 Full 时丢弃 Update,以及断开连接事件处理。https://github.com/ValidatorsDAO/solana-stream/blob/3b22307033ec6d7b78242d3b4cc9f3168209dcfd/client/geyser-rs/src/runtime/runner.rs -
SDK 源码审计与测量边界:sdk-stream-entry-audit.md。
-
[S30] Tokio Semaphore:获取与释放容量、自有许可,以及关闭时唤醒等待方。应区分 Arc 的共享所有权与同步机制。https://docs.rs/tokio/1.54.1/tokio/sync/struct.Semaphore.html
-
[L04] 完整 Rust Geyser 回放示例与功能检查:src/replay.rs、执行说明,以及功能检查记录。合成输入验证所有权交接、处理结果、顺序、Full、故障和关闭。详细验证条件见验证说明。
-
[S34] 等待 JoinHandle 会返回任务输出或 JoinError。仅丢弃句柄会使任务脱离管理,并不确认完成。https://docs.rs/tokio/1.54.1/tokio/task/struct.JoinHandle.html
-
Validators DAO 官方 Discord:https://discord.gg/C7ZQSrCkYR
-
一手来源映射:技术来源与已验证要点












