84 lines
4.7 KiB
JavaScript
84 lines
4.7 KiB
JavaScript
// 定性「快速断开重连」的失败形态。零消息副作用,只建连/关闭。
|
||||
|
|
//
|
|||
|
|
// 起因:reliability 探针里 R3 在 R 刚 close 后立刻建连,首次握手直接 error + close 1006,
|
|||
|
|
// 等 150ms 后第二次建连才成功。同时 ws-conn-limit-probe 证明并发 6 条连接全部正常,
|
|||
|
|
// 因此排除「并发连接数上限」。本探针用可控间隔重复「close -> 立即重连」,回答:
|
|||
|
|
// Q1 失败是否与「距上次 close 的间隔」相关(服务端半开未回收 / 重连限流)?
|
|||
|
|
// Q2 失败率随间隔如何变化,从而给出 QLinker 重连退避的最小安全间隔建议?
|
|||
|
|
// Q3 失败时的确切形态(onerror 先于 onclose?close code 是 1006 还是别的?是否有 retcode 帧?)
|
|||
|
|
// —— 决定 QLinker 的重连状态机该把哪种现象当作「等待后重试」而不是「认证失败」。
|
|||
|
|
//
|
|||
|
|
// 用法: NAPCAT_WS_TOKEN=xxx node tools/probe/ws-reconnect-probe.mjs [--gaps=0,100,300,1000,3000] [--rounds=4]
|
|||
|
|
|
|||
|
|
const WS_URL = process.env.NAPCAT_WS_URL || 'ws://192.168.5.32:16099';
|
|||
|
|
const TOKEN = process.env.NAPCAT_WS_TOKEN;
|
|||
|
|
const arg = (k, d) => (process.argv.find((a) => a.startsWith(`--${k}=`)) || `--${k}=${d}`).split('=')[1];
|
|||
|
|
const GAPS = arg('gaps', '0,100,300,1000,3000').split(',').map(Number);
|
|||
|
|
const ROUNDS = Number(arg('rounds', '4'));
|
|||
|
|
|
|||
|
|
if (!TOKEN) { console.error('缺少环境变量 NAPCAT_WS_TOKEN。凭据不得硬编码。'); process.exit(1); }
|
|||
|
|
|
|||
|
|
const fs = await import('node:fs');
|
|||
|
|
const path = await import('node:path');
|
|||
|
|
const OUT_DIR = process.env.PROBE_OUT || 'local';
|
|||
|
|
fs.mkdirSync(OUT_DIR, { recursive: true });
|
|||
|
|
const OUT = path.join(OUT_DIR, `ws-reconnect-${new Date().toISOString().replace(/[:.]/g, '-')}.jsonl`);
|
|||
|
|
const rec = (o) => { fs.appendFileSync(OUT, JSON.stringify(o) + '\n'); return o; };
|
|||
|
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|||
|
|
const t0 = Date.now();
|
|||
|
|
const at = () => Date.now() - t0;
|
|||
|
|
|
|||
|
|
function connectOnce(tag) {
|
|||
|
|
return new Promise((resolve) => {
|
|||
|
|
const r = { tag, opened: null, authed: null, closed: null, code: null, err: null, frames: [], ws: null };
|
|||
|
|
const ws = new WebSocket(`${WS_URL.replace(/\/$/, '')}/?access_token=${encodeURIComponent(TOKEN)}`);
|
|||
|
|
r.ws = ws;
|
|||
|
|
const hard = setTimeout(() => { try { ws.close(); } catch {} settle(); }, 12000);
|
|||
|
|
let settled = false;
|
|||
|
|
const settle = () => { if (settled) return; settled = true; clearTimeout(hard); resolve(r); };
|
|||
|
|
ws.onopen = () => { r.opened = at(); };
|
|||
|
|
ws.onmessage = (ev) => {
|
|||
|
|
let o; try { o = JSON.parse(String(ev.data)); } catch { return; }
|
|||
|
|
if (o.post_type === 'meta_event' && o.meta_event_type === 'lifecycle') { r.authed = at(); }
|
|||
|
|
else if (o.retcode && o.retcode !== 0) r.frames.push({ at: at(), retcode: o.retcode, message: o.message ?? null });
|
|||
|
|
};
|
|||
|
|
ws.onclose = (e) => { r.closed = at(); r.code = e.code ?? null; settle(); };
|
|||
|
|
ws.onerror = () => { if (r.err === null) r.err = at(); };
|
|||
|
|
setTimeout(() => { if (r.authed !== null) settle(); }, 2500);
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const rows = [];
|
|||
|
|
for (const gap of GAPS) {
|
|||
|
|
let ok = 0, fail = 0;
|
|||
|
|
const detail = [];
|
|||
|
|
for (let round = 1; round <= ROUNDS; round++) {
|
|||
|
|
// 先建立一条基准连接并立即关闭,制造「刚断开」的状态
|
|||
|
|
const base = await connectOnce(`base-${gap}-${round}`);
|
|||
|
|
try { base.ws && base.ws.close(); } catch {}
|
|||
|
|
await sleep(gap);
|
|||
|
|
const r = await connectOnce(`try-${gap}-${round}`);
|
|||
|
|
const good = r.authed !== null;
|
|||
|
|
good ? ok++ : fail++;
|
|||
|
|
const line = `gap=${gap}ms round=${round}: ${good ? 'OK' : 'FAIL'} opened=${r.opened} authed=${r.authed} close=${r.code}@${r.closed} err@${r.err}` +
|
|||
|
|
(r.frames.length ? ' frames=' + JSON.stringify(r.frames) : '');
|
|||
|
|
detail.push(line);
|
|||
|
|
console.log(' ' + line);
|
|||
|
|
rec({ gap, round, ok: good, opened: r.opened, authed: r.authed, close_code: r.code, err_at: r.err, frames: r.frames });
|
|||
|
|
try { r.ws && r.ws.close(); } catch {}
|
|||
|
|
await sleep(1200);
|
|||
|
|
}
|
|||
|
|
rows.push({ gap, ok, fail, detail });
|
|||
|
|
console.log(`gap=${gap}ms => 成功 ${ok}/${ROUNDS}\n`);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log('===== 汇总 =====');
|
|||
|
|
for (const r of rows) console.log(` 断开后等待 ${String(r.gap).padStart(5)}ms 重连: 成功 ${r.ok}/${r.ok + r.fail}`);
|
|||
|
|
const firstClean = rows.find((r) => r.fail === 0);
|
|||
|
|
console.log(' 首次出现「全部成功」的最小间隔 = ' + (firstClean ? firstClean.gap + 'ms' : `>${GAPS[GAPS.length - 1]}ms(需增大 --gaps 上界)`));
|
|||
|
|
console.log(' ⇒ 该值只能作为本轮观测建议,不得当作上游保证;QLinker 重连仍必须用指数退避 + lifecycle 作为可用判据(GC-008)。');
|
|||
|
|
rec({ kind: 'summary', rows: rows.map(({ gap, ok, fail }) => ({ gap, ok, fail })) });
|
|||
|
|
console.log('\n结果 -> ' + OUT);
|
|||
|
|
await sleep(300);
|
|||
|
|
process.exit(0);
|