Wake the idle order service at the next effective GTD deadline

This commit is contained in:
2026-12-03 14:43:10 +08:00
parent 2a6332620c
commit 4262b7adaa
5 changed files with 38 additions and 5 deletions
+4 -2
View File
@@ -1,6 +1,6 @@
# cli
可信操作员的 JSON 行命令入口。启动参数指定数据库文件,程序复用同一个 Exchange 连接。标准输入的每一行对应标准输出的一条 JSON 回执。输入结束时进程退出。输入线程和交易线程之间使用有界队列,交易线程每五秒检查到期任务,空闲时仍可撤掉断线客户端的挂单。
可信操作员的 JSON 行命令入口。启动参数指定数据库文件,程序复用同一个 Exchange 连接。标准输入的每一行对应标准输出的一条 JSON 回执。输入结束时进程退出。输入线程和交易线程之间使用有界队列,交易线程按最近一笔 GTD 的有效截止时间唤醒,最长每五秒检查一次其他到期任务。
## 文件依赖
@@ -24,8 +24,10 @@ sequenceDiagram
U->>C: 启动并指定数据库路径
C->>E: 打开数据库
C->>S: 传入连接和输入输出流
opt 输入空闲达到定时检查时刻
S->>E: 查询最近一笔待到期 GTD 剩余时间
opt 达到 GTD 截止点或五秒检查周期
S->>E: maintain 检查到期注册、释放余单并同步盘口
S->>E: 重新计算下次唤醒时间
end
loop 输入一行
U->>S: JSON 命令
+12 -2
View File
@@ -25,11 +25,21 @@ pub(crate) fn serve(
}
});
let interval = Duration::from_secs(5);
let mut next = Instant::now() + interval;
// New GTDs live at least two minutes, so rechecking within five seconds
// also catches orders added by another process before their cutoff.
let next_check = |exchange: &Exchange| -> Result<Instant> {
let start = Instant::now();
let wait = exchange
.next_gtd_expiry()?
.unwrap_or(interval)
.min(interval);
Ok(start + wait)
};
let mut next = next_check(engine)?;
loop {
if Instant::now() >= next {
engine.maintain()?;
next = Instant::now() + interval;
next = next_check(engine)?;
}
let line = match reader.recv_timeout(next.saturating_duration_since(Instant::now())) {
Ok(line) => line?,